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