View Javadoc
1 /* 2 * Copyright (c) 2001, 2002 The XDoclet team 3 * All rights reserved. 4 */ 5 package xdoclet.util; 6 7 import org.apache.commons.logging.LogFactory; 8 9 import java.io.File; 10 import java.io.FileInputStream; 11 import java.io.FileNotFoundException; 12 import java.io.IOException; 13 import java.io.InputStream; 14 15 import java.util.StringTokenizer; 16 import java.util.jar.JarFile; 17 import java.util.zip.ZipEntry; 18 import java.net.URL; 19 import java.net.URI; 20 import java.net.URISyntaxException; 21 22 /*** 23 * Utility methods for dealing with directories and zip files. 24 * 25 * @author <a href="mailto:aslak.hellesoy at netcom.no">Aslak Hellesøy</a> 26 * @version $Revision: 1.7 $ 27 */ 28 public class FileUtils { 29 /*** 30 * Returns an InputStream for a resource relative to a file. The file can be a directory or a zip/jar file. 31 * The InputStream is the resource located at a relative path from the file or zip/jar file 32 * 33 * @param file directory or zip/jar 34 * @param resourcePath the path of the resource, relative to file. 35 * @return an InputStream for the relative path to the dir or jar, or null if not found. 36 */ 37 public static InputStream getResourceAsStream(File file, String resourcePath) { 38 if (file.isDirectory()) { 39 File descriptorFile = new File(file, resourcePath).getAbsoluteFile(); 40 41 try { 42 return new FileInputStream(descriptorFile); 43 } catch (FileNotFoundException e) { 44 // that's fair. just hand back null 45 return null; 46 } 47 } else { 48 // try to treat it as a zip (jar) file, regardless of its name 49 try { 50 JarFile jar = new JarFile(file); 51 ZipEntry resourceEntry = jar.getEntry(resourcePath); 52 53 if (resourceEntry != null) { 54 return jar.getInputStream(resourceEntry); 55 } 56 } catch (IOException e) { 57 return null; 58 } 59 } 60 61 return null; 62 } 63 64 /*** 65 * Returns true if the file can be read as a zip file. 66 * 67 * @param file the file to test. 68 * @return true if zip. false otherwise. 69 */ 70 public static boolean isJar(File file) { 71 if (file.getName().endsWith(".jar")) { 72 return true; 73 74 /* This is a bit overkill 75 try { 76 new JarFile(file); 77 78 return true; 79 } catch (IOException e) { 80 return false; 81 } 82 */ 83 } else { 84 return false; 85 } 86 } 87 88 /*** 89 * Returns the root directory of the package hierarchy where this class is 90 * located. This will either be a directory or a jar file. 91 * 92 * @return the root directory or jar file where the class is located. 93 */ 94 public static File getRoot(Class clazz) 95 { 96 File dir; 97 URL classURL = clazz.getResource( "/" + clazz.getName().replace( '.', '/' ) + ".class" ); 98 String classFile = classURL.getFile(); 99 100 if( classFile.indexOf('!') != -1 ) { 101 // Sometimes (at least on windows) we get file:F:\bla. Convert to file:/F:/bla 102 if( classFile.charAt(5) != '/' ) { 103 classFile = "file:/" + classFile.substring(5); 104 } 105 classFile = classFile.replace( '//','/' ); 106 String uriSpec = classFile.substring(0, classFile.indexOf('!')); 107 try { 108 dir = new File( new URI( uriSpec ) ); 109 } catch (URISyntaxException e) { 110 LogFactory.getLog(FileUtils.class).error("Couldn't create URI for " + uriSpec, e); 111 throw new IllegalStateException(e.getMessage()); 112 } 113 } else { 114 dir = new File( classFile ).getParentFile(); 115 StringTokenizer st = new StringTokenizer( clazz.getName(), "." ); 116 117 for( int i = 0; i < st.countTokens() - 1; i++ ) 118 { 119 dir = dir.getParentFile(); 120 } 121 } 122 return dir; 123 } 124 }

This page was automatically generated by Maven