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 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | import java.io.File;
|
---|
22 | //import org.apache.soap.encoding.soapenc.Base64;
|
---|
23 | import org.apache.axis.encoding.Base64;
|
---|
24 | import java.io.BufferedInputStream;
|
---|
25 | import java.io.BufferedOutputStream;
|
---|
26 | import java.io.FileInputStream;
|
---|
27 | import java.io.FileOutputStream;
|
---|
28 | import java.io.InputStream;
|
---|
29 | import java.io.IOException;
|
---|
30 | import java.lang.ClassLoader; // to find files on the class path
|
---|
31 | import java.net.URL;
|
---|
32 | import 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: 10650 $
|
---|
42 | * @see File
|
---|
43 | */
|
---|
44 |
|
---|
45 | public 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 | String index_stem) {
|
---|
292 |
|
---|
293 | String db_ext = ".ldb";
|
---|
294 | if (Misc.isMac()) {
|
---|
295 | db_ext = ".bdb";
|
---|
296 | }
|
---|
297 | return site_home + File.separatorChar +
|
---|
298 | "collect" + File.separatorChar +
|
---|
299 | collection_name + File.separatorChar +
|
---|
300 | "index" + File.separatorChar + "text" + File.separatorChar +
|
---|
301 | index_stem + db_ext;
|
---|
302 |
|
---|
303 | }
|
---|
304 |
|
---|
305 | // some file utility methods
|
---|
306 |
|
---|
307 | /** read in a file and encode it using base64
|
---|
308 | * encoded data returned as a String */
|
---|
309 | static public String base64EncodeFromFile(String in_filename) {
|
---|
310 | byte [] data=null;
|
---|
311 | try {
|
---|
312 | data = readFile(in_filename);
|
---|
313 | } catch (Exception e) {
|
---|
314 | System.err.println("GSFile.base64EncodeFromFile: couldn't read the file");
|
---|
315 | }
|
---|
316 | String encodedString = Base64.encode(data);
|
---|
317 | return encodedString;
|
---|
318 |
|
---|
319 | }
|
---|
320 |
|
---|
321 | /** decode some base64 data, and write it to the specified file */
|
---|
322 | static public boolean base64DecodeToFile(String data, String out_filename) {
|
---|
323 | try {
|
---|
324 | byte[] buffer=Base64.decode(data);
|
---|
325 | writeFile(buffer, out_filename);
|
---|
326 |
|
---|
327 | } catch (Exception e) {
|
---|
328 | System.err.println("GSFile.base64DecodeToFile: file opening/closing errors"+e.getMessage());
|
---|
329 | return false;
|
---|
330 | }
|
---|
331 | return true;
|
---|
332 |
|
---|
333 | }
|
---|
334 |
|
---|
335 | /** read in a file to a byte array */
|
---|
336 | public static byte[] readFile(String filename) throws IOException {
|
---|
337 | File file = new File(filename);
|
---|
338 | BufferedInputStream bis = new BufferedInputStream(new
|
---|
339 | FileInputStream(file));
|
---|
340 | int bytes = (int) file.length();
|
---|
341 | byte[] buffer = new byte[bytes];
|
---|
342 | int readBytes = bis.read(buffer);
|
---|
343 | bis.close();
|
---|
344 | return buffer;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /** write a byte array to a file */
|
---|
348 | public static void writeFile(byte [] buffer, String filename) throws IOException {
|
---|
349 | File file = new File(filename);
|
---|
350 | BufferedOutputStream bos = new BufferedOutputStream(new
|
---|
351 | FileOutputStream(file));
|
---|
352 | bos.write(buffer);
|
---|
353 | bos.close();
|
---|
354 | }
|
---|
355 |
|
---|
356 | public static boolean deleteFile(File f) {
|
---|
357 |
|
---|
358 | if (f.isDirectory()) {
|
---|
359 | File[] files = f.listFiles();
|
---|
360 | for (int i=0; files!=null && i<files.length; i++) {
|
---|
361 | deleteFile(files[i]);
|
---|
362 | }
|
---|
363 |
|
---|
364 | }
|
---|
365 | // delete the file or directory
|
---|
366 | return f.delete();
|
---|
367 | }
|
---|
368 |
|
---|
369 | public static boolean copyFile(File source, File destination) {
|
---|
370 | if (!source.isFile()) {
|
---|
371 | System.err.println("GSFile.copyFile(): "+source.getPath()+" is not a file!");
|
---|
372 | return false;
|
---|
373 | }
|
---|
374 | try {
|
---|
375 | destination.getParentFile().mkdirs();
|
---|
376 | FileInputStream in = new FileInputStream(source);
|
---|
377 | FileOutputStream out = new FileOutputStream(destination);
|
---|
378 | int value = 0;
|
---|
379 | while((value = in.read()) != -1) {
|
---|
380 | out.write(value);
|
---|
381 | }
|
---|
382 | in.close();
|
---|
383 | out.close();
|
---|
384 | } catch (Exception e) {
|
---|
385 | System.err.println("GSFile.copyFile(): something went wrong copying "+source.getPath()+" to "+destination.getPath());
|
---|
386 | System.err.println("Exception: "+e.getMessage());
|
---|
387 | return false;
|
---|
388 | }
|
---|
389 |
|
---|
390 | return true;
|
---|
391 | }
|
---|
392 | /** Recursively moves the contents of source file to destination,
|
---|
393 | * maintaining paths.
|
---|
394 | * @param source A File representing the directory whose contents you
|
---|
395 | * wish to move.
|
---|
396 | * @param destination A File representing the directory you wish to move
|
---|
397 | * files to.
|
---|
398 | * @return true if successful
|
---|
399 | */
|
---|
400 | public static boolean moveDirectory(File source, File destination) {
|
---|
401 |
|
---|
402 | // first try rename
|
---|
403 | if (source.renameTo(destination)) {
|
---|
404 | return true;
|
---|
405 | }
|
---|
406 |
|
---|
407 | // john T. said that this sometimes doesn't work, so you have to copy files manually
|
---|
408 |
|
---|
409 | // copied from gatherer Utility class
|
---|
410 | File input[] = source.listFiles();
|
---|
411 | for(int i = 0; i < input.length; i++) {
|
---|
412 | File output = new File(destination, input[i].getName());
|
---|
413 | if (input[i].isDirectory()) {
|
---|
414 | moveDirectory(input[i], output);
|
---|
415 | } else {
|
---|
416 | // Copy the file
|
---|
417 | try {
|
---|
418 | output.getParentFile().mkdirs();
|
---|
419 | FileInputStream in = new FileInputStream(input[i]);
|
---|
420 | FileOutputStream out = new FileOutputStream(output);
|
---|
421 | int value = 0;
|
---|
422 | while((value = in.read()) != -1) {
|
---|
423 | out.write(value);
|
---|
424 | }
|
---|
425 | in.close();
|
---|
426 | out.close();
|
---|
427 | // Delete file
|
---|
428 | input[i].delete();
|
---|
429 | } catch (Exception e) {
|
---|
430 | System.err.println("GSFile.moveDirectory: exception: "+e.getMessage());
|
---|
431 | return false;
|
---|
432 | }
|
---|
433 |
|
---|
434 | }
|
---|
435 | }
|
---|
436 | return true;
|
---|
437 | }
|
---|
438 |
|
---|
439 | }
|
---|