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

Last change on this file since 24609 was 24509, checked in by sjm84, 13 years ago

Adding in the DocumentBasket functionality and also the new cascading way of handling xsl files which will look in the collect/site/interface/base interface places in that order

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