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