001: package jdepend.framework;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: /**
007: * The <code>FileManager</code> class is responsible for extracting
008: * Java class files (<code>.class</code> files) from a collection of
009: * registered directories.
010: *
011: * @author <b>Mike Clark</b>
012: * @author Clarkware Consulting, Inc.
013: */
014:
015: public class FileManager {
016:
017: private ArrayList directories;
018: private boolean acceptInnerClasses;
019:
020: public FileManager() {
021: directories = new ArrayList();
022: acceptInnerClasses = true;
023: }
024:
025: /**
026: * Determines whether inner classes should be collected.
027: *
028: * @param b <code>true</code> to collect inner classes;
029: * <code>false</code> otherwise.
030: */
031: public void acceptInnerClasses(boolean b) {
032: acceptInnerClasses = b;
033: }
034:
035: public void addDirectory(String name) throws IOException {
036:
037: File directory = new File(name);
038:
039: if (directory.isDirectory() || acceptJarFile(directory)) {
040: directories.add(directory);
041: } else {
042: throw new IOException("Invalid directory or JAR file: "
043: + name);
044: }
045: }
046:
047: public boolean acceptFile(File file) {
048: return acceptClassFile(file) || acceptJarFile(file);
049: }
050:
051: public boolean acceptClassFile(File file) {
052: if (!file.isFile()) {
053: return false;
054: }
055: return acceptClassFileName(file.getName());
056: }
057:
058: public boolean acceptClassFileName(String name) {
059:
060: if (!acceptInnerClasses) {
061: if (name.toLowerCase().indexOf("$") > 0) {
062: return false;
063: }
064: }
065:
066: if (!name.toLowerCase().endsWith(".class")) {
067: return false;
068: }
069:
070: return true;
071: }
072:
073: public boolean acceptJarFile(File file) {
074: return isJar(file) || isZip(file) || isWar(file);
075: }
076:
077: public Collection extractFiles() {
078:
079: Collection files = new ArrayList();
080:
081: for (Iterator i = directories.iterator(); i.hasNext();) {
082: File directory = (File) i.next();
083: collectFiles(directory, files);
084: }
085:
086: return files;
087: }
088:
089: private void collectFiles(File directory, Collection files) {
090:
091: if (directory.isFile()) {
092:
093: addFile(directory, files);
094:
095: } else {
096:
097: String[] directoryFiles = directory.list();
098:
099: for (int i = 0; i < directoryFiles.length; i++) {
100:
101: File file = new File(directory, directoryFiles[i]);
102: if (acceptFile(file)) {
103: addFile(file, files);
104: } else if (file.isDirectory()) {
105: collectFiles(file, files);
106: }
107: }
108: }
109: }
110:
111: private void addFile(File f, Collection files) {
112: if (!files.contains(f)) {
113: files.add(f);
114: }
115: }
116:
117: private boolean isWar(File file) {
118: return existsWithExtension(file, ".war");
119: }
120:
121: private boolean isZip(File file) {
122: return existsWithExtension(file, ".zip");
123: }
124:
125: private boolean isJar(File file) {
126: return existsWithExtension(file, ".jar");
127: }
128:
129: private boolean existsWithExtension(File file, String extension) {
130: return file.isFile()
131: && file.getName().toLowerCase().endsWith(extension);
132: }
133:
134: }
|