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

Last change on this file since 5261 was 5261, checked in by kjdon, 21 years ago

added method for finding a stylesheet in a coll

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