1 10 package org.jgap.util; 11 12 import java.io.*; 13 import java.util.jar.*; 14 import java.net.*; 15 import java.util.*; 16 17 public class FileKit { 18 19 private final static String CVS_REVISION = "$Revision: 1.6 $"; 20 21 public static String fileseparator = System.getProperty("file.separator"); 22 23 35 public static void copyFile(String source, String dest) 36 throws FileNotFoundException, IOException { 37 copyFile(source, dest, 0); 38 } 39 40 53 public static void copyFile(String source, String dest, int a_offset) 54 throws FileNotFoundException, IOException { 55 if (getFilename(dest).length() == 0) { 56 dest = dest + getFilename(source); 57 } 58 File inputFile = new File(source); 59 File outputFile = new File(dest); 60 FileInputStream in; 61 FileOutputStream out; 62 in = new FileInputStream(inputFile); 63 out = new FileOutputStream(outputFile); 64 int c; 65 int currentOffset = 0; 66 while ( (c = in.read()) != -1) { 67 if (currentOffset >= a_offset) { 68 out.write(c); 69 } 70 currentOffset++; 71 } 72 in.close(); 73 out.close(); 74 } 75 76 public static String getFilename(String name_and_path) { 77 return getFilename(name_and_path, fileseparator); 78 } 79 80 90 public static String getFilename(String name_and_path, 91 String fileseparator) { 92 93 if (name_and_path == null) { 94 return ""; 95 } 96 String s = name_and_path; 97 if (fileseparator.equals("/")) { 99 s = s.replace('\\', '/'); 100 } 101 else { 102 s = s.replace('/', '\\'); 103 } 104 int p = s.lastIndexOf(fileseparator); 105 if (p < 0) { 106 return s; 108 } 109 else { 110 s = s.substring(p + 1); 111 if (s == null) { 112 s = ""; 113 } 114 return s; 115 } 116 } 117 118 125 public static String getCurrentDir() 126 throws IOException { 127 File file = new File("."); 128 return file.getCanonicalPath(); 129 } 130 131 142 public static String addSubDir(String dir, String subDir, boolean makeNice) { 143 File f = new File(getConformPath(dir, true), subDir); 144 String s = getConformPath(f.getAbsolutePath(), makeNice); 145 return s; 146 } 147 148 public static String getConformPath(String path, boolean makeNice) { 149 if (makeNice) { 150 return getNiceURL(getConformPath(path), fileseparator); 151 } 152 else { 153 return getConformPath(path); 154 } 155 } 156 157 public static String getConformPath(String path) { 158 return getConformPath(path,fileseparator); 159 } 160 161 public static String getConformPath(String path, String a_fileseparator) { 162 String result = path; 163 if (a_fileseparator.equals("/")) { 164 result = removeDoubleSeparators(path.replace('\\', '/')); 165 } 166 else { 167 result = removeDoubleSeparators(path.replace('/', '\\')); 168 } 169 if (!result.endsWith(a_fileseparator)) { 170 result += a_fileseparator; 171 } 172 return result; 173 } 174 175 185 public static String getNiceURL(String url, String separator) { 186 if (url == null) { 187 return null; 188 } 189 if (url.length() == 0) { 190 return separator; 191 } 192 int p = url.lastIndexOf(separator); 193 if (p < url.length() - 1) { 194 return removeDoubleSeparators(url + separator); 195 } 196 else { 197 return removeDoubleSeparators(url); 198 } 199 } 200 201 210 public static String removeDoubleSeparators(String dir) { 211 int p; 212 do { 213 String sep; 214 if (fileseparator.length() > 1) { 215 sep = fileseparator; 216 } 217 else { 218 sep = fileseparator + fileseparator; 219 } 220 p = dir.lastIndexOf(sep); 221 if (p >= 0) { 222 dir = dir.substring(0, p) + dir.substring(p + 1); 223 } 224 else { 225 break; 226 } 227 } while (true); 228 return dir; 229 } 230 231 public static boolean directoryExists(String a_dir) { 232 File f = new File(a_dir); 233 return f.exists(); 234 } 235 236 public static boolean existsFile(String a_filename) { 237 File file = new File(getConformPath(a_filename)); 238 return file.exists(); 239 } 240 241 247 public static boolean deleteFile(String a_filename) { 248 File file = new File(getConformPath(a_filename)); 249 if (file.exists()) { 250 return file.delete(); 251 } 252 else { 253 return false; 254 } 255 } 256 257 270 public static ClassLoader loadJar(String a_filename) 271 throws Exception { 272 JarClassLoader cl = new JarClassLoader(a_filename); 274 return cl; 275 } 276 277 287 public static Manifest getManifestOfJar(String a_filename) 288 throws Exception { 289 URL url = new URL("jar:file:" + a_filename + "!/"); 290 JarURLConnection jarConnection = (JarURLConnection) url.openConnection(); 291 Manifest manifest = jarConnection.getManifest(); 292 jarConnection.getJarFile().close(); 295 return manifest; 296 } 297 298 306 public static String getJGAPVersion(Manifest a_JGAPManifest) { 307 Attributes attr = a_JGAPManifest.getMainAttributes(); 308 return attr.getValue("JGAP-Version"); 309 } 310 311 318 public static String getModuleVersion(Manifest a_JGAPManifest) { 319 Attributes attr = a_JGAPManifest.getMainAttributes(); 320 return attr.getValue("Module-Version"); 321 } 322 323 333 public static String getVersionOfModule(String a_filename) 334 throws Exception { 335 Manifest mf = getManifestOfJar(a_filename); 336 String version = getModuleVersion(mf); 337 if (version == null) { 338 version = "no version info found!"; 339 } 340 return version; 341 } 342 343 354 public static String getVersionOfJGAP(String a_filename) 355 throws Exception { 356 Manifest mf = getManifestOfJar(a_filename); 357 String version = getJGAPVersion(mf); 358 if (version == null) { 359 version = "no version info found!"; 360 } 361 return version; 362 } 363 364 374 public static String toJarFileName(String a_filename) { 375 String result = a_filename.replace('\\', '/'); 376 return result; 380 } 381 382 391 public static void createDirectory(String a_dirname) 392 throws IOException { 393 File file = new File(a_dirname); 394 if (file.exists()) { 395 return; 396 } 397 if (!file.mkdirs()) { 398 throw new IOException("Directory " + a_dirname + 399 " could not be created!"); 400 } 401 } 402 403 413 public static Vector readFile(String a_filename) 414 throws IOException { 415 416 Vector v = new Vector(); 417 String thisLine; 418 FileInputStream fin = new FileInputStream(a_filename); 419 BufferedReader myInput = new BufferedReader 420 (new InputStreamReader(fin)); 421 while ( (thisLine = myInput.readLine()) != null) { 422 v.add(thisLine); 423 } 424 return (v); 425 } 426 427 } 428 | Popular Tags |