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

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

added a few new methods

  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 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: 5109 $
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
175
176 /** returns the absolute path to a stylesheet
177 * stylesheets are looked for in the following order
178 * collect-specific,
179 * site-specific, interface-specific, default
180 * returns null if the file cannot be found
181 *
182 * this is not so good because sites may be on a different computer */
183 static public String stylesheetFile(String gsdl3_home,
184 String site_name,
185 String interface_name,
186 String collection,
187 String filename) {
188
189 String site_home = siteHome(gsdl3_home, site_name);
190 // try collection first
191 File stylesheet = null;
192 if (!collection.equals("")) {
193
194 String coll_home = collectionBaseDir(site_home, collection);
195 stylesheet = new File(coll_home +File.separatorChar+
196 "transform"+File.separatorChar+filename);
197 if (stylesheet.exists()) {
198 return stylesheet.getPath();
199 }
200 }
201 // try site one next
202
203 stylesheet = new File(site_home +File.separatorChar+
204 "transform"+File.separatorChar+filename);
205 if (stylesheet.exists()) {
206 return stylesheet.getPath();
207 }
208 // try current interface
209 String interface_home = interfaceHome(gsdl3_home,
210 interface_name);
211 stylesheet = new File(interface_home+File.separatorChar+
212 "transform"+File.separatorChar+filename);
213 if (stylesheet.exists()) {
214 return stylesheet.getPath();
215 }
216 // try default interface
217 interface_home = interfaceHome(gsdl3_home, "default");
218 stylesheet = new File(interface_home+File.separatorChar+
219 "transform"+File.separatorChar+filename);
220 if (stylesheet.exists()) {
221 return stylesheet.getPath();
222 }
223
224 // cant find it
225 return null;
226 }
227
228 /** returns the path to the stylesheet used to transform config file
229 * format statements*/
230 public static String configFileFormatStylesheet() {
231 URL url = ClassLoader.getSystemResource("config_format.xsl");
232 if (url == null) {
233 return "";
234 }
235 return url.getFile();
236 }
237
238 /** base directory for phind data */
239 public static String phindBaseDir(String site_home, String coll_name,
240 String phind_index) {
241 return site_home + File.separatorChar +
242 "collect" + File.separatorChar +
243 coll_name + File.separatorChar +
244 "index" + File.separatorChar +
245 "phind"+phind_index;
246 }
247
248 /** the gdbm database file -
249 * note, need to change extension depending on OS */
250 static public String GDBMDatabaseFile(String site_home,
251 String collection_name) {
252
253 return site_home + File.separatorChar +
254 "collect" + File.separatorChar +
255 collection_name + File.separatorChar +
256 "index" + File.separatorChar + "text" + File.separatorChar +
257 collection_name + ".ldb";
258
259 }
260
261 // some file utility methods
262
263 /** read in a file and encode it using base64
264 * encoded data returned as a String */
265 static public String base64EncodeFromFile(String in_filename) {
266 byte [] data=null;
267 try {
268 data = readFile(in_filename);
269 } catch (Exception e) {
270 System.err.println("GSFile.base64EncodeFromFile: couldn't read the file");
271 }
272 String encodedString = Base64.encode(data);
273 return encodedString;
274
275 }
276
277 /** decode some base64 data, and write it to the specified file */
278 static public boolean base64DecodeToFile(String data, String out_filename) {
279 try {
280 byte[] buffer=Base64.decode(data);
281 writeFile(buffer, out_filename);
282
283 } catch (Exception e) {
284 System.err.println("GSFile.base64DecodeToFile: file opening/closing errors"+e.getMessage());
285 return false;
286 }
287 return true;
288
289 }
290
291 /** read in a file to a byte array */
292 public static byte[] readFile(String filename) throws IOException {
293 File file = new File(filename);
294 BufferedInputStream bis = new BufferedInputStream(new
295 FileInputStream(file));
296 int bytes = (int) file.length();
297 byte[] buffer = new byte[bytes];
298 int readBytes = bis.read(buffer);
299 bis.close();
300 return buffer;
301 }
302
303 /** write a byte array to a file */
304 public static void writeFile(byte [] buffer, String filename) throws IOException {
305 File file = new File(filename);
306 BufferedOutputStream bos = new BufferedOutputStream(new
307 FileOutputStream(file));
308 bos.write(buffer);
309 bos.close();
310 }
311
312 public static boolean deleteFile(File f) {
313
314 if (f.isDirectory()) {
315 File[] files = f.listFiles();
316 for (int i=0; files!=null && i<files.length; i++) {
317 deleteFile(files[i]);
318 }
319
320 }
321 // delete the file or directory
322 return f.delete();
323 }
324
325 /** Recursively moves the contents of source file to destination,
326 * maintaining paths.
327 * @param source A File representing the directory whose contents you
328 * wish to move.
329 * @param destination A File representing the directory you wish to move
330 * files to.
331 * @return true if successful
332 */
333 public static boolean moveDirectory(File source, File destination) {
334
335 // first try rename
336 if (source.renameTo(destination)) {
337 return true;
338 }
339
340 // john T. said that this sometimes doesn't work, so you have to copy files manually
341
342 // copied from gatherer Utility class
343 File input[] = source.listFiles();
344 for(int i = 0; i < input.length; i++) {
345 File output = new File(destination, input[i].getName());
346 if (input[i].isDirectory()) {
347 moveDirectory(input[i], output);
348 } else {
349 // Copy the file
350 try {
351 output.getParentFile().mkdirs();
352 FileInputStream in = new FileInputStream(input[i]);
353 FileOutputStream out = new FileOutputStream(output);
354 int value = 0;
355 while((value = in.read()) != -1) {
356 out.write(value);
357 }
358 in.close();
359 out.close();
360 // Delete file
361 input[i].delete();
362 } catch (Exception e) {
363 System.err.println("GSFile.moveDirectory: exception: "+e.getMessage());
364 return false;
365 }
366
367 }
368 }
369 return true;
370 }
371
372}
Note: See TracBrowser for help on using the repository browser.