[3235] | 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 | */
|
---|
[3222] | 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
| 21 | import java.io.File;
|
---|
[9874] | 22 | //import org.apache.soap.encoding.soapenc.Base64;
|
---|
| 23 | import org.apache.axis.encoding.Base64;
|
---|
[3284] | 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;
|
---|
[3848] | 30 | import java.lang.ClassLoader; // to find files on the class path
|
---|
| 31 | import java.net.URL;
|
---|
[8562] | 32 | import java.util.ArrayList;
|
---|
[3222] | 33 |
|
---|
| 34 | /**
|
---|
[3440] | 35 | * GSFile - utility class for Greenstone.
|
---|
[3222] | 36 | *
|
---|
[3440] | 37 | * all file paths are created here
|
---|
[3477] | 38 | * also has file utility methods
|
---|
[3222] | 39 | *
|
---|
[3235] | 40 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
| 41 | * @version $Revision: 10650 $
|
---|
[3222] | 42 | * @see File
|
---|
| 43 | */
|
---|
[3440] | 44 |
|
---|
[3222] | 45 | public class GSFile {
|
---|
| 46 |
|
---|
[3440] | 47 | /** site config file path */
|
---|
| 48 | static public String siteConfigFile(String site_home) {
|
---|
[3489] | 49 | return site_home + File.separatorChar+"siteConfig.xml";
|
---|
[3222] | 50 |
|
---|
| 51 | }
|
---|
[4245] | 52 | /** interface config file path */
|
---|
| 53 | static public String interfaceConfigFile(String interface_home) {
|
---|
| 54 | return interface_home + File.separatorChar+"interfaceConfig.xml";
|
---|
[3222] | 55 |
|
---|
[4245] | 56 | }
|
---|
| 57 |
|
---|
[3440] | 58 | /** collection directory path */
|
---|
| 59 | static public String collectDir(String site_home) {
|
---|
| 60 | return site_home+File.separatorChar+"collect";
|
---|
[3222] | 61 | }
|
---|
| 62 |
|
---|
[3284] | 63 |
|
---|
[3440] | 64 | /** collection config file path*/
|
---|
| 65 | static public String collectionConfigFile(String site_home,
|
---|
| 66 | String collection_name) {
|
---|
[6863] | 67 | return collectionConfigFile(collectionBaseDir(site_home, collection_name));
|
---|
| 68 | }
|
---|
| 69 | static public String collectionConfigFile(String collection_home) {
|
---|
| 70 | return collectionEtcDir(collection_home) +
|
---|
[3489] | 71 | File.separatorChar+"collectionConfig.xml";
|
---|
[3222] | 72 |
|
---|
| 73 | }
|
---|
[5109] | 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";
|
---|
[3222] | 81 |
|
---|
[5109] | 82 | }
|
---|
| 83 |
|
---|
[3440] | 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"+
|
---|
[3222] | 88 | File.separatorChar+collection_name+
|
---|
| 89 | File.separatorChar+"index"+
|
---|
[3489] | 90 | File.separatorChar+"buildConfig.xml";
|
---|
[3222] | 91 | }
|
---|
[3477] | 92 | /** collection build config file path*/
|
---|
| 93 | static public String collectionBuildConfigFileBuilding(String site_home,
|
---|
| 94 | String collection_name ) {
|
---|
[6893] | 95 | return collectionBuildConfigFileBuilding(collectionBaseDir(site_home, collection_name));
|
---|
[3477] | 96 | }
|
---|
[6893] | 97 | static public String collectionBuildConfigFileBuilding(String collection_home ) {
|
---|
| 98 | return collection_home+
|
---|
| 99 | File.separatorChar+"building"+
|
---|
| 100 | File.separatorChar+"buildConfig.xml";
|
---|
| 101 | }
|
---|
[3222] | 102 |
|
---|
[3440] | 103 | /** XML Transform directory path */
|
---|
| 104 | static public String xmlTransformDir(String interface_home) {
|
---|
| 105 | return interface_home+File.separatorChar+"transform";
|
---|
[3222] | 106 | }
|
---|
[3284] | 107 |
|
---|
[3440] | 108 | /** collection base directory path */
|
---|
[3284] | 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 | }
|
---|
[6863] | 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 |
|
---|
[3477] | 123 | /** collection building directory path */
|
---|
| 124 | static public String collectionBuildDir(String site_home,
|
---|
| 125 | String collection_name) {
|
---|
[6863] | 126 | return collectionBuildDir(collectionBaseDir(site_home, collection_name));
|
---|
[3477] | 127 | }
|
---|
[6863] | 128 | static public String collectionBuildDir(String collection_home) {
|
---|
| 129 | return collection_home+File.separator+"building";
|
---|
| 130 | }
|
---|
[3477] | 131 | /** collection building directory path */
|
---|
[6863] | 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 */
|
---|
[5109] | 141 | static public String collectionImportDir(String site_home,
|
---|
| 142 | String collection_name) {
|
---|
[6863] | 143 | return collectionImportDir(collectionBaseDir(site_home, collection_name));
|
---|
| 144 |
|
---|
[5109] | 145 | }
|
---|
[6863] | 146 | static public String collectionImportDir(String collection_home) {
|
---|
| 147 | return collection_home + File.separatorChar+"import";
|
---|
| 148 | }
|
---|
[5109] | 149 | /** collection building directory path */
|
---|
[3477] | 150 | static public String collectionIndexDir(String site_home,
|
---|
| 151 | String collection_name) {
|
---|
[6863] | 152 | return collectionIndexDir(collectionBaseDir(site_home, collection_name));
|
---|
| 153 |
|
---|
[3477] | 154 | }
|
---|
[6863] | 155 | static public String collectionIndexDir(String collection_home) {
|
---|
| 156 | return collection_home + File.separatorChar+"index";
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[3440] | 159 | /** text path (for doc retrieval) relative to collectionBaseDir */
|
---|
[9874] | 160 | static public String collectionTextPath(String index_stem) {
|
---|
[3284] | 161 | return "index"+File.separatorChar+"text"+File.separatorChar+
|
---|
[9874] | 162 | index_stem;
|
---|
[3284] | 163 | }
|
---|
| 164 |
|
---|
[3440] | 165 | /** index path (for querying) relative to collectionBaseDir */
|
---|
[9874] | 166 | static public String collectionIndexPath(String index_stem,
|
---|
[3284] | 167 | String index_name) {
|
---|
| 168 | return "index"+File.separatorChar+index_name+File.separatorChar+
|
---|
[9874] | 169 | index_stem;
|
---|
[3284] | 170 | }
|
---|
[9425] | 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 | }
|
---|
[3284] | 181 |
|
---|
[3440] | 182 | /** absolute path for an associated file */
|
---|
[3284] | 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 | }
|
---|
[3477] | 192 |
|
---|
[4050] | 193 | static public String siteHome(String gsdl3_home, String site_name) {
|
---|
[10100] | 194 | return gsdl3_home + File.separatorChar + "sites" +
|
---|
[3477] | 195 | File.separatorChar +site_name;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[4050] | 198 | static public String interfaceHome(String gsdl3_home,
|
---|
[3477] | 199 | String interface_name) {
|
---|
[10100] | 200 | return gsdl3_home + File.separatorChar + "interfaces" +
|
---|
[3477] | 201 | File.separatorChar + interface_name;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[5109] | 204 | static public String siteStylesheetFile(String site_home,
|
---|
| 205 | String filename) {
|
---|
| 206 | return site_home+File.separatorChar + "transform" +
|
---|
| 207 | File.separatorChar + filename;
|
---|
| 208 | }
|
---|
[5261] | 209 | static public String collStylesheetFile(String site_home,
|
---|
| 210 | String coll_name,
|
---|
| 211 | String filename) {
|
---|
| 212 | return collectionBaseDir(site_home, coll_name) + File.separatorChar+
|
---|
[5961] | 213 | "transform" + File.separatorChar + filename;
|
---|
[5261] | 214 | }
|
---|
[5109] | 215 |
|
---|
| 216 |
|
---|
[3341] | 217 | /** returns the absolute path to a stylesheet
|
---|
[8562] | 218 | * stylesheets are looked for in the following places, in the
|
---|
| 219 | * following order:
|
---|
| 220 | * current collection, current site, current interface, base interfaces
|
---|
[3941] | 221 | * returns null if the file cannot be found
|
---|
| 222 | *
|
---|
| 223 | * this is not so good because sites may be on a different computer */
|
---|
[4700] | 224 | static public String stylesheetFile(String gsdl3_home,
|
---|
| 225 | String site_name,
|
---|
[8562] | 226 | String collection,
|
---|
[4700] | 227 | String interface_name,
|
---|
[8562] | 228 | ArrayList base_interfaces,
|
---|
[4700] | 229 | String filename) {
|
---|
[4860] | 230 |
|
---|
[4700] | 231 | String site_home = siteHome(gsdl3_home, site_name);
|
---|
[4860] | 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 |
|
---|
[8562] | 244 | // try site one next
|
---|
[4860] | 245 | stylesheet = new File(site_home +File.separatorChar+
|
---|
[8562] | 246 | "transform"+File.separatorChar+filename);
|
---|
[3341] | 247 | if (stylesheet.exists()) {
|
---|
| 248 | return stylesheet.getPath();
|
---|
| 249 | }
|
---|
[8562] | 250 |
|
---|
[3440] | 251 | // try current interface
|
---|
[4700] | 252 | String interface_home = interfaceHome(gsdl3_home,
|
---|
| 253 | interface_name);
|
---|
[3477] | 254 | stylesheet = new File(interface_home+File.separatorChar+
|
---|
[3341] | 255 | "transform"+File.separatorChar+filename);
|
---|
| 256 | if (stylesheet.exists()) {
|
---|
| 257 | return stylesheet.getPath();
|
---|
| 258 | }
|
---|
[8562] | 259 | // try base interface
|
---|
| 260 | if (base_interfaces==null || base_interfaces.size()==0) {
|
---|
| 261 | return null; // no base interfaces to look for
|
---|
[3341] | 262 | }
|
---|
[8562] | 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 | }
|
---|
[3341] | 271 |
|
---|
[8562] | 272 | // still can't find it and we have looked everywhere
|
---|
[3341] | 273 | return null;
|
---|
| 274 | }
|
---|
[3440] | 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,
|
---|
[10650] | 290 | String collection_name,
|
---|
| 291 | String index_stem) {
|
---|
[3440] | 292 |
|
---|
[10187] | 293 | String db_ext = ".ldb";
|
---|
| 294 | if (Misc.isMac()) {
|
---|
| 295 | db_ext = ".bdb";
|
---|
| 296 | }
|
---|
[3440] | 297 | return site_home + File.separatorChar +
|
---|
| 298 | "collect" + File.separatorChar +
|
---|
| 299 | collection_name + File.separatorChar +
|
---|
| 300 | "index" + File.separatorChar + "text" + File.separatorChar +
|
---|
[10650] | 301 | index_stem + db_ext;
|
---|
[3440] | 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 */
|
---|
[3284] | 309 | static public String base64EncodeFromFile(String in_filename) {
|
---|
| 310 | byte [] data=null;
|
---|
| 311 | try {
|
---|
| 312 | data = readFile(in_filename);
|
---|
| 313 | } catch (Exception e) {
|
---|
[3941] | 314 | System.err.println("GSFile.base64EncodeFromFile: couldn't read the file");
|
---|
[3284] | 315 | }
|
---|
| 316 | String encodedString = Base64.encode(data);
|
---|
| 317 | return encodedString;
|
---|
| 318 |
|
---|
| 319 | }
|
---|
[3440] | 320 |
|
---|
| 321 | /** decode some base64 data, and write it to the specified file */
|
---|
[3284] | 322 | static public boolean base64DecodeToFile(String data, String out_filename) {
|
---|
| 323 | try {
|
---|
| 324 | byte[] buffer=Base64.decode(data);
|
---|
| 325 | writeFile(buffer, out_filename);
|
---|
[3489] | 326 |
|
---|
[3284] | 327 | } catch (Exception e) {
|
---|
[3941] | 328 | System.err.println("GSFile.base64DecodeToFile: file opening/closing errors"+e.getMessage());
|
---|
[3284] | 329 | return false;
|
---|
| 330 | }
|
---|
| 331 | return true;
|
---|
| 332 |
|
---|
| 333 | }
|
---|
[3222] | 334 |
|
---|
[3440] | 335 | /** read in a file to a byte array */
|
---|
[3284] | 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 | }
|
---|
[3440] | 346 |
|
---|
| 347 | /** write a byte array to a file */
|
---|
[3284] | 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 |
|
---|
[3477] | 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 |
|
---|
[5961] | 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 | }
|
---|
[3477] | 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());
|
---|
[3941] | 413 | if (input[i].isDirectory()) {
|
---|
[3477] | 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) {
|
---|
[3941] | 430 | System.err.println("GSFile.moveDirectory: exception: "+e.getMessage());
|
---|
[3477] | 431 | return false;
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | }
|
---|
| 435 | }
|
---|
| 436 | return true;
|
---|
| 437 | }
|
---|
[5961] | 438 |
|
---|
[3222] | 439 | }
|
---|