KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jdepend > framework > JavaClassBuilder


1 package jdepend.framework;
2
3 import java.io.*;
4 import java.util.*;
5 import java.util.jar.*;
6 import java.util.zip.*;
7
8 /**
9  * The <code>JavaClassBuilder</code> builds <code>JavaClass</code>
10  * instances from .class, .jar, .war, or .zip files.
11  *
12  * @author <b>Mike Clark</b>
13  * @author Clarkware Consulting, Inc.
14  */

15
16 public class JavaClassBuilder {
17
18     private AbstractParser parser;
19     private FileManager fileManager;
20
21     
22     public JavaClassBuilder() {
23         this(new ClassFileParser(), new FileManager());
24     }
25
26     public JavaClassBuilder(FileManager fm) {
27         this(new ClassFileParser(), fm);
28     }
29
30     public JavaClassBuilder(AbstractParser parser, FileManager fm) {
31         this.parser = parser;
32         this.fileManager = fm;
33     }
34
35     public int countClasses() {
36         AbstractParser counter = new AbstractParser() {
37
38             public JavaClass parse(InputStream is) {
39                 return new JavaClass("");
40             }
41         };
42
43         JavaClassBuilder builder = new JavaClassBuilder(counter, fileManager);
44         Collection classes = builder.build();
45         return classes.size();
46     }
47
48     /**
49      * Builds the <code>JavaClass</code> instances.
50      *
51      * @return Collection of <code>JavaClass</code> instances.
52      */

53     public Collection build() {
54
55         Collection classes = new ArrayList();
56
57         for (Iterator i = fileManager.extractFiles().iterator(); i.hasNext();) {
58
59             File nextFile = (File)i.next();
60
61             try {
62
63                 classes.addAll(buildClasses(nextFile));
64
65             } catch (IOException ioe) {
66                 System.err.println("\n" + ioe.getMessage());
67             }
68         }
69
70         return classes;
71     }
72
73     /**
74      * Builds the <code>JavaClass</code> instances from the
75      * specified file.
76      *
77      * @param file Class or Jar file.
78      * @return Collection of <code>JavaClass</code> instances.
79      */

80     public Collection buildClasses(File file) throws IOException {
81
82         if (fileManager.acceptClassFile(file)) {
83             FileInputStream fis = null;
84             try {
85                 fis = new FileInputStream(file);
86                 JavaClass parsedClass = parser.parse(fis);
87                 Collection javaClasses = new ArrayList();
88                 javaClasses.add(parsedClass);
89                 return javaClasses;
90             } finally {
91                 if (fis != null) {
92                     fis.close();
93                 }
94             }
95         } else if (fileManager.acceptJarFile(file)) {
96
97             JarFile jarFile = new JarFile(file);
98             Collection result = buildClasses(jarFile);
99             jarFile.close();
100             return result;
101
102         } else {
103             throw new IOException("File is not a valid " +
104                 ".class, .jar, .war, or .zip file: " +
105                 file.getPath());
106         }
107     }
108
109     /**
110      * Builds the <code>JavaClass</code> instances from the specified
111      * jar, war, or zip file.
112      *
113      * @param file Jar, war, or zip file.
114      * @return Collection of <code>JavaClass</code> instances.
115      */

116     public Collection buildClasses(JarFile file) throws IOException {
117
118         Collection javaClasses = new ArrayList();
119
120         Enumeration entries = file.entries();
121         while (entries.hasMoreElements()) {
122             ZipEntry e = (ZipEntry) entries.nextElement();
123             if (fileManager.acceptClassFileName(e.getName())) {
124                 InputStream is = null;
125                 try {
126                     is = file.getInputStream(e);
127                     JavaClass jc = parser.parse(is);
128                     javaClasses.add(jc);
129                 } finally {
130                     is.close();
131                 }
132             }
133         }
134
135         return javaClasses;
136     }
137 }
138
Popular Tags