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

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

Adding constants for the various XML namespaces used in the code

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