1 3 package jodd.io.findfile; 4 5 import jodd.util.StringUtil; 6 import jodd.util.Wildcard; 7 8 import java.net.URL ; 9 import java.net.URI ; 10 import java.net.URISyntaxException ; 11 import java.util.zip.ZipFile ; 12 import java.util.zip.ZipEntry ; 13 import java.util.Enumeration ; 14 import java.io.*; 15 16 21 public abstract class FindClass { 22 23 private static final String CLASS_FILE_EXT = ".class"; 24 25 private static final String JAR_FILE_EXT = ".jar"; 26 27 29 33 protected String [] excludedJars = new String [] { 34 "*/jre/lib/*.jar", 35 "*/jre/lib/ext/*.jar", 36 "*/tools.jar", 37 "*/j2ee.jar" 38 }; 39 40 41 public String [] getExcludedJars() { 42 return excludedJars; 43 } 44 45 public void setExcludedJars(String [] excludedJars) { 46 this.excludedJars = excludedJars; 47 } 48 49 53 protected String [] includedJars = null; 54 55 public String [] getIncludedJars() { 56 return includedJars; 57 } 58 59 public void setIncludedJars(String [] includedJars) { 60 this.includedJars = includedJars; 61 } 62 63 65 68 protected String [] includedPackages; 69 70 public String [] getIncludedPackages() { 71 return includedPackages; 72 } 73 74 public void setIncludedPackages(String [] includedPackages) { 75 this.includedPackages = includedPackages; 76 } 77 78 80 83 protected boolean createInputStream = false; 84 85 public FindClass() { 86 } 87 88 93 protected void scanUrl(URL url) throws IOException { 94 File file; 95 try { 96 file = new File (new URI (url.toString())); 97 } catch (URISyntaxException usex) { 98 throw new IOException(usex.toString()); 99 } 100 String pathString = url.toString(); 101 if (StringUtil.endsWithIgnoreCase(pathString, JAR_FILE_EXT) == true) { 102 if (excludedJars != null) { 103 if (Wildcard.matchOne(pathString, excludedJars) != -1) { 104 return; 105 } 106 } 107 if (includedJars != null) { 108 if (Wildcard.matchOne(pathString, includedJars) == -1) { 109 return; 110 } 111 } 112 scanJarFile(new ZipFile (file)); 113 } else if (file.isDirectory() == true) { 114 scanClassPath(file); 115 } 116 } 117 118 123 protected void scanJarFile(ZipFile zipFile) throws IOException { 124 Enumeration entries = zipFile.entries(); 125 while (entries.hasMoreElements()) { 126 ZipEntry zipEntry = (ZipEntry ) entries.nextElement(); 127 String zipEntryName = zipEntry.getName(); 128 if (StringUtil.endsWithIgnoreCase(zipEntryName, CLASS_FILE_EXT)) { 129 scanClassName(zipEntryName, createInputStream == true ? zipFile.getInputStream(zipEntry) : null); 130 } 131 } 132 } 133 134 138 protected void scanClassPath(File root) throws FileNotFoundException { 139 String rootPath = root.getAbsolutePath(); 141 if (rootPath.endsWith(File.separator) == false) { 142 rootPath += File.separatorChar; 143 } 144 145 FindFile ff = new FindFile().excludeDirs().recursive().searchPath(rootPath); 146 File file; 147 while ((file = ff.nextFile()) != null) { 148 String filePath = file.getAbsolutePath(); 149 if (StringUtil.endsWithIgnoreCase(filePath, CLASS_FILE_EXT)) { 150 if (StringUtil.startsWithIgnoreCase(filePath, rootPath) == true) { 151 scanClassName(filePath.substring(rootPath.length()), createInputStream == true ? new FileInputStream(file) : null); 152 } 153 } 154 } 155 } 156 157 158 162 protected void scanClassName(String name, InputStream inputStream) { 163 name = name.substring(0, name.length() - 6); 164 name = StringUtil.replace(name, '/', '.'); 165 name = StringUtil.replace(name, '\\', '.'); 166 167 if (includedPackages != null) { 168 if (Wildcard.matchOne(name, includedPackages) == -1) { 169 return; 170 } 171 } 172 onClassName(name, inputStream); 173 } 174 175 176 178 185 protected abstract void onClassName(String className, InputStream inputStream); 186 } 187 | Popular Tags |