KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > depend > AntAnalyzer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.depend;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Vector JavaDoc;
27 import java.util.zip.ZipEntry JavaDoc;
28 import java.util.zip.ZipFile JavaDoc;
29 import org.apache.tools.ant.util.depend.AbstractAnalyzer;
30
31 /**
32  * An analyzer which uses the depend task's bytecode classes to analyze
33  * dependencies
34  *
35  */

36 public class AntAnalyzer extends AbstractAnalyzer {
37     /**
38      * Default constructor
39      */

40     public AntAnalyzer() {
41     }
42
43     /**
44      * Determine the dependencies of the configured root classes.
45      *
46      * @param files a vector to be populated with the files which contain
47      * the dependency classes
48      * @param classes a vector to be populated with the names of the
49      * dependency classes.
50      */

51     protected void determineDependencies(Vector JavaDoc files, Vector JavaDoc classes) {
52         // we get the root classes and build up a set of
53
// classes upon which they depend
54
Hashtable JavaDoc dependencies = new Hashtable JavaDoc();
55         Hashtable JavaDoc containers = new Hashtable JavaDoc();
56         Hashtable JavaDoc toAnalyze = new Hashtable JavaDoc();
57         for (Enumeration JavaDoc e = getRootClasses(); e.hasMoreElements();) {
58             String JavaDoc classname = (String JavaDoc) e.nextElement();
59             toAnalyze.put(classname, classname);
60         }
61
62         int count = 0;
63         int maxCount = isClosureRequired() ? MAX_LOOPS : 1;
64         Hashtable JavaDoc analyzedDeps = null;
65         while (toAnalyze.size() != 0 && count++ < maxCount) {
66             analyzedDeps = new Hashtable JavaDoc();
67             for (Enumeration JavaDoc e = toAnalyze.keys(); e.hasMoreElements();) {
68                 String JavaDoc classname = (String JavaDoc) e.nextElement();
69                 dependencies.put(classname, classname);
70                 try {
71                     File JavaDoc container = getClassContainer(classname);
72                     if (container == null) {
73                         continue;
74                     }
75                     containers.put(container, container);
76
77                     ZipFile JavaDoc zipFile = null;
78                     InputStream JavaDoc inStream = null;
79                     try {
80                         if (container.getName().endsWith(".class")) {
81                             inStream = new FileInputStream JavaDoc(container.getPath());
82                         } else {
83                             zipFile = new ZipFile JavaDoc(container.getPath());
84                             String JavaDoc entryName
85                                 = classname.replace('.', '/') + ".class";
86                             ZipEntry JavaDoc entry = new ZipEntry JavaDoc(entryName);
87                             inStream
88                                 = zipFile.getInputStream(entry);
89                         }
90                         ClassFile classFile = new ClassFile();
91                         classFile.read(inStream);
92                         Vector JavaDoc dependencyList = classFile.getClassRefs();
93                         Enumeration JavaDoc depEnum = dependencyList.elements();
94                         while (depEnum.hasMoreElements()) {
95                             String JavaDoc dependency = (String JavaDoc) 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 JavaDoc ioe) {
107                     // ignore
108
}
109             }
110
111             toAnalyze.clear();
112
113             // now recover all the dependencies collected and add to the list.
114
Enumeration JavaDoc depsEnum = analyzedDeps.elements();
115             while (depsEnum.hasMoreElements()) {
116                 String JavaDoc className = (String JavaDoc) depsEnum.nextElement();
117                 if (!dependencies.containsKey(className)) {
118                     toAnalyze.put(className, className);
119                 }
120             }
121         }
122
123         // pick up the last round of dependencies that were determined
124
Enumeration JavaDoc depsEnum = analyzedDeps.elements();
125         while (depsEnum.hasMoreElements()) {
126             String JavaDoc className = (String JavaDoc) depsEnum.nextElement();
127             dependencies.put(className, className);
128         }
129
130         files.removeAllElements();
131         for (Enumeration JavaDoc e = containers.keys(); e.hasMoreElements();) {
132             files.addElement((File JavaDoc) e.nextElement());
133         }
134
135         classes.removeAllElements();
136         for (Enumeration JavaDoc e = dependencies.keys(); e.hasMoreElements();) {
137             classes.addElement((String JavaDoc) e.nextElement());
138         }
139     }
140
141     /**
142      * Indicate if this analyzer can determine dependent files.
143      *
144      * @return true if the analyzer provides dependency file information.
145      */

146     protected boolean supportsFileDependencies() {
147         return true;
148     }
149
150 }
151
152
Popular Tags