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

Last change on this file since 25635 was 25635, checked in by sjm84, 12 years ago

Fixing Greenstone 3's use (or lack thereof) of generics, this was done automatically so we may want to change it over time. This change will also auto-format any files that have not already been formatted.

  • Property svn:keywords set to Author Date Id Revision
File size: 15.6 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.nio.channels.FileChannel;
33import java.util.ArrayList;
34
35import org.apache.log4j.*;
36
37/**
38 * GSFile - utility class for Greenstone.
39 *
40 * all file paths are created here also has file utility methods
41 *
42 * @author <a href="mailto:[email protected]">Katherine Don</a>
43 * @version $Revision: 25635 $
44 * @see File
45 */
46
47public class GSFile
48{
49
50 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSFile.class.getName());
51
52 /** site config file path */
53 static public String siteConfigFile(String site_home)
54 {
55 return site_home + File.separatorChar + "siteConfig.xml";
56
57 }
58
59 /** interface config file path */
60 static public String interfaceConfigFile(String interface_home)
61 {
62 return interface_home + File.separatorChar + "interfaceConfig.xml";
63
64 }
65
66 /** collection directory path */
67 static public String collectDir(String site_home)
68 {
69 return site_home + File.separatorChar + "collect";
70 }
71
72 /** collection config file path */
73 static public String collectionConfigFile(String site_home, String collection_name)
74 {
75 return collectionConfigFile(collectionBaseDir(site_home, collection_name));
76 }
77
78 static public String collectionConfigFile(String collection_home)
79 {
80 return collectionEtcDir(collection_home) + File.separatorChar + "collectionConfig.xml";
81
82 }
83
84 /** collection init file path */
85 static public String collectionInitFile(String site_home, String collection_name)
86 {
87 return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "etc" + File.separatorChar + "collectionInit.xml";
88
89 }
90
91 /** collection build config file path */
92 static public String collectionBuildConfigFile(String site_home, String collection_name)
93 {
94 return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "index" + File.separatorChar + "buildConfig.xml";
95 }
96
97 /** collection build config file path */
98 static public String collectionBuildConfigFileBuilding(String site_home, String collection_name)
99 {
100 return collectionBuildConfigFileBuilding(collectionBaseDir(site_home, collection_name));
101 }
102
103 static public String collectionBuildConfigFileBuilding(String collection_home)
104 {
105 return collection_home + File.separatorChar + "building" + File.separatorChar + "buildConfig.xml";
106 }
107
108 /** XML Transform directory path */
109 static public String xmlTransformDir(String interface_home)
110 {
111 return interface_home + File.separatorChar + "transform";
112 }
113
114 /** collection base directory path */
115 static public String collectionBaseDir(String site_home, String collection_name)
116 {
117 return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name;
118 }
119
120 /** collection archive directory path */
121 static public String collectionArchiveDir(String site_home, String collection_name)
122 {
123 return collectionArchiveDir(collectionBaseDir(site_home, collection_name));
124 }
125
126 static public String collectionArchiveDir(String collection_home)
127 {
128 return collection_home + File.separatorChar + "archives";
129 }
130
131 /** collection building directory path */
132 static public String collectionBuildDir(String site_home, String collection_name)
133 {
134 return collectionBuildDir(collectionBaseDir(site_home, collection_name));
135 }
136
137 static public String collectionBuildDir(String collection_home)
138 {
139 return collection_home + File.separator + "building";
140 }
141
142 /** collection building directory path */
143 static public String collectionEtcDir(String site_home, String collection_name)
144 {
145 return collectionEtcDir(collectionBaseDir(site_home, collection_name));
146 }
147
148 static public String collectionEtcDir(String collection_home)
149 {
150 return collection_home + File.separator + "etc";
151 }
152
153 /** collection building directory path */
154 static public String collectionImportDir(String site_home, String collection_name)
155 {
156 return collectionImportDir(collectionBaseDir(site_home, collection_name));
157
158 }
159
160 static public String collectionImportDir(String collection_home)
161 {
162 return collection_home + File.separatorChar + "import";
163 }
164
165 /** collection building directory path */
166 static public String collectionIndexDir(String site_home, String collection_name)
167 {
168 return collectionIndexDir(collectionBaseDir(site_home, collection_name));
169
170 }
171
172 static public String collectionIndexDir(String collection_home)
173 {
174 return collection_home + File.separatorChar + "index";
175 }
176
177 /** text path (for doc retrieval) relative to collectionBaseDir */
178 static public String collectionTextPath(String index_stem)
179 {
180 return "index" + File.separatorChar + "text" + File.separatorChar + index_stem;
181 }
182
183 /** index path (for querying) relative to collectionBaseDir */
184 static public String collectionIndexPath(String index_stem, String index_name)
185 {
186 return "index" + File.separatorChar + index_name + File.separatorChar + index_stem;
187 }
188
189 /** collection resources directory path */
190 static public String collectionResourceDir(String site_home, String collection_name)
191 {
192 return collectionResourceDir(collectionBaseDir(site_home, collection_name));
193
194 }
195
196 static public String collectionResourceDir(String collection_home)
197 {
198 return collection_home + File.separatorChar + "resources";
199 }
200
201 /** absolute path for an associated file */
202 static public String assocFileAbsolutePath(String site_home, String collection_name, String assoc_file_path, String filename)
203 {
204 return collectionBaseDir(site_home, collection_name) + File.separatorChar + "index" + File.separatorChar + "assoc" + File.separatorChar + assoc_file_path + File.separatorChar + filename;
205 }
206
207 static public String extHome(String gsdl3_home, String ext_name)
208 {
209 return gsdl3_home + File.separatorChar + "ext" + File.separatorChar + ext_name;
210 }
211
212 static public String siteHome(String gsdl3_home, String site_name)
213 {
214 return gsdl3_home + File.separatorChar + "sites" + File.separatorChar + site_name;
215 }
216
217 static public String interfaceHome(String gsdl3_home, String interface_name)
218 {
219 return gsdl3_home + File.separatorChar + "interfaces" + File.separatorChar + interface_name;
220 }
221
222 static public String interfaceStylesheetFile(String gsdl3_home, String interface_name, String filename)
223 {
224 return gsdl3_home + File.separatorChar + "interfaces" + File.separatorChar + interface_name + File.separatorChar + "transform" + File.separatorChar + filename;
225 }
226
227 static public String siteStylesheetFile(String site_home, String filename)
228 {
229 return site_home + File.separatorChar + "transform" + File.separatorChar + filename;
230 }
231
232 static public String collStylesheetFile(String site_home, String coll_name, String filename)
233 {
234 return collectionBaseDir(site_home, coll_name) + File.separatorChar + "transform" + File.separatorChar + filename;
235 }
236
237 /**
238 * returns the absolute path to a stylesheet stylesheets are looked for in
239 * the following places, in the following order: current collection, current
240 * site, current interface, base interfaces returns null if the file cannot
241 * be found
242 *
243 * this is not so good because sites may be on a different computer
244 */
245 static public String stylesheetFile(String gsdl3_home, String site_name, String collection, String interface_name, ArrayList<String> base_interfaces, String filename)
246 {
247
248 String site_home = siteHome(gsdl3_home, site_name);
249 // try collection first
250 File stylesheet = null;
251 if (!collection.equals(""))
252 {
253
254 String coll_home = collectionBaseDir(site_home, collection);
255 stylesheet = new File(coll_home + File.separatorChar + "transform" + File.separatorChar + filename);
256 if (stylesheet.exists())
257 {
258 return stylesheet.getPath();
259 }
260 }
261
262 // try site one next
263 stylesheet = new File(site_home + File.separatorChar + "transform" + File.separatorChar + filename);
264 if (stylesheet.exists())
265 {
266 return stylesheet.getPath();
267 }
268
269 // try current interface
270 String interface_home = interfaceHome(gsdl3_home, interface_name);
271 stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
272 if (stylesheet.exists())
273 {
274 return stylesheet.getPath();
275 }
276 // try base interface
277 if (base_interfaces == null || base_interfaces.size() == 0)
278 {
279 return null; // no base interfaces to look for
280 }
281 for (int i = 0; i < base_interfaces.size(); i++)
282 {
283 interface_home = interfaceHome(gsdl3_home, base_interfaces.get(i));
284 stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
285 if (stylesheet.exists())
286 {
287 return stylesheet.getPath();
288 }
289 }
290
291 // still can't find it and we have looked everywhere
292 return null;
293 }
294
295 static public ArrayList<File> getStylesheetFiles(String gsdl3_home, String site_name, String collection, String interface_name, ArrayList<String> base_interfaces, String filename)
296 {
297 ArrayList<File> stylesheets = new ArrayList<File>();
298 String site_home = siteHome(gsdl3_home, site_name);
299 // try collection first
300 File stylesheet = null;
301 if (!collection.equals(""))
302 {
303 String coll_home = collectionBaseDir(site_home, collection);
304 stylesheet = new File(coll_home + File.separatorChar + "transform" + File.separatorChar + filename);
305 if (stylesheet.exists())
306 {
307 stylesheets.add(stylesheet);
308 }
309 }
310
311 // try site one next
312 stylesheet = new File(site_home + File.separatorChar + "transform" + File.separatorChar + filename);
313 if (stylesheet.exists())
314 {
315 stylesheets.add(stylesheet);
316 }
317
318 // try current interface
319 String interface_home = interfaceHome(gsdl3_home, interface_name);
320 stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
321 if (stylesheet.exists())
322 {
323 stylesheets.add(stylesheet);
324 }
325 // try base interface
326 if (base_interfaces != null && base_interfaces.size() != 0)
327 {
328 for (int i = 0; i < base_interfaces.size(); i++)
329 {
330 interface_home = interfaceHome(gsdl3_home, base_interfaces.get(i));
331 stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
332 if (stylesheet.exists())
333 {
334 stylesheets.add(stylesheet);
335 }
336 }
337 }
338
339 return stylesheets;
340 }
341
342 /** base directory for phind data */
343 public static String phindBaseDir(String site_home, String coll_name, String phind_index)
344 {
345 return site_home + File.separatorChar + "collect" + File.separatorChar + coll_name + File.separatorChar + "index" + File.separatorChar + "phind" + phind_index;
346 }
347
348 /** the collection database file - */
349 static public String collectionDatabaseFile(String site_home, String collection_name, String index_stem, String database_type)
350 {
351
352 String db_ext = null;
353 if (database_type.equalsIgnoreCase("jdbm"))
354 {
355 db_ext = ".jdb";
356 }
357 else
358 {
359 // assume gdbm
360 db_ext = ".gdb";
361 }
362
363 return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "index" + File.separatorChar + "text" + File.separatorChar + index_stem + db_ext;
364
365 }
366
367 // some file utility methods
368
369 /**
370 * read in a file and encode it using base64 encoded data returned as a
371 * String
372 */
373 static public String base64EncodeFromFile(String in_filename)
374 {
375 byte[] data = null;
376 try
377 {
378 data = readFile(in_filename);
379 }
380 catch (Exception e)
381 {
382 logger.error("couldn't read the file");
383 }
384 String encodedString = Base64.encode(data);
385 return encodedString;
386
387 }
388
389 /** decode some base64 data, and write it to the specified file */
390 static public boolean base64DecodeToFile(String data, String out_filename)
391 {
392 try
393 {
394 byte[] buffer = Base64.decode(data);
395 writeFile(buffer, out_filename);
396
397 }
398 catch (Exception e)
399 {
400 logger.error("file opening/closing errors" + e.getMessage());
401 return false;
402 }
403 return true;
404
405 }
406
407 /** read in a file to a byte array */
408 public static byte[] readFile(String filename) throws IOException
409 {
410 File file = new File(filename);
411 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
412 int bytes = (int) file.length();
413 byte[] buffer = new byte[bytes];
414 int readBytes = bis.read(buffer);
415 bis.close();
416 return buffer;
417 }
418
419 /** write a byte array to a file */
420 public static void writeFile(byte[] buffer, String filename) throws IOException
421 {
422 File file = new File(filename);
423 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
424 bos.write(buffer);
425 bos.close();
426 }
427
428 public static boolean deleteFile(File f)
429 {
430
431 if (f.isDirectory())
432 {
433 File[] files = f.listFiles();
434 for (int i = 0; files != null && i < files.length; i++)
435 {
436 deleteFile(files[i]);
437 }
438
439 }
440 // delete the file or directory
441 return f.delete();
442 }
443
444 public static boolean copyFile(File source, File destination)
445 {
446 if (!source.isFile())
447 {
448 logger.error(source.getPath() + " is not a file!");
449 return false;
450 }
451 try
452 {
453 destination.getParentFile().mkdirs();
454 FileInputStream in = new FileInputStream(source);
455 FileOutputStream out = new FileOutputStream(destination);
456 int value = 0;
457 while ((value = in.read()) != -1)
458 {
459 out.write(value);
460 }
461 in.close();
462 out.close();
463 }
464 catch (Exception e)
465 {
466 logger.error("something went wrong copying " + source.getPath() + " to " + destination.getPath());
467 logger.error("Exception: " + e.getMessage());
468 return false;
469 }
470
471 return true;
472 }
473
474 /**
475 * Recursively moves the contents of source file to destination, maintaining
476 * paths.
477 *
478 * @param source
479 * A File representing the directory whose contents you wish to
480 * move.
481 * @param destination
482 * A File representing the directory you wish to move files to.
483 * @return true if successful
484 */
485 public static boolean moveDirectory(File source, File destination)
486 {
487
488 // first try rename
489 if (source.renameTo(destination))
490 {
491 return true;
492 }
493
494 // john T. said that this sometimes doesn't work, so you have to copy files manually
495
496 // copied from gatherer Utility class
497 File input[] = source.listFiles();
498 for (int i = 0; i < input.length; i++)
499 {
500 File output = new File(destination, input[i].getName());
501 if (input[i].isDirectory())
502 {
503 moveDirectory(input[i], output);
504 }
505 else
506 {
507 // Copy the file
508 try
509 {
510 output.getParentFile().mkdirs();
511 FileInputStream in = new FileInputStream(input[i]);
512 FileOutputStream out = new FileOutputStream(output);
513
514 FileChannel inC = in.getChannel();
515 FileChannel outC = out.getChannel();
516
517 System.err.println(inC.transferTo(0, inC.size(), outC));
518
519 in.close();
520 out.close();
521
522 // Delete file
523 input[i].delete();
524 }
525 catch (Exception e)
526 {
527 logger.error("exception: " + e.getMessage());
528 return false;
529 }
530
531 }
532 }
533 return true;
534 }
535
536}
Note: See TracBrowser for help on using the repository browser.