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

Last change on this file since 30262 was 30262, checked in by jmt12, 9 years ago

Altered the collectionDatabaseFile function to no longer use hardcoded file extensions, but instead to look them up via the registered database types in the DBHelper.

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