source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GSFile.java@ 9874

Last change on this file since 9874 was 9874, checked in by kjdon, 19 years ago

merged from branch ant-install-branch: merge 1

  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 KB
Line 
1/*
2 * GSFile.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import java.io.File;
22//import org.apache.soap.encoding.soapenc.Base64;
23import org.apache.axis.encoding.Base64;
24import java.io.BufferedInputStream;
25import java.io.BufferedOutputStream;
26import java.io.FileInputStream;
27import java.io.FileOutputStream;
28import java.io.InputStream;
29import java.io.IOException;
30import java.lang.ClassLoader; // to find files on the class path
31import java.net.URL;
32import java.util.ArrayList;
33
34/**
35 * GSFile - utility class for Greenstone.
36 *
37 * all file paths are created here
38 * also has file utility methods
39 *
40 * @author <a href="mailto:[email protected]">Katherine Don</a>
41 * @version $Revision: 9874 $
42 * @see File
43 */
44
45public class GSFile {
46
47 static public String dtdDir(String gsdl3_home) {
48 return gsdl3_home + File.separatorChar + "resources" +
49 File.separatorChar + "dtd";
50 }
51 /** site config file path */
52 static public String siteConfigFile(String site_home) {
53 return site_home + File.separatorChar+"siteConfig.xml";
54
55 }
56 /** interface config file path */
57 static public String interfaceConfigFile(String interface_home) {
58 return interface_home + File.separatorChar+"interfaceConfig.xml";
59
60 }
61
62 /** collection directory path */
63 static public String collectDir(String site_home) {
64 return site_home+File.separatorChar+"collect";
65 }
66
67
68 /** collection config file path*/
69 static public String collectionConfigFile(String site_home,
70 String collection_name) {
71 return collectionConfigFile(collectionBaseDir(site_home, collection_name));
72 }
73 static public String collectionConfigFile(String collection_home) {
74 return collectionEtcDir(collection_home) +
75 File.separatorChar+"collectionConfig.xml";
76
77 }
78 /** collection init file path*/
79 static public String collectionInitFile(String site_home,
80 String collection_name) {
81 return site_home+File.separatorChar+"collect"+
82 File.separatorChar+collection_name+
83 File.separatorChar+"etc"+
84 File.separatorChar+"collectionInit.xml";
85
86 }
87
88 /** collection build config file path*/
89 static public String collectionBuildConfigFile(String site_home,
90 String collection_name ) {
91 return site_home+File.separatorChar+"collect"+
92 File.separatorChar+collection_name+
93 File.separatorChar+"index"+
94 File.separatorChar+"buildConfig.xml";
95 }
96 /** collection build config file path*/
97 static public String collectionBuildConfigFileBuilding(String site_home,
98 String collection_name ) {
99 return collectionBuildConfigFileBuilding(collectionBaseDir(site_home, collection_name));
100 }
101 static public String collectionBuildConfigFileBuilding(String collection_home ) {
102 return collection_home+
103 File.separatorChar+"building"+
104 File.separatorChar+"buildConfig.xml";
105 }
106
107 /** XML Transform directory path */
108 static public String xmlTransformDir(String interface_home) {
109 return interface_home+File.separatorChar+"transform";
110 }
111
112 /** collection base directory path */
113 static public String collectionBaseDir(String site_home,
114 String collection_name) {
115 return site_home+File.separatorChar+"collect"+
116 File.separatorChar+collection_name;
117 }
118 /** collection archive directory path */
119 static public String collectionArchiveDir(String site_home,
120 String collection_name) {
121 return collectionArchiveDir(collectionBaseDir(site_home, collection_name));
122 }
123 static public String collectionArchiveDir(String collection_home) {
124 return collection_home+File.separatorChar+"archives";
125 }
126
127 /** collection building directory path */
128 static public String collectionBuildDir(String site_home,
129 String collection_name) {
130 return collectionBuildDir(collectionBaseDir(site_home, collection_name));
131 }
132 static public String collectionBuildDir(String collection_home) {
133 return collection_home+File.separator+"building";
134 }
135 /** collection building directory path */
136 static public String collectionEtcDir(String site_home,
137 String collection_name) {
138 return collectionEtcDir(collectionBaseDir(site_home, collection_name));
139 }
140 static public String collectionEtcDir(String collection_home) {
141 return collection_home+File.separator+"etc";
142 }
143
144 /** collection building directory path */
145 static public String collectionImportDir(String site_home,
146 String collection_name) {
147 return collectionImportDir(collectionBaseDir(site_home, collection_name));
148
149 }
150 static public String collectionImportDir(String collection_home) {
151 return collection_home + File.separatorChar+"import";
152 }
153 /** collection building directory path */
154 static public String collectionIndexDir(String site_home,
155 String collection_name) {
156 return collectionIndexDir(collectionBaseDir(site_home, collection_name));
157
158 }
159 static public String collectionIndexDir(String collection_home) {
160 return collection_home + File.separatorChar+"index";
161 }
162
163 /** text path (for doc retrieval) relative to collectionBaseDir */
164 static public String collectionTextPath(String index_stem) {
165 return "index"+File.separatorChar+"text"+File.separatorChar+
166 index_stem;
167 }
168
169 /** index path (for querying) relative to collectionBaseDir */
170 static public String collectionIndexPath(String index_stem,
171 String index_name) {
172 return "index"+File.separatorChar+index_name+File.separatorChar+
173 index_stem;
174 }
175
176 /** collection resources directory path */
177 static public String collectionResourceDir(String site_home,
178 String collection_name) {
179 return collectionResourceDir(collectionBaseDir(site_home, collection_name));
180
181 }
182 static public String collectionResourceDir(String collection_home) {
183 return collection_home + File.separatorChar+"resources";
184 }
185
186 /** absolute path for an associated file */
187 static public String assocFileAbsolutePath(String site_home,
188 String collection_name,
189 String assoc_file_path,
190 String filename) {
191 return collectionBaseDir(site_home, collection_name)+
192 File.separatorChar+"index"+File.separatorChar+
193 "assoc"+File.separatorChar+assoc_file_path+
194 File.separatorChar+filename;
195 }
196
197 static public String siteHome(String gsdl3_home, String site_name) {
198 return gsdl3_home + File.separatorChar + "web" +
199 File.separatorChar + "sites" +
200 File.separatorChar +site_name;
201 }
202
203 static public String interfaceHome(String gsdl3_home,
204 String interface_name) {
205 return gsdl3_home + File.separatorChar + "web" +
206 File.separatorChar + "interfaces" +
207 File.separatorChar + interface_name;
208 }
209
210 static public String siteStylesheetFile(String site_home,
211 String filename) {
212 return site_home+File.separatorChar + "transform" +
213 File.separatorChar + filename;
214 }
215 static public String collStylesheetFile(String site_home,
216 String coll_name,
217 String filename) {
218 return collectionBaseDir(site_home, coll_name) + File.separatorChar+
219 "transform" + File.separatorChar + filename;
220 }
221
222
223 /** returns the absolute path to a stylesheet
224 * stylesheets are looked for in the following places, in the
225 * following order:
226 * current collection, current site, current interface, base interfaces
227 * returns null if the file cannot be found
228 *
229 * this is not so good because sites may be on a different computer */
230 static public String stylesheetFile(String gsdl3_home,
231 String site_name,
232 String collection,
233 String interface_name,
234 ArrayList base_interfaces,
235 String filename) {
236
237 String site_home = siteHome(gsdl3_home, site_name);
238 // try collection first
239 File stylesheet = null;
240 if (!collection.equals("")) {
241
242 String coll_home = collectionBaseDir(site_home, collection);
243 stylesheet = new File(coll_home +File.separatorChar+
244 "transform"+File.separatorChar+filename);
245 if (stylesheet.exists()) {
246 return stylesheet.getPath();
247 }
248 }
249
250 // try site one next
251 stylesheet = new File(site_home +File.separatorChar+
252 "transform"+File.separatorChar+filename);
253 if (stylesheet.exists()) {
254 return stylesheet.getPath();
255 }
256
257 // try current interface
258 String interface_home = interfaceHome(gsdl3_home,
259 interface_name);
260 stylesheet = new File(interface_home+File.separatorChar+
261 "transform"+File.separatorChar+filename);
262 if (stylesheet.exists()) {
263 return stylesheet.getPath();
264 }
265 // try base interface
266 if (base_interfaces==null || base_interfaces.size()==0) {
267 return null; // no base interfaces to look for
268 }
269 for (int i=0; i<base_interfaces.size(); i++) {
270 interface_home = interfaceHome(gsdl3_home, (String)base_interfaces.get(i));
271 stylesheet = new File(interface_home+File.separatorChar+
272 "transform"+File.separatorChar+filename);
273 if (stylesheet.exists()) {
274 return stylesheet.getPath();
275 }
276 }
277
278 // still can't find it and we have looked everywhere
279 return null;
280 }
281
282 /** returns the path to the stylesheet used to transform config file
283 * format statements*/
284 public static String configFileFormatStylesheet() {
285 URL url = ClassLoader.getSystemResource("config_format.xsl");
286 if (url == null) {
287 return "";
288 }
289 return url.getFile();
290 }
291
292 /** base directory for phind data */
293 public static String phindBaseDir(String site_home, String coll_name,
294 String phind_index) {
295 return site_home + File.separatorChar +
296 "collect" + File.separatorChar +
297 coll_name + File.separatorChar +
298 "index" + File.separatorChar +
299 "phind"+phind_index;
300 }
301
302 /** the gdbm database file -
303 * note, need to change extension depending on OS */
304 static public String GDBMDatabaseFile(String site_home,
305 String collection_name) {
306
307 return site_home + File.separatorChar +
308 "collect" + File.separatorChar +
309 collection_name + File.separatorChar +
310 "index" + File.separatorChar + "text" + File.separatorChar +
311 collection_name + ".ldb";
312
313 }
314
315 // some file utility methods
316
317 /** read in a file and encode it using base64
318 * encoded data returned as a String */
319 static public String base64EncodeFromFile(String in_filename) {
320 byte [] data=null;
321 try {
322 data = readFile(in_filename);
323 } catch (Exception e) {
324 System.err.println("GSFile.base64EncodeFromFile: couldn't read the file");
325 }
326 String encodedString = Base64.encode(data);
327 return encodedString;
328
329 }
330
331 /** decode some base64 data, and write it to the specified file */
332 static public boolean base64DecodeToFile(String data, String out_filename) {
333 try {
334 byte[] buffer=Base64.decode(data);
335 writeFile(buffer, out_filename);
336
337 } catch (Exception e) {
338 System.err.println("GSFile.base64DecodeToFile: file opening/closing errors"+e.getMessage());
339 return false;
340 }
341 return true;
342
343 }
344
345 /** read in a file to a byte array */
346 public static byte[] readFile(String filename) throws IOException {
347 File file = new File(filename);
348 BufferedInputStream bis = new BufferedInputStream(new
349 FileInputStream(file));
350 int bytes = (int) file.length();
351 byte[] buffer = new byte[bytes];
352 int readBytes = bis.read(buffer);
353 bis.close();
354 return buffer;
355 }
356
357 /** write a byte array to a file */
358 public static void writeFile(byte [] buffer, String filename) throws IOException {
359 File file = new File(filename);
360 BufferedOutputStream bos = new BufferedOutputStream(new
361 FileOutputStream(file));
362 bos.write(buffer);
363 bos.close();
364 }
365
366 public static boolean deleteFile(File f) {
367
368 if (f.isDirectory()) {
369 File[] files = f.listFiles();
370 for (int i=0; files!=null && i<files.length; i++) {
371 deleteFile(files[i]);
372 }
373
374 }
375 // delete the file or directory
376 return f.delete();
377 }
378
379 public static boolean copyFile(File source, File destination) {
380 if (!source.isFile()) {
381 System.err.println("GSFile.copyFile(): "+source.getPath()+" is not a file!");
382 return false;
383 }
384 try {
385 destination.getParentFile().mkdirs();
386 FileInputStream in = new FileInputStream(source);
387 FileOutputStream out = new FileOutputStream(destination);
388 int value = 0;
389 while((value = in.read()) != -1) {
390 out.write(value);
391 }
392 in.close();
393 out.close();
394 } catch (Exception e) {
395 System.err.println("GSFile.copyFile(): something went wrong copying "+source.getPath()+" to "+destination.getPath());
396 System.err.println("Exception: "+e.getMessage());
397 return false;
398 }
399
400 return true;
401 }
402 /** Recursively moves the contents of source file to destination,
403 * maintaining paths.
404 * @param source A File representing the directory whose contents you
405 * wish to move.
406 * @param destination A File representing the directory you wish to move
407 * files to.
408 * @return true if successful
409 */
410 public static boolean moveDirectory(File source, File destination) {
411
412 // first try rename
413 if (source.renameTo(destination)) {
414 return true;
415 }
416
417 // john T. said that this sometimes doesn't work, so you have to copy files manually
418
419 // copied from gatherer Utility class
420 File input[] = source.listFiles();
421 for(int i = 0; i < input.length; i++) {
422 File output = new File(destination, input[i].getName());
423 if (input[i].isDirectory()) {
424 moveDirectory(input[i], output);
425 } else {
426 // Copy the file
427 try {
428 output.getParentFile().mkdirs();
429 FileInputStream in = new FileInputStream(input[i]);
430 FileOutputStream out = new FileOutputStream(output);
431 int value = 0;
432 while((value = in.read()) != -1) {
433 out.write(value);
434 }
435 in.close();
436 out.close();
437 // Delete file
438 input[i].delete();
439 } catch (Exception e) {
440 System.err.println("GSFile.moveDirectory: exception: "+e.getMessage());
441 return false;
442 }
443
444 }
445 }
446 return true;
447 }
448
449}
Note: See TracBrowser for help on using the repository browser.