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

Last change on this file since 24395 was 24248, checked in by sjb48, 13 years ago

Oran code for supporting format changes to document.

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