1 18 package org.apache.tools.ant.taskdefs.optional.depend; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.Enumeration ; 25 import java.util.Hashtable ; 26 import java.util.Vector ; 27 import java.util.zip.ZipEntry ; 28 import java.util.zip.ZipFile ; 29 import org.apache.tools.ant.util.depend.AbstractAnalyzer; 30 31 36 public class AntAnalyzer extends AbstractAnalyzer { 37 40 public AntAnalyzer() { 41 } 42 43 51 protected void determineDependencies(Vector files, Vector classes) { 52 Hashtable dependencies = new Hashtable (); 55 Hashtable containers = new Hashtable (); 56 Hashtable toAnalyze = new Hashtable (); 57 for (Enumeration e = getRootClasses(); e.hasMoreElements();) { 58 String classname = (String ) e.nextElement(); 59 toAnalyze.put(classname, classname); 60 } 61 62 int count = 0; 63 int maxCount = isClosureRequired() ? MAX_LOOPS : 1; 64 Hashtable analyzedDeps = null; 65 while (toAnalyze.size() != 0 && count++ < maxCount) { 66 analyzedDeps = new Hashtable (); 67 for (Enumeration e = toAnalyze.keys(); e.hasMoreElements();) { 68 String classname = (String ) e.nextElement(); 69 dependencies.put(classname, classname); 70 try { 71 File container = getClassContainer(classname); 72 if (container == null) { 73 continue; 74 } 75 containers.put(container, container); 76 77 ZipFile zipFile = null; 78 InputStream inStream = null; 79 try { 80 if (container.getName().endsWith(".class")) { 81 inStream = new FileInputStream (container.getPath()); 82 } else { 83 zipFile = new ZipFile (container.getPath()); 84 String entryName 85 = classname.replace('.', '/') + ".class"; 86 ZipEntry entry = new ZipEntry (entryName); 87 inStream 88 = zipFile.getInputStream(entry); 89 } 90 ClassFile classFile = new ClassFile(); 91 classFile.read(inStream); 92 Vector dependencyList = classFile.getClassRefs(); 93 Enumeration depEnum = dependencyList.elements(); 94 while (depEnum.hasMoreElements()) { 95 String dependency = (String ) depEnum.nextElement(); 96 analyzedDeps.put(dependency, dependency); 97 } 98 } finally { 99 if (inStream != null) { 100 inStream.close(); 101 } 102 if (zipFile != null) { 103 zipFile.close(); 104 } 105 } 106 } catch (IOException ioe) { 107 } 109 } 110 111 toAnalyze.clear(); 112 113 Enumeration depsEnum = analyzedDeps.elements(); 115 while (depsEnum.hasMoreElements()) { 116 String className = (String ) depsEnum.nextElement(); 117 if (!dependencies.containsKey(className)) { 118 toAnalyze.put(className, className); 119 } 120 } 121 } 122 123 Enumeration depsEnum = analyzedDeps.elements(); 125 while (depsEnum.hasMoreElements()) { 126 String className = (String ) depsEnum.nextElement(); 127 dependencies.put(className, className); 128 } 129 130 files.removeAllElements(); 131 for (Enumeration e = containers.keys(); e.hasMoreElements();) { 132 files.addElement((File ) e.nextElement()); 133 } 134 135 classes.removeAllElements(); 136 for (Enumeration e = dependencies.keys(); e.hasMoreElements();) { 137 classes.addElement((String ) e.nextElement()); 138 } 139 } 140 141 146 protected boolean supportsFileDependencies() { 147 return true; 148 } 149 150 } 151 152 | Popular Tags |