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

Last change on this file since 8746 was 8562, checked in by kjdon, 20 years ago

interfaces are now based on another, rather than all using the default as a base. So the chain of looking for xslt is now collection, site, current interface, base interfaces (maybe more than one or none)

  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 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: 8562 $
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 /** absolute path for an associated file */
176 static public String assocFileAbsolutePath(String site_home,
177 String collection_name,
178 String assoc_file_path,
179 String filename) {
180 return collectionBaseDir(site_home, collection_name)+
181 File.separatorChar+"index"+File.separatorChar+
182 "assoc"+File.separatorChar+assoc_file_path+
183 File.separatorChar+filename;
184 }
185
186 static public String siteHome(String gsdl3_home, String site_name) {
187 return gsdl3_home + File.separatorChar + "web" +
188 File.separatorChar + "sites" +
189 File.separatorChar +site_name;
190 }
191
192 static public String interfaceHome(String gsdl3_home,
193 String interface_name) {
194 return gsdl3_home + File.separatorChar + "web" +
195 File.separatorChar + "interfaces" +
196 File.separatorChar + interface_name;
197 }
198
199 static public String siteStylesheetFile(String site_home,
200 String filename) {
201 return site_home+File.separatorChar + "transform" +
202 File.separatorChar + filename;
203 }
204 static public String collStylesheetFile(String site_home,
205 String coll_name,
206 String filename) {
207 return collectionBaseDir(site_home, coll_name) + File.separatorChar+
208 "transform" + File.separatorChar + filename;
209 }
210
211
212 /** returns the absolute path to a stylesheet
213 * stylesheets are looked for in the following places, in the
214 * following order:
215 * current collection, current site, current interface, base interfaces
216 * returns null if the file cannot be found
217 *
218 * this is not so good because sites may be on a different computer */
219 static public String stylesheetFile(String gsdl3_home,
220 String site_name,
221 String collection,
222 String interface_name,
223 ArrayList base_interfaces,
224 String filename) {
225
226 String site_home = siteHome(gsdl3_home, site_name);
227 // try collection first
228 File stylesheet = null;
229 if (!collection.equals("")) {
230
231 String coll_home = collectionBaseDir(site_home, collection);
232 stylesheet = new File(coll_home +File.separatorChar+
233 "transform"+File.separatorChar+filename);
234 if (stylesheet.exists()) {
235 return stylesheet.getPath();
236 }
237 }
238
239 // try site one next
240 stylesheet = new File(site_home +File.separatorChar+
241 "transform"+File.separatorChar+filename);
242 if (stylesheet.exists()) {
243 return stylesheet.getPath();
244 }
245
246 // try current interface
247 String interface_home = interfaceHome(gsdl3_home,
248 interface_name);
249 stylesheet = new File(interface_home+File.separatorChar+
250 "transform"+File.separatorChar+filename);
251 if (stylesheet.exists()) {
252 return stylesheet.getPath();
253 }
254 // try base interface
255 if (base_interfaces==null || base_interfaces.size()==0) {
256 return null; // no base interfaces to look for
257 }
258 for (int i=0; i<base_interfaces.size(); i++) {
259 interface_home = interfaceHome(gsdl3_home, (String)base_interfaces.get(i));
260 stylesheet = new File(interface_home+File.separatorChar+
261 "transform"+File.separatorChar+filename);
262 if (stylesheet.exists()) {
263 return stylesheet.getPath();
264 }
265 }
266
267 // still can't find it and we have looked everywhere
268 return null;
269 }
270
271 /** returns the path to the stylesheet used to transform config file
272 * format statements*/
273 public static String configFileFormatStylesheet() {
274 URL url = ClassLoader.getSystemResource("config_format.xsl");
275 if (url == null) {
276 return "";
277 }
278 return url.getFile();
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
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 + ".ldb";
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.