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

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

added getResourceDir methods to provide the path to the collection resources directory

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