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

Last change on this file since 30833 was 30833, checked in by kjdon, 8 years ago

added siteResourcesDir method

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