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

Last change on this file since 10187 was 10187, checked in by kjdon, 19 years ago

modified the gdbm db file path to use .bdb if on a mac

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