1 18 package org.apache.tools.ant.util.depend; 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Enumeration ; 22 import java.util.Vector ; 23 import java.util.zip.ZipFile ; 24 import org.apache.tools.ant.types.Path; 25 26 31 public abstract class AbstractAnalyzer implements DependencyAnalyzer { 32 33 public static final int MAX_LOOPS = 1000; 34 35 36 private Path sourcePath = new Path(null); 37 38 39 private Path classPath = new Path(null); 40 41 42 private Vector rootClasses = new Vector (); 43 44 45 private boolean determined = false; 46 47 48 private Vector fileDependencies; 49 50 private Vector classDependencies; 51 52 53 private boolean closure = true; 54 55 56 protected AbstractAnalyzer() { 57 reset(); 58 } 59 60 68 public void setClosure(boolean closure) { 69 this.closure = closure; 70 } 71 72 79 public Enumeration getFileDependencies() { 80 if (!supportsFileDependencies()) { 81 throw new RuntimeException ("File dependencies are not supported " 82 + "by this analyzer"); 83 } 84 if (!determined) { 85 determineDependencies(fileDependencies, classDependencies); 86 } 87 return fileDependencies.elements(); 88 } 89 90 97 public Enumeration getClassDependencies() { 98 if (!determined) { 99 determineDependencies(fileDependencies, classDependencies); 100 } 101 return classDependencies.elements(); 102 } 103 104 112 public File getClassContainer(String classname) throws IOException { 113 String classLocation = classname.replace('.', '/') + ".class"; 114 return getResourceContainer(classLocation, classPath.list()); 117 } 118 119 127 public File getSourceContainer(String classname) throws IOException { 128 String sourceLocation = classname.replace('.', '/') + ".java"; 129 130 return getResourceContainer(sourceLocation, sourcePath.list()); 134 } 135 136 144 public void addSourcePath(Path sourcePath) { 145 if (sourcePath == null) { 146 return; 147 } 148 this.sourcePath.append(sourcePath); 149 this.sourcePath.setProject(sourcePath.getProject()); 150 } 151 152 160 public void addClassPath(Path classPath) { 161 if (classPath == null) { 162 return; 163 } 164 165 this.classPath.append(classPath); 166 this.classPath.setProject(classPath.getProject()); 167 } 168 169 176 public void addRootClass(String className) { 177 if (className == null) { 178 return; 179 } 180 if (!rootClasses.contains(className)) { 181 rootClasses.addElement(className); 182 } 183 } 184 185 192 public void config(String name, Object info) { 193 } 195 196 200 public void reset() { 201 rootClasses.removeAllElements(); 202 determined = false; 203 fileDependencies = new Vector (); 204 classDependencies = new Vector (); 205 } 206 207 213 protected Enumeration getRootClasses() { 214 return rootClasses.elements(); 215 } 216 217 223 protected boolean isClosureRequired() { 224 return closure; 225 } 226 227 235 protected abstract void determineDependencies(Vector files, Vector classes); 236 237 243 protected abstract boolean supportsFileDependencies(); 244 245 254 private File getResourceContainer(String resourceLocation, String [] paths) 255 throws IOException { 256 for (int i = 0; i < paths.length; ++i) { 257 File element = new File (paths[i]); 258 if (!element.exists()) { 259 continue; 260 } 261 if (element.isDirectory()) { 262 File resource = new File (element, resourceLocation); 263 if (resource.exists()) { 264 return resource; 265 } 266 } else { 267 ZipFile zipFile = null; 269 try { 270 zipFile = new ZipFile (element); 271 if (zipFile.getEntry(resourceLocation) != null) { 272 return element; 273 } 274 } finally { 275 if (zipFile != null) { 276 zipFile.close(); 277 } 278 } 279 } 280 } 281 return null; 282 } 283 } 284 285 | Popular Tags |