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

Last change on this file since 37597 was 37597, checked in by davidb, 13 months ago

Added in check to avoid reading in emacs auto-save buffer files (given partial state of editing, potentially invalid syntax anyway)

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