source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GSFile.java@ 13270

Last change on this file since 13270 was 13270, checked in by shaoqun, 17 years ago

replace Category class which is deprecated with Logger class

  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 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: 13270 $
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 siteStylesheetFile(String site_home,
209 String filename) {
210 return site_home+File.separatorChar + "transform" +
211 File.separatorChar + filename;
212 }
213 static public String collStylesheetFile(String site_home,
214 String coll_name,
215 String filename) {
216 return collectionBaseDir(site_home, coll_name) + File.separatorChar+
217 "transform" + File.separatorChar + filename;
218 }
219
220
221 /** returns the absolute path to a stylesheet
222 * stylesheets are looked for in the following places, in the
223 * following order:
224 * current collection, current site, current interface, base interfaces
225 * returns null if the file cannot be found
226 *
227 * this is not so good because sites may be on a different computer */
228 static public String stylesheetFile(String gsdl3_home,
229 String site_name,
230 String collection,
231 String interface_name,
232 ArrayList base_interfaces,
233 String filename) {
234
235 String site_home = siteHome(gsdl3_home, site_name);
236 // try collection first
237 File stylesheet = null;
238 if (!collection.equals("")) {
239
240 String coll_home = collectionBaseDir(site_home, collection);
241 stylesheet = new File(coll_home +File.separatorChar+
242 "transform"+File.separatorChar+filename);
243 if (stylesheet.exists()) {
244 return stylesheet.getPath();
245 }
246 }
247
248 // try site one next
249 stylesheet = new File(site_home +File.separatorChar+
250 "transform"+File.separatorChar+filename);
251 if (stylesheet.exists()) {
252 return stylesheet.getPath();
253 }
254
255 // try current interface
256 String interface_home = interfaceHome(gsdl3_home,
257 interface_name);
258 stylesheet = new File(interface_home+File.separatorChar+
259 "transform"+File.separatorChar+filename);
260 if (stylesheet.exists()) {
261 return stylesheet.getPath();
262 }
263 // try base interface
264 if (base_interfaces==null || base_interfaces.size()==0) {
265 return null; // no base interfaces to look for
266 }
267 for (int i=0; i<base_interfaces.size(); i++) {
268 interface_home = interfaceHome(gsdl3_home, (String)base_interfaces.get(i));
269 stylesheet = new File(interface_home+File.separatorChar+
270 "transform"+File.separatorChar+filename);
271 if (stylesheet.exists()) {
272 return stylesheet.getPath();
273 }
274 }
275
276 // still can't find it and we have looked everywhere
277 return null;
278 }
279
280
281 /** base directory for phind data */
282 public static String phindBaseDir(String site_home, String coll_name,
283 String phind_index) {
284 return site_home + File.separatorChar +
285 "collect" + File.separatorChar +
286 coll_name + File.separatorChar +
287 "index" + File.separatorChar +
288 "phind"+phind_index;
289 }
290
291 /** the gdbm database file -
292 * note, need to change extension depending on OS */
293 static public String GDBMDatabaseFile(String site_home,
294 String collection_name,
295 String index_stem) {
296
297 String db_ext = ".ldb";
298 if (Misc.isBigEndian()) {
299 db_ext = ".bdb";
300 }
301 return site_home + File.separatorChar +
302 "collect" + File.separatorChar +
303 collection_name + File.separatorChar +
304 "index" + File.separatorChar + "text" + File.separatorChar +
305 index_stem + db_ext;
306
307 }
308
309 // some file utility methods
310
311 /** read in a file and encode it using base64
312 * encoded data returned as a String */
313 static public String base64EncodeFromFile(String in_filename) {
314 byte [] data=null;
315 try {
316 data = readFile(in_filename);
317 } catch (Exception e) {
318 logger.error("couldn't read the file");
319 }
320 String encodedString = Base64.encode(data);
321 return encodedString;
322
323 }
324
325 /** decode some base64 data, and write it to the specified file */
326 static public boolean base64DecodeToFile(String data, String out_filename) {
327 try {
328 byte[] buffer=Base64.decode(data);
329 writeFile(buffer, out_filename);
330
331 } catch (Exception e) {
332 logger.error("file opening/closing errors"+e.getMessage());
333 return false;
334 }
335 return true;
336
337 }
338
339 /** read in a file to a byte array */
340 public static byte[] readFile(String filename) throws IOException {
341 File file = new File(filename);
342 BufferedInputStream bis = new BufferedInputStream(new
343 FileInputStream(file));
344 int bytes = (int) file.length();
345 byte[] buffer = new byte[bytes];
346 int readBytes = bis.read(buffer);
347 bis.close();
348 return buffer;
349 }
350
351 /** write a byte array to a file */
352 public static void writeFile(byte [] buffer, String filename) throws IOException {
353 File file = new File(filename);
354 BufferedOutputStream bos = new BufferedOutputStream(new
355 FileOutputStream(file));
356 bos.write(buffer);
357 bos.close();
358 }
359
360 public static boolean deleteFile(File f) {
361
362 if (f.isDirectory()) {
363 File[] files = f.listFiles();
364 for (int i=0; files!=null && i<files.length; i++) {
365 deleteFile(files[i]);
366 }
367
368 }
369 // delete the file or directory
370 return f.delete();
371 }
372
373 public static boolean copyFile(File source, File destination) {
374 if (!source.isFile()) {
375 logger.error(source.getPath()+" is not a file!");
376 return false;
377 }
378 try {
379 destination.getParentFile().mkdirs();
380 FileInputStream in = new FileInputStream(source);
381 FileOutputStream out = new FileOutputStream(destination);
382 int value = 0;
383 while((value = in.read()) != -1) {
384 out.write(value);
385 }
386 in.close();
387 out.close();
388 } catch (Exception e) {
389 logger.error("something went wrong copying "+source.getPath()+" to "+destination.getPath());
390 logger.error("Exception: "+e.getMessage());
391 return false;
392 }
393
394 return true;
395 }
396 /** Recursively moves the contents of source file to destination,
397 * maintaining paths.
398 * @param source A File representing the directory whose contents you
399 * wish to move.
400 * @param destination A File representing the directory you wish to move
401 * files to.
402 * @return true if successful
403 */
404 public static boolean moveDirectory(File source, File destination) {
405
406 // first try rename
407 if (source.renameTo(destination)) {
408 return true;
409 }
410
411 // john T. said that this sometimes doesn't work, so you have to copy files manually
412
413 // copied from gatherer Utility class
414 File input[] = source.listFiles();
415 for(int i = 0; i < input.length; i++) {
416 File output = new File(destination, input[i].getName());
417 if (input[i].isDirectory()) {
418 moveDirectory(input[i], output);
419 } else {
420 // Copy the file
421 try {
422 output.getParentFile().mkdirs();
423 FileInputStream in = new FileInputStream(input[i]);
424 FileOutputStream out = new FileOutputStream(output);
425 int value = 0;
426 while((value = in.read()) != -1) {
427 out.write(value);
428 }
429 in.close();
430 out.close();
431 // Delete file
432 input[i].delete();
433 } catch (Exception e) {
434 logger.error("exception: "+e.getMessage());
435 return false;
436 }
437
438 }
439 }
440 return true;
441 }
442
443}
Note: See TracBrowser for help on using the repository browser.