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

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

Added collection groups. Config file in web/sites/localsite/groupConfig.xml
PageAction now send request to CollectionGroups service to retrieve information about collections and groups.

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