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