001: package jdepend.framework;
002:
003: import java.io.*;
004: import java.util.*;
005: import java.util.jar.*;
006: import java.util.zip.*;
007:
008: /**
009: * The <code>JavaClassBuilder</code> builds <code>JavaClass</code>
010: * instances from .class, .jar, .war, or .zip files.
011: *
012: * @author <b>Mike Clark</b>
013: * @author Clarkware Consulting, Inc.
014: */
015:
016: public class JavaClassBuilder {
017:
018: private AbstractParser parser;
019: private FileManager fileManager;
020:
021: public JavaClassBuilder() {
022: this (new ClassFileParser(), new FileManager());
023: }
024:
025: public JavaClassBuilder(FileManager fm) {
026: this (new ClassFileParser(), fm);
027: }
028:
029: public JavaClassBuilder(AbstractParser parser, FileManager fm) {
030: this .parser = parser;
031: this .fileManager = fm;
032: }
033:
034: public int countClasses() {
035: AbstractParser counter = new AbstractParser() {
036:
037: public JavaClass parse(InputStream is) {
038: return new JavaClass("");
039: }
040: };
041:
042: JavaClassBuilder builder = new JavaClassBuilder(counter,
043: fileManager);
044: Collection classes = builder.build();
045: return classes.size();
046: }
047:
048: /**
049: * Builds the <code>JavaClass</code> instances.
050: *
051: * @return Collection of <code>JavaClass</code> instances.
052: */
053: public Collection build() {
054:
055: Collection classes = new ArrayList();
056:
057: for (Iterator i = fileManager.extractFiles().iterator(); i
058: .hasNext();) {
059:
060: File nextFile = (File) i.next();
061:
062: try {
063:
064: classes.addAll(buildClasses(nextFile));
065:
066: } catch (IOException ioe) {
067: System.err.println("\n" + ioe.getMessage());
068: }
069: }
070:
071: return classes;
072: }
073:
074: /**
075: * Builds the <code>JavaClass</code> instances from the
076: * specified file.
077: *
078: * @param file Class or Jar file.
079: * @return Collection of <code>JavaClass</code> instances.
080: */
081: public Collection buildClasses(File file) throws IOException {
082:
083: if (fileManager.acceptClassFile(file)) {
084: FileInputStream fis = null;
085: try {
086: fis = new FileInputStream(file);
087: JavaClass parsedClass = parser.parse(fis);
088: Collection javaClasses = new ArrayList();
089: javaClasses.add(parsedClass);
090: return javaClasses;
091: } finally {
092: if (fis != null) {
093: fis.close();
094: }
095: }
096: } else if (fileManager.acceptJarFile(file)) {
097:
098: JarFile jarFile = new JarFile(file);
099: Collection result = buildClasses(jarFile);
100: jarFile.close();
101: return result;
102:
103: } else {
104: throw new IOException("File is not a valid "
105: + ".class, .jar, .war, or .zip file: "
106: + file.getPath());
107: }
108: }
109:
110: /**
111: * Builds the <code>JavaClass</code> instances from the specified
112: * jar, war, or zip file.
113: *
114: * @param file Jar, war, or zip file.
115: * @return Collection of <code>JavaClass</code> instances.
116: */
117: public Collection buildClasses(JarFile file) throws IOException {
118:
119: Collection javaClasses = new ArrayList();
120:
121: Enumeration entries = file.entries();
122: while (entries.hasMoreElements()) {
123: ZipEntry e = (ZipEntry) entries.nextElement();
124: if (fileManager.acceptClassFileName(e.getName())) {
125: InputStream is = null;
126: try {
127: is = file.getInputStream(e);
128: JavaClass jc = parser.parse(is);
129: javaClasses.add(jc);
130: } finally {
131: is.close();
132: }
133: }
134: }
135:
136: return javaClasses;
137: }
138: }
|