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

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

Also check the site directory to see if there is any XSL files at this level

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