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

Last change on this file since 31230 was 31230, checked in by ak19, 7 years ago

Commit for GS3 server side part of OAI deletion police implementation. Still to implement the GS2 server side part. The earlier commits implemented the PERL side, the oai-inf db implementation. I think I've now got the GS3 server side working, but have yet to try validating against the OAI validator. (I need to test that on a machine that is publicly accessible).

  • Property svn:keywords set to Author Date Id Revision
File size: 19.3 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: 31230 $
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 oai-inf database file in the collection's etc folder */
376 static public String OAIInfoDatabaseFile(String site_home, String collection_name, String db_tailname, 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 + "etc" + File.separatorChar + db_tailname + db_ext; // db tailname should be oai-inf
385 }
386
387
388 /** the archives database file - */
389 static public String archivesDatabaseFile(String site_home, String collection_name, String database_type)
390 {
391 String db_ext = DBHelper.getDBExtFromDBType(database_type);
392 if (null == db_ext || db_ext.equals("")) {
393 logger.warn("Could not recognise database type \"" + database_type + "\", defaulting to GDBM and extension \".gdb\"");
394 // assume gdbm
395 db_ext = ".gdb";
396 }
397 return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "archives" + File.separatorChar + "archiveinf-doc" + db_ext;
398 }
399 // some file utility methods
400
401 /**
402 * read in a file and encode it using base64 encoded data returned as a
403 * String
404 */
405 static public String base64EncodeFromFile(String in_filename)
406 {
407 byte[] data = null;
408 try
409 {
410 data = readFile(in_filename);
411 }
412 catch (Exception e)
413 {
414 logger.error("couldn't read the file");
415 }
416 String encodedString = Base64.encode(data);
417 return encodedString;
418
419 }
420
421 /** decode some base64 data, and write it to the specified file */
422 static public boolean base64DecodeToFile(String data, String out_filename)
423 {
424 try
425 {
426 byte[] buffer = Base64.decode(data);
427 writeFile(buffer, out_filename);
428
429 }
430 catch (Exception e)
431 {
432 logger.error("file opening/closing errors" + e.getMessage());
433 return false;
434 }
435 return true;
436
437 }
438
439 /** read in a file to a byte array */
440 public static byte[] readFile(String filename) throws IOException
441 {
442 File file = new File(filename);
443 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
444 int bytes = (int) file.length();
445 byte[] buffer = new byte[bytes];
446 int readBytes = bis.read(buffer);
447 bis.close();
448 return buffer;
449 }
450
451 /** write a byte array to a file */
452 public static void writeFile(byte[] buffer, String filename) throws IOException
453 {
454 File file = new File(filename);
455 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
456 bos.write(buffer);
457 bos.close();
458 }
459
460 public static boolean deleteFile(File f)
461 {
462
463 if (f.isDirectory())
464 {
465 File[] files = f.listFiles();
466 for (int i = 0; files != null && i < files.length; i++)
467 {
468 deleteFile(files[i]);
469 }
470
471 }
472 // delete the file or directory
473 return f.delete();
474 }
475
476 public static boolean copyFile(File source, File destination)
477 {
478 if (!source.isFile())
479 {
480 logger.error(source.getPath() + " is not a file!");
481 return false;
482 }
483 try
484 {
485 destination.getParentFile().mkdirs();
486 FileInputStream in = new FileInputStream(source);
487 FileOutputStream out = new FileOutputStream(destination);
488 int value = 0;
489 while ((value = in.read()) != -1)
490 {
491 out.write(value);
492 }
493 in.close();
494 out.close();
495 }
496 catch (Exception e)
497 {
498 logger.error("something went wrong copying " + source.getPath() + " to " + destination.getPath());
499 logger.error("Exception: " + e.getMessage());
500 return false;
501 }
502
503 return true;
504 }
505
506 /**
507 * Recursively moves the contents of source file to destination, maintaining
508 * paths.
509 *
510 * @param source
511 * A File representing the directory whose contents you wish to
512 * move.
513 * @param destination
514 * A File representing the directory you wish to move files to.
515 * @return true if successful
516 */
517 public static boolean moveDirectory(File source, File destination)
518 {
519
520 // first try rename
521 if (source.renameTo(destination))
522 {
523 return true;
524 }
525
526 // john T. said that this sometimes doesn't work, so you have to copy files manually
527
528 // copied from gatherer Utility class
529 File input[] = source.listFiles();
530 for (int i = 0; i < input.length; i++)
531 {
532 File output = new File(destination, input[i].getName());
533 if (input[i].isDirectory())
534 {
535 moveDirectory(input[i], output);
536 }
537 else
538 {
539 // Copy the file
540 try
541 {
542 output.getParentFile().mkdirs();
543 FileInputStream in = new FileInputStream(input[i]);
544 FileOutputStream out = new FileOutputStream(output);
545
546 FileChannel inC = in.getChannel();
547 FileChannel outC = out.getChannel();
548
549 System.err.println(inC.transferTo(0, inC.size(), outC));
550
551 in.close();
552 out.close();
553
554 // Delete file
555 input[i].delete();
556 }
557 catch (Exception e)
558 {
559 logger.error("exception: " + e.getMessage());
560 return false;
561 }
562
563 }
564 }
565 return true;
566 }
567
568 public static ArrayList<File> getAllXSLFiles(String siteName)
569 {
570 ArrayList<File> filesToReturn = new ArrayList<File>();
571
572 String siteHome = GSFile.siteHome(GlobalProperties.getGSDL3Home(), siteName);
573
574 //Add XSL files from the site transform directory
575 File siteTransformDir = new File(siteHome + File.separator + "transform");
576 if (siteTransformDir.exists() && siteTransformDir.isDirectory())
577 {
578 filesToReturn.addAll(getXSLFilesFromDirectoryRecursive(siteTransformDir));
579 }
580
581 //Add XSL files from collection transform directories
582 File siteCollectionDir = new File(siteHome + File.separator + "collect");
583 if (siteCollectionDir.exists() && siteCollectionDir.isDirectory())
584 {
585 File[] collections = siteCollectionDir.listFiles();
586
587 for (File collection : collections)
588 {
589 if (collection.isDirectory())
590 {
591 File collectionTranformDir = new File(collection.getAbsolutePath() + File.separator + "transform");
592 if (collectionTranformDir.exists() && collectionTranformDir.isDirectory())
593 {
594 filesToReturn.addAll(getXSLFilesFromDirectoryRecursive(collectionTranformDir));
595 }
596 }
597 }
598 }
599
600 //Add XSL files from the interface transform directory
601 File interfaceDir = new File(GlobalProperties.getGSDL3Home() + File.separator + "interfaces");
602 if (interfaceDir.exists() && interfaceDir.isDirectory())
603 {
604 filesToReturn.addAll(getXSLFilesFromDirectoryRecursive(interfaceDir));
605 }
606
607 return filesToReturn;
608 }
609
610 protected static ArrayList<File> getXSLFilesFromDirectoryRecursive(File directory)
611 {
612 ArrayList<File> filesToReturn = new ArrayList<File>();
613
614 if (!directory.isDirectory())
615 {
616 return filesToReturn;
617 }
618
619 File[] currentFiles = directory.listFiles();
620 for (File current : currentFiles)
621 {
622 if (current.isDirectory())
623 {
624 filesToReturn.addAll(GSFile.getXSLFilesFromDirectoryRecursive(current));
625 }
626 else if (current.getName().endsWith(".xsl"))
627 {
628 filesToReturn.add(current);
629 }
630 }
631
632 return filesToReturn;
633 }
634}
Note: See TracBrowser for help on using the repository browser.