KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jdepend > framework > FileManager


1 package jdepend.framework;
2
3 import java.io.*;
4 import java.util.*;
5
6 /**
7  * The <code>FileManager</code> class is responsible for extracting
8  * Java class files (<code>.class</code> files) from a collection of
9  * registered directories.
10  *
11  * @author <b>Mike Clark</b>
12  * @author Clarkware Consulting, Inc.
13  */

14
15 public class FileManager {
16
17     private ArrayList directories;
18     private boolean acceptInnerClasses;
19
20
21     public FileManager() {
22         directories = new ArrayList();
23         acceptInnerClasses = true;
24     }
25
26     /**
27      * Determines whether inner classes should be collected.
28      *
29      * @param b <code>true</code> to collect inner classes;
30      * <code>false</code> otherwise.
31      */

32     public void acceptInnerClasses(boolean b) {
33         acceptInnerClasses = b;
34     }
35
36     public void addDirectory(String JavaDoc name) throws IOException {
37
38         File directory = new File(name);
39
40         if (directory.isDirectory() || acceptJarFile(directory)) {
41             directories.add(directory);
42         } else {
43             throw new IOException("Invalid directory or JAR file: " + name);
44         }
45     }
46
47     public boolean acceptFile(File file) {
48         return acceptClassFile(file) || acceptJarFile(file);
49     }
50
51     public boolean acceptClassFile(File file) {
52         if (!file.isFile()) {
53             return false;
54         }
55         return acceptClassFileName(file.getName());
56     }
57
58     public boolean acceptClassFileName(String JavaDoc name) {
59
60         if (!acceptInnerClasses) {
61             if (name.toLowerCase().indexOf("$") > 0) {
62                 return false;
63             }
64         }
65
66         if (!name.toLowerCase().endsWith(".class")) {
67             return false;
68         }
69
70         return true;
71     }
72
73     public boolean acceptJarFile(File file) {
74         return isJar(file) || isZip(file) || isWar(file);
75     }
76
77     public Collection extractFiles() {
78
79         Collection files = new ArrayList();
80
81         for (Iterator i = directories.iterator(); i.hasNext();) {
82             File directory = (File)i.next();
83             collectFiles(directory, files);
84         }
85
86         return files;
87     }
88
89     private void collectFiles(File directory, Collection files) {
90
91         if (directory.isFile()) {
92
93             addFile(directory, files);
94
95         } else {
96
97             String JavaDoc[] directoryFiles = directory.list();
98
99             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 JavaDoc extension) {
130         return file.isFile() &&
131             file.getName().toLowerCase().endsWith(extension);
132     }
133
134 }
Popular Tags