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

Last change on this file since 30516 was 30516, checked in by Georgiy Litvinov, 8 years ago

Moved to incremental rebuild in webeditor

  • Property svn:keywords set to Author Date Id Revision
File size: 18.2 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: 30516 $
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 /** the archives database file - */
359 static public String archivesDatabaseFile(String site_home, String collection_name, String database_type)
360 {
361 String db_ext = DBHelper.getDBExtFromDBType(database_type);
362 if (null == db_ext || db_ext.equals("")) {
363 logger.warn("Could not recognise database type \"" + database_type + "\", defaulting to GDBM and extension \".gdb\"");
364 // assume gdbm
365 db_ext = ".gdb";
366 }
367 return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "archives" + File.separatorChar + "archiveinf-doc" + db_ext;
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 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 interfaceDir = new File(GlobalProperties.getGSDL3Home() + File.separator + "interfaces");
572 if (interfaceDir.exists() && interfaceDir.isDirectory())
573 {
574 filesToReturn.addAll(getXSLFilesFromDirectoryRecursive(interfaceDir));
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.