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

Last change on this file since 33627 was 33627, checked in by kjdon, 4 years ago

removed unnecessary comments

  • Property svn:keywords set to Author Date Id Revision
File size: 19.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 */
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 collectionResourceDir(String collection_home)
206 {
207 return collection_home + File.separatorChar + "resources";
208 }
209
210 static public String siteResourceDir(String site_home) {
211 return site_home+File.separatorChar+"resources";
212 }
213 /** absolute path for an associated file */
214 static public String assocFileAbsolutePath(String site_home, String collection_name, String assoc_file_path, String filename)
215 {
216 return collectionBaseDir(site_home, collection_name) + File.separatorChar + "index" + File.separatorChar + "assoc" + File.separatorChar + assoc_file_path + File.separatorChar + filename;
217 }
218
219 static public String extHome(String gsdl3_home, String ext_name)
220 {
221 return gsdl3_home + File.separatorChar + "ext" + File.separatorChar + ext_name;
222 }
223
224 static public String siteHome(String gsdl3_home, String site_name)
225 {
226 return gsdl3_home + File.separatorChar + "sites" + File.separatorChar + site_name;
227 }
228
229 static public String interfaceHome(String gsdl3_home, String interface_name)
230 {
231 return gsdl3_home + File.separatorChar + "interfaces" + File.separatorChar + interface_name;
232 }
233
234 static public String interfaceStylesheetFile(String gsdl3_home, String interface_name, String filename)
235 {
236 return gsdl3_home + File.separatorChar + "interfaces" + File.separatorChar + interface_name + File.separatorChar + "transform" + File.separatorChar + filename;
237 }
238
239 static public String siteStylesheetFile(String site_home, String filename)
240 {
241 return site_home + File.separatorChar + "transform" + File.separatorChar + filename;
242 }
243
244 static public String collStylesheetFile(String site_home, String coll_name, String filename)
245 {
246 return collectionBaseDir(site_home, coll_name) + File.separatorChar + "transform" + File.separatorChar + filename;
247 }
248
249 /**
250 * returns the absolute path to a stylesheet. Stylesheets are looked for in
251 * the following places, in the following order: current collection, current
252 * site, current interface, base interfaces returns null if the file cannot
253 * be found
254 *
255 * this is not so good because sites may be on a different computer
256 */
257 static public String stylesheetFile(String gsdl3_home, String site_name, String collection, String interface_name, ArrayList<String> base_interfaces, String filename)
258 {
259
260 String site_home = siteHome(gsdl3_home, site_name);
261 // try collection first
262 File stylesheet = null;
263 if (!collection.equals(""))
264 {
265
266 String coll_home = collectionBaseDir(site_home, collection);
267 stylesheet = new File(coll_home + File.separatorChar + "transform" + File.separatorChar + filename);
268 if (stylesheet.exists())
269 {
270 return stylesheet.getPath();
271 }
272 }
273
274 // try site one next
275 stylesheet = new File(site_home + File.separatorChar + "transform" + File.separatorChar + filename);
276 if (stylesheet.exists())
277 {
278 return stylesheet.getPath();
279 }
280
281 // try current interface
282 String interface_home = interfaceHome(gsdl3_home, interface_name);
283 stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
284 if (stylesheet.exists())
285 {
286 return stylesheet.getPath();
287 }
288 // try base interface
289 if (base_interfaces == null || base_interfaces.size() == 0)
290 {
291 return null; // no base interfaces to look for
292 }
293 for (int i = 0; i < base_interfaces.size(); i++)
294 {
295 interface_home = interfaceHome(gsdl3_home, base_interfaces.get(i));
296 stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
297 if (stylesheet.exists())
298 {
299 return stylesheet.getPath();
300 }
301 }
302
303 // still can't find it and we have looked everywhere
304 return null;
305 }
306
307 static public ArrayList<File> getStylesheetFiles(String gsdl3_home, String site_name, String collection, String interface_name, ArrayList<String> base_interfaces, String filename)
308 {
309 ArrayList<File> stylesheets = new ArrayList<File>();
310 String site_home = siteHome(gsdl3_home, site_name);
311 // try collection first
312 File stylesheet = null;
313 if (!collection.equals(""))
314 {
315 String coll_home = collectionBaseDir(site_home, collection);
316 stylesheet = new File(coll_home + File.separatorChar + "transform" + File.separatorChar + filename);
317 if (stylesheet.exists())
318 {
319 stylesheets.add(stylesheet);
320 }
321 }
322
323 // try site one next
324 stylesheet = new File(site_home + File.separatorChar + "transform" + File.separatorChar + filename);
325 if (stylesheet.exists())
326 {
327 stylesheets.add(stylesheet);
328 }
329
330 // try current interface
331 String interface_home = interfaceHome(gsdl3_home, interface_name);
332 stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
333 if (stylesheet.exists())
334 {
335 stylesheets.add(stylesheet);
336 }
337 // try base interface
338 if (base_interfaces != null && base_interfaces.size() != 0)
339 {
340 for (int i = 0; i < base_interfaces.size(); i++)
341 {
342 interface_home = interfaceHome(gsdl3_home, base_interfaces.get(i));
343 stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
344 if (stylesheet.exists())
345 {
346 stylesheets.add(stylesheet);
347 }
348 }
349 }
350
351 return stylesheets;
352 }
353
354 /** base directory for phind data */
355 public static String phindBaseDir(String site_home, String coll_name, String phind_index)
356 {
357 return site_home + File.separatorChar + "collect" + File.separatorChar + coll_name + File.separatorChar + "index" + File.separatorChar + "phind" + phind_index;
358 }
359
360 /** the collection database file - */
361 static public String collectionDatabaseFile(String site_home, String collection_name, String index_stem, String database_type)
362 {
363 String db_ext = DBHelper.getDBExtFromDBType(database_type);
364 if (null == db_ext || db_ext.equals("")) {
365 logger.warn("Could not recognise database type \"" + database_type + "\", defaulting to GDBM and extension \".gdb\"");
366 // assume gdbm
367 db_ext = ".gdb";
368 }
369 return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "index" + File.separatorChar + "text" + File.separatorChar + index_stem + db_ext;
370 }
371
372 /** the oai-inf database file in the collection's etc folder */
373 static public String OAIInfoDatabaseFile(String site_home, String collection_name, String db_tailname, String database_type)
374 {
375 String db_ext = DBHelper.getDBExtFromDBType(database_type);
376 if (null == db_ext || db_ext.equals("")) {
377 logger.warn("Could not recognise database type \"" + database_type + "\", defaulting to GDBM and extension \".gdb\"");
378 // assume gdbm
379 db_ext = ".gdb";
380 }
381 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
382 }
383
384
385 /** the archives database file - */
386 static public String archivesDatabaseFile(String site_home, String collection_name, String database_type)
387 {
388 String db_ext = DBHelper.getDBExtFromDBType(database_type);
389 if (null == db_ext || db_ext.equals("")) {
390 logger.warn("Could not recognise database type \"" + database_type + "\", defaulting to GDBM and extension \".gdb\"");
391 // assume gdbm
392 db_ext = ".gdb";
393 }
394 return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "archives" + File.separatorChar + "archiveinf-doc" + db_ext;
395 }
396 // some file utility methods
397
398 /**
399 * read in a file and encode it using base64 encoded data returned as a
400 * String
401 */
402 static public String base64EncodeFromFile(String in_filename)
403 {
404 byte[] data = null;
405 try
406 {
407 data = readFile(in_filename);
408 }
409 catch (Exception e)
410 {
411 logger.error("couldn't read the file");
412 }
413 String encodedString = Base64.encode(data);
414 return encodedString;
415
416 }
417
418 /** decode some base64 data, and write it to the specified file */
419 static public boolean base64DecodeToFile(String data, String out_filename)
420 {
421 try
422 {
423 byte[] buffer = Base64.decode(data);
424 writeFile(buffer, out_filename);
425
426 }
427 catch (Exception e)
428 {
429 logger.error("file opening/closing errors" + e.getMessage());
430 return false;
431 }
432 return true;
433
434 }
435
436 /** read in a file to a byte array */
437 public static byte[] readFile(String filename) throws IOException
438 {
439 File file = new File(filename);
440 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
441 int bytes = (int) file.length();
442 byte[] buffer = new byte[bytes];
443 int readBytes = bis.read(buffer);
444 bis.close();
445 return buffer;
446 }
447
448 /** write a byte array to a file */
449 public static void writeFile(byte[] buffer, String filename) throws IOException
450 {
451 File file = new File(filename);
452 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
453 bos.write(buffer);
454 bos.close();
455 }
456
457 public static boolean deleteFile(File f)
458 {
459
460 if (f.isDirectory())
461 {
462 File[] files = f.listFiles();
463 for (int i = 0; files != null && i < files.length; i++)
464 {
465 deleteFile(files[i]);
466 }
467
468 }
469 // delete the file or directory
470 return f.delete();
471 }
472
473 public static boolean copyFile(File source, File destination)
474 {
475 if (!source.isFile())
476 {
477 logger.error(source.getPath() + " is not a file!");
478 return false;
479 }
480 try
481 {
482 destination.getParentFile().mkdirs();
483 FileInputStream in = new FileInputStream(source);
484 FileOutputStream out = new FileOutputStream(destination);
485 int value = 0;
486 while ((value = in.read()) != -1)
487 {
488 out.write(value);
489 }
490 in.close();
491 out.close();
492 }
493 catch (Exception e)
494 {
495 logger.error("something went wrong copying " + source.getPath() + " to " + destination.getPath());
496 logger.error("Exception: " + e.getMessage());
497 return false;
498 }
499
500 return true;
501 }
502
503 /**
504 * Recursively moves the contents of source file to destination, maintaining
505 * paths.
506 *
507 * @param source
508 * A File representing the directory whose contents you wish to
509 * move.
510 * @param destination
511 * A File representing the directory you wish to move files to.
512 * @return true if successful
513 */
514 public static boolean moveDirectory(File source, File destination)
515 {
516
517 // first try rename
518 if (source.renameTo(destination))
519 {
520 return true;
521 }
522
523 // john T. said that this sometimes doesn't work, so you have to copy files manually
524
525 // copied from gatherer Utility class
526 File input[] = source.listFiles();
527 for (int i = 0; i < input.length; i++)
528 {
529 File output = new File(destination, input[i].getName());
530 if (input[i].isDirectory())
531 {
532 moveDirectory(input[i], output);
533 }
534 else
535 {
536 // Copy the file
537 try
538 {
539 output.getParentFile().mkdirs();
540 FileInputStream in = new FileInputStream(input[i]);
541 FileOutputStream out = new FileOutputStream(output);
542
543 FileChannel inC = in.getChannel();
544 FileChannel outC = out.getChannel();
545
546 System.err.println(inC.transferTo(0, inC.size(), outC));
547
548 in.close();
549 out.close();
550
551 // Delete file
552 input[i].delete();
553 }
554 catch (Exception e)
555 {
556 logger.error("exception: " + e.getMessage());
557 return false;
558 }
559
560 }
561 }
562 return true;
563 }
564
565 public static ArrayList<File> getAllXSLFiles(String siteName)
566 {
567 ArrayList<File> filesToReturn = new ArrayList<File>();
568
569 String siteHome = GSFile.siteHome(GlobalProperties.getGSDL3Home(), siteName);
570
571 //Add XSL files from the site transform directory
572 File siteTransformDir = new File(siteHome + File.separator + "transform");
573 if (siteTransformDir.exists() && siteTransformDir.isDirectory())
574 {
575 filesToReturn.addAll(getXSLFilesFromDirectoryRecursive(siteTransformDir));
576 }
577
578 //Add XSL files from collection transform directories
579 File siteCollectionDir = new File(siteHome + File.separator + "collect");
580 if (siteCollectionDir.exists() && siteCollectionDir.isDirectory())
581 {
582 File[] collections = siteCollectionDir.listFiles();
583
584 for (File collection : collections)
585 {
586 if (collection.isDirectory())
587 {
588 File collectionTranformDir = new File(collection.getAbsolutePath() + File.separator + "transform");
589 if (collectionTranformDir.exists() && collectionTranformDir.isDirectory())
590 {
591 filesToReturn.addAll(getXSLFilesFromDirectoryRecursive(collectionTranformDir));
592 }
593 }
594 }
595 }
596
597 //Add XSL files from the interface transform directory
598 File interfaceDir = new File(GlobalProperties.getGSDL3Home() + File.separator + "interfaces");
599 if (interfaceDir.exists() && interfaceDir.isDirectory())
600 {
601 filesToReturn.addAll(getXSLFilesFromDirectoryRecursive(interfaceDir));
602 }
603
604 return filesToReturn;
605 }
606
607 protected static ArrayList<File> getXSLFilesFromDirectoryRecursive(File directory)
608 {
609 ArrayList<File> filesToReturn = new ArrayList<File>();
610
611 if (!directory.isDirectory())
612 {
613 return filesToReturn;
614 }
615
616 File[] currentFiles = directory.listFiles();
617 for (File current : currentFiles)
618 {
619 if (current.isDirectory())
620 {
621 filesToReturn.addAll(GSFile.getXSLFilesFromDirectoryRecursive(current));
622 }
623 else if (current.getName().endsWith(".xsl"))
624 {
625 filesToReturn.add(current);
626 }
627 }
628
629 return filesToReturn;
630 }
631}
Note: See TracBrowser for help on using the repository browser.