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

Last change on this file since 17713 was 17713, checked in by ak19, 15 years ago

No longer defaults the db extension for all Macs to bdb.

  • Property svn:keywords set to Author Date Id Revision
File size: 14.8 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
34import org.apache.log4j.*;
35
36/**
37 * GSFile - utility class for Greenstone.
38 *
39 * all file paths are created here
40 * also has file utility methods
41 *
42 * @author <a href="mailto:[email protected]">Katherine Don</a>
43 * @version $Revision: 17713 $
44 * @see File
45 */
46
47public class GSFile {
48
49 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSFile.class.getName());
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 + "sites" +
199 File.separatorChar +site_name;
200 }
201
202 static public String interfaceHome(String gsdl3_home,
203 String interface_name) {
204 return gsdl3_home + File.separatorChar + "interfaces" +
205 File.separatorChar + interface_name;
206 }
207
208 static public String siteStylesheetFile(String site_home,
209 String filename) {
210 return site_home+File.separatorChar + "transform" +
211 File.separatorChar + filename;
212 }
213 static public String collStylesheetFile(String site_home,
214 String coll_name,
215 String filename) {
216 return collectionBaseDir(site_home, coll_name) + File.separatorChar+
217 "transform" + File.separatorChar + filename;
218 }
219
220
221 /** returns the absolute path to a stylesheet
222 * stylesheets are looked for in the following places, in the
223 * following order:
224 * current collection, current site, current interface, base interfaces
225 * returns null if the file cannot be found
226 *
227 * this is not so good because sites may be on a different computer */
228 static public String stylesheetFile(String gsdl3_home,
229 String site_name,
230 String collection,
231 String interface_name,
232 ArrayList base_interfaces,
233 String filename) {
234
235 String site_home = siteHome(gsdl3_home, site_name);
236 // try collection first
237 File stylesheet = null;
238 if (!collection.equals("")) {
239
240 String coll_home = collectionBaseDir(site_home, collection);
241 stylesheet = new File(coll_home +File.separatorChar+
242 "transform"+File.separatorChar+filename);
243 if (stylesheet.exists()) {
244 return stylesheet.getPath();
245 }
246 }
247
248 // try site one next
249 stylesheet = new File(site_home +File.separatorChar+
250 "transform"+File.separatorChar+filename);
251 if (stylesheet.exists()) {
252 return stylesheet.getPath();
253 }
254
255 // try current interface
256 String interface_home = interfaceHome(gsdl3_home,
257 interface_name);
258 stylesheet = new File(interface_home+File.separatorChar+
259 "transform"+File.separatorChar+filename);
260 if (stylesheet.exists()) {
261 return stylesheet.getPath();
262 }
263 // try base interface
264 if (base_interfaces==null || base_interfaces.size()==0) {
265 return null; // no base interfaces to look for
266 }
267 for (int i=0; i<base_interfaces.size(); i++) {
268 interface_home = interfaceHome(gsdl3_home, (String)base_interfaces.get(i));
269 stylesheet = new File(interface_home+File.separatorChar+
270 "transform"+File.separatorChar+filename);
271 if (stylesheet.exists()) {
272 return stylesheet.getPath();
273 }
274 }
275
276 // still can't find it and we have looked everywhere
277 return null;
278 }
279
280
281 /** base directory for phind data */
282 public static String phindBaseDir(String site_home, String coll_name,
283 String phind_index) {
284 return site_home + File.separatorChar +
285 "collect" + File.separatorChar +
286 coll_name + File.separatorChar +
287 "index" + File.separatorChar +
288 "phind"+phind_index;
289 }
290
291 /** the collection database file -
292 * note, need to change extension depending on OS and database type */
293 // The perl code can't detect big vs little endian on a Mac, so the Perl
294 // code will always name the gdbm db file .bdb on a Mac whether its little
295 // endian or not. A Mac little endian file is NOT the same as a Linux
296 // little endian file
297 // jdbm files are endian independent ??
298 static public String collectionDatabaseFile(String site_home,
299 String collection_name,
300 String index_stem,
301 String database_type) {
302
303 String db_ext = null;
304 if (database_type.equalsIgnoreCase("jdbm")) {
305 db_ext = ".db";
306 } else {
307 // assume gdbm
308 db_ext = ".ldb";
309
310 // No longer default all Macs to .bdb extension regardless of what endian,
311 // they are since the Misc.isBigEndian() method does return little endian
312 // for rata (Intel Mac) and big endian for shuttle (Power PC).
313 if (Misc.isBigEndian()) { // if (Misc.isMac() || Misc.isBigEndian()) {
314 db_ext = ".bdb";
315 }
316 }
317
318 return site_home + File.separatorChar +
319 "collect" + File.separatorChar +
320 collection_name + File.separatorChar +
321 "index" + File.separatorChar + "text" + File.separatorChar +
322 index_stem + db_ext;
323
324 }
325
326 // some file utility methods
327
328 /** read in a file and encode it using base64
329 * encoded data returned as a String */
330 static public String base64EncodeFromFile(String in_filename) {
331 byte [] data=null;
332 try {
333 data = readFile(in_filename);
334 } catch (Exception e) {
335 logger.error("couldn't read the file");
336 }
337 String encodedString = Base64.encode(data);
338 return encodedString;
339
340 }
341
342 /** decode some base64 data, and write it to the specified file */
343 static public boolean base64DecodeToFile(String data, String out_filename) {
344 try {
345 byte[] buffer=Base64.decode(data);
346 writeFile(buffer, out_filename);
347
348 } catch (Exception e) {
349 logger.error("file opening/closing errors"+e.getMessage());
350 return false;
351 }
352 return true;
353
354 }
355
356 /** read in a file to a byte array */
357 public static byte[] readFile(String filename) throws IOException {
358 File file = new File(filename);
359 BufferedInputStream bis = new BufferedInputStream(new
360 FileInputStream(file));
361 int bytes = (int) file.length();
362 byte[] buffer = new byte[bytes];
363 int readBytes = bis.read(buffer);
364 bis.close();
365 return buffer;
366 }
367
368 /** write a byte array to a file */
369 public static void writeFile(byte [] buffer, String filename) throws IOException {
370 File file = new File(filename);
371 BufferedOutputStream bos = new BufferedOutputStream(new
372 FileOutputStream(file));
373 bos.write(buffer);
374 bos.close();
375 }
376
377 public static boolean deleteFile(File f) {
378
379 if (f.isDirectory()) {
380 File[] files = f.listFiles();
381 for (int i=0; files!=null && i<files.length; i++) {
382 deleteFile(files[i]);
383 }
384
385 }
386 // delete the file or directory
387 return f.delete();
388 }
389
390 public static boolean copyFile(File source, File destination) {
391 if (!source.isFile()) {
392 logger.error(source.getPath()+" is not a file!");
393 return false;
394 }
395 try {
396 destination.getParentFile().mkdirs();
397 FileInputStream in = new FileInputStream(source);
398 FileOutputStream out = new FileOutputStream(destination);
399 int value = 0;
400 while((value = in.read()) != -1) {
401 out.write(value);
402 }
403 in.close();
404 out.close();
405 } catch (Exception e) {
406 logger.error("something went wrong copying "+source.getPath()+" to "+destination.getPath());
407 logger.error("Exception: "+e.getMessage());
408 return false;
409 }
410
411 return true;
412 }
413 /** Recursively moves the contents of source file to destination,
414 * maintaining paths.
415 * @param source A File representing the directory whose contents you
416 * wish to move.
417 * @param destination A File representing the directory you wish to move
418 * files to.
419 * @return true if successful
420 */
421 public static boolean moveDirectory(File source, File destination) {
422
423 // first try rename
424 if (source.renameTo(destination)) {
425 return true;
426 }
427
428 // john T. said that this sometimes doesn't work, so you have to copy files manually
429
430 // copied from gatherer Utility class
431 File input[] = source.listFiles();
432 for(int i = 0; i < input.length; i++) {
433 File output = new File(destination, input[i].getName());
434 if (input[i].isDirectory()) {
435 moveDirectory(input[i], output);
436 } else {
437 // Copy the file
438 try {
439 output.getParentFile().mkdirs();
440 FileInputStream in = new FileInputStream(input[i]);
441 FileOutputStream out = new FileOutputStream(output);
442 int value = 0;
443 while((value = in.read()) != -1) {
444 out.write(value);
445 }
446 in.close();
447 out.close();
448 // Delete file
449 input[i].delete();
450 } catch (Exception e) {
451 logger.error("exception: "+e.getMessage());
452 return false;
453 }
454
455 }
456 }
457 return true;
458 }
459
460}
Note: See TracBrowser for help on using the repository browser.