[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 |
|
---|
[24509] | 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;
|
---|
[24859] | 32 | import java.nio.channels.FileChannel;
|
---|
[8562] | 33 | import java.util.ArrayList;
|
---|
[26026] | 34 | import java.util.Collection;
|
---|
[3222] | 35 |
|
---|
[13124] | 36 | import org.apache.log4j.*;
|
---|
[26026] | 37 | import org.greenstone.util.GlobalProperties;
|
---|
[13124] | 38 |
|
---|
[3222] | 39 | /**
|
---|
[3440] | 40 | * GSFile - utility class for Greenstone.
|
---|
[24509] | 41 | *
|
---|
| 42 | * all file paths are created here also has file utility methods
|
---|
| 43 | *
|
---|
[25727] | 44 | * @author Katherine Don
|
---|
[3235] | 45 | * @version $Revision: 26026 $
|
---|
[3222] | 46 | * @see File
|
---|
| 47 | */
|
---|
[3440] | 48 |
|
---|
[24509] | 49 | public class GSFile
|
---|
| 50 | {
|
---|
[3222] | 51 |
|
---|
[24509] | 52 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSFile.class.getName());
|
---|
[13124] | 53 |
|
---|
[24509] | 54 | /** site config file path */
|
---|
| 55 | static public String siteConfigFile(String site_home)
|
---|
| 56 | {
|
---|
| 57 | return site_home + File.separatorChar + "siteConfig.xml";
|
---|
[3222] | 58 |
|
---|
[24509] | 59 | }
|
---|
[3222] | 60 |
|
---|
[24509] | 61 | /** interface config file path */
|
---|
| 62 | static public String interfaceConfigFile(String interface_home)
|
---|
| 63 | {
|
---|
| 64 | return interface_home + File.separatorChar + "interfaceConfig.xml";
|
---|
[4245] | 65 |
|
---|
[24509] | 66 | }
|
---|
[3222] | 67 |
|
---|
[24509] | 68 | /** collection directory path */
|
---|
| 69 | static public String collectDir(String site_home)
|
---|
| 70 | {
|
---|
| 71 | return site_home + File.separatorChar + "collect";
|
---|
| 72 | }
|
---|
[3222] | 73 |
|
---|
[24509] | 74 | /** collection config file path */
|
---|
| 75 | static public String collectionConfigFile(String site_home, String collection_name)
|
---|
| 76 | {
|
---|
| 77 | return collectionConfigFile(collectionBaseDir(site_home, collection_name));
|
---|
| 78 | }
|
---|
[5109] | 79 |
|
---|
[24509] | 80 | static public String collectionConfigFile(String collection_home)
|
---|
| 81 | {
|
---|
| 82 | return collectionEtcDir(collection_home) + File.separatorChar + "collectionConfig.xml";
|
---|
[3222] | 83 |
|
---|
[24509] | 84 | }
|
---|
[3284] | 85 |
|
---|
[24509] | 86 | /** collection init file path */
|
---|
| 87 | static public String collectionInitFile(String site_home, String collection_name)
|
---|
| 88 | {
|
---|
| 89 | return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "etc" + File.separatorChar + "collectionInit.xml";
|
---|
[6863] | 90 |
|
---|
[24509] | 91 | }
|
---|
[3284] | 92 |
|
---|
[24509] | 93 | /** collection build config file path */
|
---|
| 94 | static public String collectionBuildConfigFile(String site_home, String collection_name)
|
---|
| 95 | {
|
---|
| 96 | return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "index" + File.separatorChar + "buildConfig.xml";
|
---|
| 97 | }
|
---|
[3284] | 98 |
|
---|
[24509] | 99 | /** collection build config file path */
|
---|
| 100 | static public String collectionBuildConfigFileBuilding(String site_home, String collection_name)
|
---|
| 101 | {
|
---|
| 102 | return collectionBuildConfigFileBuilding(collectionBaseDir(site_home, collection_name));
|
---|
| 103 | }
|
---|
[3477] | 104 |
|
---|
[24509] | 105 | static public String collectionBuildConfigFileBuilding(String collection_home)
|
---|
| 106 | {
|
---|
| 107 | return collection_home + File.separatorChar + "building" + File.separatorChar + "buildConfig.xml";
|
---|
| 108 | }
|
---|
[3477] | 109 |
|
---|
[24509] | 110 | /** XML Transform directory path */
|
---|
| 111 | static public String xmlTransformDir(String interface_home)
|
---|
| 112 | {
|
---|
| 113 | return interface_home + File.separatorChar + "transform";
|
---|
| 114 | }
|
---|
[24248] | 115 |
|
---|
[24509] | 116 | /** collection base directory path */
|
---|
| 117 | static public String collectionBaseDir(String site_home, String collection_name)
|
---|
| 118 | {
|
---|
| 119 | return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name;
|
---|
| 120 | }
|
---|
[4860] | 121 |
|
---|
[24509] | 122 | /** collection archive directory path */
|
---|
| 123 | static public String collectionArchiveDir(String site_home, String collection_name)
|
---|
| 124 | {
|
---|
| 125 | return collectionArchiveDir(collectionBaseDir(site_home, collection_name));
|
---|
[4860] | 126 | }
|
---|
[24509] | 127 |
|
---|
| 128 | static public String collectionArchiveDir(String collection_home)
|
---|
| 129 | {
|
---|
| 130 | return collection_home + File.separatorChar + "archives";
|
---|
[3341] | 131 | }
|
---|
[24509] | 132 |
|
---|
| 133 | /** collection building directory path */
|
---|
| 134 | static public String collectionBuildDir(String site_home, String collection_name)
|
---|
| 135 | {
|
---|
| 136 | return collectionBuildDir(collectionBaseDir(site_home, collection_name));
|
---|
[3341] | 137 | }
|
---|
[24509] | 138 |
|
---|
| 139 | static public String collectionBuildDir(String collection_home)
|
---|
| 140 | {
|
---|
| 141 | return collection_home + File.separator + "building";
|
---|
[3341] | 142 | }
|
---|
[24509] | 143 |
|
---|
| 144 | /** collection building directory path */
|
---|
| 145 | static public String collectionEtcDir(String site_home, String collection_name)
|
---|
| 146 | {
|
---|
| 147 | return collectionEtcDir(collectionBaseDir(site_home, collection_name));
|
---|
[8562] | 148 | }
|
---|
[3440] | 149 |
|
---|
[24509] | 150 | static public String collectionEtcDir(String collection_home)
|
---|
| 151 | {
|
---|
| 152 | return collection_home + File.separator + "etc";
|
---|
| 153 | }
|
---|
[3440] | 154 |
|
---|
[24509] | 155 | /** collection building directory path */
|
---|
| 156 | static public String collectionImportDir(String site_home, String collection_name)
|
---|
| 157 | {
|
---|
| 158 | return collectionImportDir(collectionBaseDir(site_home, collection_name));
|
---|
[3440] | 159 |
|
---|
[24509] | 160 | }
|
---|
[3440] | 161 |
|
---|
[24509] | 162 | static public String collectionImportDir(String collection_home)
|
---|
| 163 | {
|
---|
| 164 | return collection_home + File.separatorChar + "import";
|
---|
[18662] | 165 | }
|
---|
[3440] | 166 |
|
---|
[24509] | 167 | /** collection building directory path */
|
---|
| 168 | static public String collectionIndexDir(String site_home, String collection_name)
|
---|
| 169 | {
|
---|
| 170 | return collectionIndexDir(collectionBaseDir(site_home, collection_name));
|
---|
[3440] | 171 |
|
---|
[24509] | 172 | }
|
---|
[3440] | 173 |
|
---|
[24509] | 174 | static public String collectionIndexDir(String collection_home)
|
---|
| 175 | {
|
---|
| 176 | return collection_home + File.separatorChar + "index";
|
---|
[3284] | 177 | }
|
---|
| 178 |
|
---|
[24509] | 179 | /** text path (for doc retrieval) relative to collectionBaseDir */
|
---|
| 180 | static public String collectionTextPath(String index_stem)
|
---|
| 181 | {
|
---|
| 182 | return "index" + File.separatorChar + "text" + File.separatorChar + index_stem;
|
---|
| 183 | }
|
---|
[3440] | 184 |
|
---|
[24509] | 185 | /** index path (for querying) relative to collectionBaseDir */
|
---|
| 186 | static public String collectionIndexPath(String index_stem, String index_name)
|
---|
| 187 | {
|
---|
| 188 | return "index" + File.separatorChar + index_name + File.separatorChar + index_stem;
|
---|
[3284] | 189 | }
|
---|
[24509] | 190 |
|
---|
| 191 | /** collection resources directory path */
|
---|
| 192 | static public String collectionResourceDir(String site_home, String collection_name)
|
---|
| 193 | {
|
---|
| 194 | return collectionResourceDir(collectionBaseDir(site_home, collection_name));
|
---|
| 195 |
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | static public String collectionResourceDir(String collection_home)
|
---|
| 199 | {
|
---|
| 200 | return collection_home + File.separatorChar + "resources";
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | /** absolute path for an associated file */
|
---|
| 204 | static public String assocFileAbsolutePath(String site_home, String collection_name, String assoc_file_path, String filename)
|
---|
| 205 | {
|
---|
| 206 | return collectionBaseDir(site_home, collection_name) + File.separatorChar + "index" + File.separatorChar + "assoc" + File.separatorChar + assoc_file_path + File.separatorChar + filename;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[24720] | 209 | static public String extHome(String gsdl3_home, String ext_name)
|
---|
| 210 | {
|
---|
| 211 | return gsdl3_home + File.separatorChar + "ext" + File.separatorChar + ext_name;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[24509] | 214 | static public String siteHome(String gsdl3_home, String site_name)
|
---|
| 215 | {
|
---|
| 216 | return gsdl3_home + File.separatorChar + "sites" + File.separatorChar + site_name;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | static public String interfaceHome(String gsdl3_home, String interface_name)
|
---|
| 220 | {
|
---|
| 221 | return gsdl3_home + File.separatorChar + "interfaces" + File.separatorChar + interface_name;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | static public String interfaceStylesheetFile(String gsdl3_home, String interface_name, String filename)
|
---|
| 225 | {
|
---|
| 226 | return gsdl3_home + File.separatorChar + "interfaces" + File.separatorChar + interface_name + File.separatorChar + "transform" + File.separatorChar + filename;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | static public String siteStylesheetFile(String site_home, String filename)
|
---|
| 230 | {
|
---|
| 231 | return site_home + File.separatorChar + "transform" + File.separatorChar + filename;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | static public String collStylesheetFile(String site_home, String coll_name, String filename)
|
---|
| 235 | {
|
---|
| 236 | return collectionBaseDir(site_home, coll_name) + File.separatorChar + "transform" + File.separatorChar + filename;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /**
|
---|
[25652] | 240 | * returns the absolute path to a stylesheet. Stylesheets are looked for in
|
---|
[24509] | 241 | * the following places, in the following order: current collection, current
|
---|
| 242 | * site, current interface, base interfaces returns null if the file cannot
|
---|
| 243 | * be found
|
---|
| 244 | *
|
---|
| 245 | * this is not so good because sites may be on a different computer
|
---|
| 246 | */
|
---|
[25635] | 247 | static public String stylesheetFile(String gsdl3_home, String site_name, String collection, String interface_name, ArrayList<String> base_interfaces, String filename)
|
---|
[24509] | 248 | {
|
---|
| 249 |
|
---|
| 250 | String site_home = siteHome(gsdl3_home, site_name);
|
---|
| 251 | // try collection first
|
---|
| 252 | File stylesheet = null;
|
---|
| 253 | if (!collection.equals(""))
|
---|
| 254 | {
|
---|
| 255 |
|
---|
| 256 | String coll_home = collectionBaseDir(site_home, collection);
|
---|
| 257 | stylesheet = new File(coll_home + File.separatorChar + "transform" + File.separatorChar + filename);
|
---|
| 258 | if (stylesheet.exists())
|
---|
| 259 | {
|
---|
| 260 | return stylesheet.getPath();
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | // try site one next
|
---|
| 265 | stylesheet = new File(site_home + File.separatorChar + "transform" + File.separatorChar + filename);
|
---|
| 266 | if (stylesheet.exists())
|
---|
| 267 | {
|
---|
| 268 | return stylesheet.getPath();
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | // try current interface
|
---|
| 272 | String interface_home = interfaceHome(gsdl3_home, interface_name);
|
---|
| 273 | stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
|
---|
| 274 | if (stylesheet.exists())
|
---|
| 275 | {
|
---|
| 276 | return stylesheet.getPath();
|
---|
| 277 | }
|
---|
| 278 | // try base interface
|
---|
| 279 | if (base_interfaces == null || base_interfaces.size() == 0)
|
---|
| 280 | {
|
---|
| 281 | return null; // no base interfaces to look for
|
---|
| 282 | }
|
---|
| 283 | for (int i = 0; i < base_interfaces.size(); i++)
|
---|
| 284 | {
|
---|
[25635] | 285 | interface_home = interfaceHome(gsdl3_home, base_interfaces.get(i));
|
---|
[24509] | 286 | stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
|
---|
| 287 | if (stylesheet.exists())
|
---|
| 288 | {
|
---|
| 289 | return stylesheet.getPath();
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | // still can't find it and we have looked everywhere
|
---|
| 294 | return null;
|
---|
| 295 | }
|
---|
[24859] | 296 |
|
---|
[25635] | 297 | static public ArrayList<File> getStylesheetFiles(String gsdl3_home, String site_name, String collection, String interface_name, ArrayList<String> base_interfaces, String filename)
|
---|
[24509] | 298 | {
|
---|
| 299 | ArrayList<File> stylesheets = new ArrayList<File>();
|
---|
| 300 | String site_home = siteHome(gsdl3_home, site_name);
|
---|
| 301 | // try collection first
|
---|
| 302 | File stylesheet = null;
|
---|
| 303 | if (!collection.equals(""))
|
---|
| 304 | {
|
---|
| 305 | String coll_home = collectionBaseDir(site_home, collection);
|
---|
| 306 | stylesheet = new File(coll_home + File.separatorChar + "transform" + File.separatorChar + filename);
|
---|
| 307 | if (stylesheet.exists())
|
---|
| 308 | {
|
---|
| 309 | stylesheets.add(stylesheet);
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
[3222] | 312 |
|
---|
[24509] | 313 | // try site one next
|
---|
| 314 | stylesheet = new File(site_home + File.separatorChar + "transform" + File.separatorChar + filename);
|
---|
| 315 | if (stylesheet.exists())
|
---|
| 316 | {
|
---|
| 317 | stylesheets.add(stylesheet);
|
---|
| 318 | }
|
---|
[3440] | 319 |
|
---|
[24509] | 320 | // try current interface
|
---|
| 321 | String interface_home = interfaceHome(gsdl3_home, interface_name);
|
---|
| 322 | stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
|
---|
| 323 | if (stylesheet.exists())
|
---|
| 324 | {
|
---|
| 325 | stylesheets.add(stylesheet);
|
---|
| 326 | }
|
---|
| 327 | // try base interface
|
---|
| 328 | if (base_interfaces != null && base_interfaces.size() != 0)
|
---|
| 329 | {
|
---|
| 330 | for (int i = 0; i < base_interfaces.size(); i++)
|
---|
| 331 | {
|
---|
[25635] | 332 | interface_home = interfaceHome(gsdl3_home, base_interfaces.get(i));
|
---|
[24509] | 333 | stylesheet = new File(interface_home + File.separatorChar + "transform" + File.separatorChar + filename);
|
---|
| 334 | if (stylesheet.exists())
|
---|
| 335 | {
|
---|
| 336 | stylesheets.add(stylesheet);
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
[3284] | 340 |
|
---|
[24509] | 341 | return stylesheets;
|
---|
| 342 | }
|
---|
[3477] | 343 |
|
---|
[24509] | 344 | /** base directory for phind data */
|
---|
| 345 | public static String phindBaseDir(String site_home, String coll_name, String phind_index)
|
---|
| 346 | {
|
---|
| 347 | return site_home + File.separatorChar + "collect" + File.separatorChar + coll_name + File.separatorChar + "index" + File.separatorChar + "phind" + phind_index;
|
---|
| 348 | }
|
---|
[3477] | 349 |
|
---|
[24509] | 350 | /** the collection database file - */
|
---|
| 351 | static public String collectionDatabaseFile(String site_home, String collection_name, String index_stem, String database_type)
|
---|
| 352 | {
|
---|
| 353 |
|
---|
| 354 | String db_ext = null;
|
---|
| 355 | if (database_type.equalsIgnoreCase("jdbm"))
|
---|
| 356 | {
|
---|
| 357 | db_ext = ".jdb";
|
---|
| 358 | }
|
---|
| 359 | else
|
---|
| 360 | {
|
---|
| 361 | // assume gdbm
|
---|
| 362 | db_ext = ".gdb";
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | return site_home + File.separatorChar + "collect" + File.separatorChar + collection_name + File.separatorChar + "index" + File.separatorChar + "text" + File.separatorChar + index_stem + db_ext;
|
---|
| 366 |
|
---|
[3477] | 367 | }
|
---|
[24509] | 368 |
|
---|
| 369 | // some file utility methods
|
---|
| 370 |
|
---|
| 371 | /**
|
---|
| 372 | * read in a file and encode it using base64 encoded data returned as a
|
---|
| 373 | * String
|
---|
| 374 | */
|
---|
| 375 | static public String base64EncodeFromFile(String in_filename)
|
---|
| 376 | {
|
---|
| 377 | byte[] data = null;
|
---|
| 378 | try
|
---|
| 379 | {
|
---|
| 380 | data = readFile(in_filename);
|
---|
| 381 | }
|
---|
| 382 | catch (Exception e)
|
---|
| 383 | {
|
---|
| 384 | logger.error("couldn't read the file");
|
---|
| 385 | }
|
---|
| 386 | String encodedString = Base64.encode(data);
|
---|
| 387 | return encodedString;
|
---|
| 388 |
|
---|
[5961] | 389 | }
|
---|
[24509] | 390 |
|
---|
| 391 | /** decode some base64 data, and write it to the specified file */
|
---|
| 392 | static public boolean base64DecodeToFile(String data, String out_filename)
|
---|
| 393 | {
|
---|
| 394 | try
|
---|
| 395 | {
|
---|
| 396 | byte[] buffer = Base64.decode(data);
|
---|
| 397 | writeFile(buffer, out_filename);
|
---|
| 398 |
|
---|
| 399 | }
|
---|
| 400 | catch (Exception e)
|
---|
| 401 | {
|
---|
| 402 | logger.error("file opening/closing errors" + e.getMessage());
|
---|
| 403 | return false;
|
---|
| 404 | }
|
---|
| 405 | return true;
|
---|
| 406 |
|
---|
[5961] | 407 | }
|
---|
| 408 |
|
---|
[24509] | 409 | /** read in a file to a byte array */
|
---|
| 410 | public static byte[] readFile(String filename) throws IOException
|
---|
| 411 | {
|
---|
| 412 | File file = new File(filename);
|
---|
| 413 | BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
---|
| 414 | int bytes = (int) file.length();
|
---|
| 415 | byte[] buffer = new byte[bytes];
|
---|
| 416 | int readBytes = bis.read(buffer);
|
---|
| 417 | bis.close();
|
---|
| 418 | return buffer;
|
---|
| 419 | }
|
---|
[3477] | 420 |
|
---|
[24509] | 421 | /** write a byte array to a file */
|
---|
| 422 | public static void writeFile(byte[] buffer, String filename) throws IOException
|
---|
| 423 | {
|
---|
| 424 | File file = new File(filename);
|
---|
| 425 | BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
|
---|
| 426 | bos.write(buffer);
|
---|
| 427 | bos.close();
|
---|
[3477] | 428 | }
|
---|
| 429 |
|
---|
[24509] | 430 | public static boolean deleteFile(File f)
|
---|
| 431 | {
|
---|
[3477] | 432 |
|
---|
[24509] | 433 | if (f.isDirectory())
|
---|
| 434 | {
|
---|
| 435 | File[] files = f.listFiles();
|
---|
| 436 | for (int i = 0; files != null && i < files.length; i++)
|
---|
| 437 | {
|
---|
| 438 | deleteFile(files[i]);
|
---|
| 439 | }
|
---|
| 440 |
|
---|
[3477] | 441 | }
|
---|
[24509] | 442 | // delete the file or directory
|
---|
| 443 | return f.delete();
|
---|
[3477] | 444 | }
|
---|
[24509] | 445 |
|
---|
| 446 | public static boolean copyFile(File source, File destination)
|
---|
| 447 | {
|
---|
| 448 | if (!source.isFile())
|
---|
| 449 | {
|
---|
| 450 | logger.error(source.getPath() + " is not a file!");
|
---|
| 451 | return false;
|
---|
| 452 | }
|
---|
| 453 | try
|
---|
| 454 | {
|
---|
| 455 | destination.getParentFile().mkdirs();
|
---|
| 456 | FileInputStream in = new FileInputStream(source);
|
---|
| 457 | FileOutputStream out = new FileOutputStream(destination);
|
---|
| 458 | int value = 0;
|
---|
| 459 | while ((value = in.read()) != -1)
|
---|
| 460 | {
|
---|
| 461 | out.write(value);
|
---|
| 462 | }
|
---|
| 463 | in.close();
|
---|
| 464 | out.close();
|
---|
| 465 | }
|
---|
| 466 | catch (Exception e)
|
---|
| 467 | {
|
---|
| 468 | logger.error("something went wrong copying " + source.getPath() + " to " + destination.getPath());
|
---|
| 469 | logger.error("Exception: " + e.getMessage());
|
---|
| 470 | return false;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | return true;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | /**
|
---|
| 477 | * Recursively moves the contents of source file to destination, maintaining
|
---|
| 478 | * paths.
|
---|
| 479 | *
|
---|
| 480 | * @param source
|
---|
| 481 | * A File representing the directory whose contents you wish to
|
---|
| 482 | * move.
|
---|
| 483 | * @param destination
|
---|
| 484 | * A File representing the directory you wish to move files to.
|
---|
| 485 | * @return true if successful
|
---|
| 486 | */
|
---|
| 487 | public static boolean moveDirectory(File source, File destination)
|
---|
| 488 | {
|
---|
| 489 |
|
---|
| 490 | // first try rename
|
---|
| 491 | if (source.renameTo(destination))
|
---|
| 492 | {
|
---|
| 493 | return true;
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | // john T. said that this sometimes doesn't work, so you have to copy files manually
|
---|
| 497 |
|
---|
| 498 | // copied from gatherer Utility class
|
---|
| 499 | File input[] = source.listFiles();
|
---|
| 500 | for (int i = 0; i < input.length; i++)
|
---|
| 501 | {
|
---|
| 502 | File output = new File(destination, input[i].getName());
|
---|
| 503 | if (input[i].isDirectory())
|
---|
| 504 | {
|
---|
| 505 | moveDirectory(input[i], output);
|
---|
| 506 | }
|
---|
| 507 | else
|
---|
| 508 | {
|
---|
| 509 | // Copy the file
|
---|
| 510 | try
|
---|
| 511 | {
|
---|
| 512 | output.getParentFile().mkdirs();
|
---|
| 513 | FileInputStream in = new FileInputStream(input[i]);
|
---|
| 514 | FileOutputStream out = new FileOutputStream(output);
|
---|
[24859] | 515 |
|
---|
| 516 | FileChannel inC = in.getChannel();
|
---|
| 517 | FileChannel outC = out.getChannel();
|
---|
| 518 |
|
---|
| 519 | System.err.println(inC.transferTo(0, inC.size(), outC));
|
---|
| 520 |
|
---|
[24509] | 521 | in.close();
|
---|
| 522 | out.close();
|
---|
[24859] | 523 |
|
---|
[24509] | 524 | // Delete file
|
---|
| 525 | input[i].delete();
|
---|
| 526 | }
|
---|
| 527 | catch (Exception e)
|
---|
| 528 | {
|
---|
| 529 | logger.error("exception: " + e.getMessage());
|
---|
| 530 | return false;
|
---|
| 531 | }
|
---|
| 532 |
|
---|
| 533 | }
|
---|
| 534 | }
|
---|
| 535 | return true;
|
---|
| 536 | }
|
---|
| 537 |
|
---|
[26026] | 538 | public static ArrayList<File> getAllXSLFiles(String interfaceName, String siteName)
|
---|
| 539 | {
|
---|
| 540 | ArrayList<File> filesToReturn = new ArrayList<File>();
|
---|
| 541 |
|
---|
| 542 | File siteCollectionDir = new File(GSFile.siteHome(GlobalProperties.getGSDL3Home(), siteName) + File.separator + "collect");
|
---|
| 543 | if(siteCollectionDir.exists() && siteCollectionDir.isDirectory())
|
---|
| 544 | {
|
---|
| 545 | File[] collections = siteCollectionDir.listFiles();
|
---|
| 546 |
|
---|
| 547 | for(File collection : collections)
|
---|
| 548 | {
|
---|
| 549 | if(collection.isDirectory())
|
---|
| 550 | {
|
---|
| 551 | File collectionTranformDir = new File(collection.getAbsolutePath() + File.separator + "transform");
|
---|
| 552 | if(collectionTranformDir.exists() && collectionTranformDir.isDirectory())
|
---|
| 553 | {
|
---|
| 554 | filesToReturn.addAll(getXSLFilesFromDirectoryRecursive(collectionTranformDir));
|
---|
| 555 | }
|
---|
| 556 | }
|
---|
| 557 | }
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | filesToReturn.addAll(getXSLFilesFromDirectoryRecursive(new File(GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), interfaceName) + File.separator + "transform")));
|
---|
| 561 |
|
---|
| 562 | return filesToReturn;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | protected static ArrayList<File> getXSLFilesFromDirectoryRecursive(File directory)
|
---|
| 566 | {
|
---|
| 567 | ArrayList<File> filesToReturn = new ArrayList<File>();
|
---|
| 568 |
|
---|
| 569 | if(!directory.isDirectory())
|
---|
| 570 | {
|
---|
| 571 | return filesToReturn;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | File[] currentFiles = directory.listFiles();
|
---|
| 575 | for(File current : currentFiles)
|
---|
| 576 | {
|
---|
| 577 | if(current.isDirectory())
|
---|
| 578 | {
|
---|
| 579 | filesToReturn.addAll(GSFile.getXSLFilesFromDirectoryRecursive(current));
|
---|
| 580 | }
|
---|
| 581 | else if(current.getName().endsWith(".xsl"))
|
---|
| 582 | {
|
---|
| 583 | filesToReturn.add(current);
|
---|
| 584 | }
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | return filesToReturn;
|
---|
| 588 | }
|
---|
[3222] | 589 | }
|
---|