KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junitdoclet > PackageLister


1 package org.junitdoclet;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.io.BufferedWriter JavaDoc;
5 import java.io.FileWriter JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.File JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 public class PackageLister {
13
14     private String JavaDoc output;
15     private String JavaDoc topPackage;
16     private String JavaDoc path;
17
18     public PackageLister() {
19         init();
20     }
21
22     public void init() {
23         setOutput(null);
24         setTopPackage(null);
25         setPath(null);
26     }
27
28     public String JavaDoc getOutput() {
29         return output;
30     }
31
32     public void setOutput(String JavaDoc output) {
33         this.output = output;
34     }
35
36     public String JavaDoc getTopPackage() {
37         return topPackage;
38     }
39
40     public void setTopPackage(String JavaDoc topPackage) {
41         this.topPackage = topPackage;
42     }
43
44     public String JavaDoc getPath() {
45         return path;
46     }
47
48     public void setPath(String JavaDoc path) {
49         this.path = path;
50     }
51
52
53     public boolean processArgs(String JavaDoc[] args) {
54         boolean returnValue = true;
55         int processedArg;
56         boolean pathFound = false;
57
58         processedArg = 0;
59
60         while (processedArg < args.length) {
61             if (args[processedArg].equals("-o")) {
62                 processedArg++;
63                 if (processedArg<args.length) {
64                     setOutput(args[processedArg]);
65                 } else {
66                     returnValue = false;
67                 }
68             } else {
69                 if (args[processedArg].equals("-top")) {
70                     processedArg++;
71                     if (processedArg<args.length) {
72                         setTopPackage(args[processedArg]);
73                     } else {
74                         returnValue = false;
75                     }
76                 } else {
77                     setPath(args[processedArg]);
78                     pathFound = true;
79                 }
80             }
81
82             processedArg++;
83         }
84
85         returnValue = returnValue && (args.length == processedArg);
86         returnValue = returnValue && pathFound;
87
88         return returnValue;
89     }
90
91     public void execute() {
92
93         HashMap JavaDoc packages = new HashMap JavaDoc();
94         String JavaDoc top;
95
96         try {
97
98             top = getTopPackage();
99
100             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(getPath(), ";");
101
102             while (tokenizer.hasMoreTokens()) {
103
104                 File JavaDoc root = new File JavaDoc(tokenizer.nextToken());
105
106                 if (root.isDirectory()) {
107                     collectPackages(packages, top, root, root);
108                 }
109             }
110
111             writeOutput(packages);
112
113         } catch (IOException JavaDoc ex) {
114             ex.printStackTrace();
115         }
116     }
117
118     private void writeOutput(HashMap JavaDoc packages) throws IOException JavaDoc {
119         PrintWriter JavaDoc writer;
120
121         if (getOutput() != null) {
122             writer = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(getOutput())));
123         } else {
124             writer = new PrintWriter JavaDoc(System.out);
125         }
126
127         writePackages(writer, packages);
128
129         if (getOutput() != null) {
130             writer.close();
131         }
132     }
133
134     public static void main(String JavaDoc[] args) {
135
136         PackageLister app;
137
138         app = new PackageLister();
139
140         if ((app != null) && app.processArgs(args)) {
141             app.execute();
142         } else {
143             printUsage();
144         }
145
146     }
147
148     public static void printUsage() {
149         String JavaDoc usage = "java PackageLister [-top <package>] <search_path> [-o <file_name>]\n"+
150                        "\n"+
151                        "Listing all packages in a given path. Useful before calling javadoc.\n"+
152                        "\n"+
153                        " -top <package> Only this package and packages below that are listed.\n"+
154                        " <search_path> Root directories separated by \";\" (sample:\"./classes;C:/classes\").\n"+
155                        " -o <file_name> Where to write the list. Unsually this is \"packages.txt\".\n"+
156                        " If this option is not specified, standard output is used.\n"+
157                        "\n";
158         System.out.println(usage);
159     }
160
161     public void collectPackages(HashMap JavaDoc map, String JavaDoc top, File JavaDoc root, File JavaDoc dir) {
162         String JavaDoc files[] = dir.list();
163         String JavaDoc aPackage;
164         boolean fileFound = true;
165
166         for (int i=0; i<files.length; i++) {
167
168             File JavaDoc file = new File JavaDoc(dir,files[i]);
169
170             if (file.isDirectory()) {
171
172                 collectPackages(map, top, root,file);
173
174             } else {
175
176                 if (fileFound && (files[i].endsWith(".class") || files[i].endsWith(".java"))) {
177
178                     fileFound = false;
179
180                     if (root.equals(dir)) {
181                         //writer.println("."); This was incorrect assumption about Javadoc
182
} else {
183                         aPackage = dir.getPath().substring(root.getPath().length()+1).replace(File.separatorChar,'.');
184                         if ((top==null) || aPackage.startsWith(top)) {
185                             map.put(aPackage, aPackage);
186                         }
187                     }
188                 }
189             }
190         }
191     }
192
193     public void writePackages(PrintWriter JavaDoc writer, HashMap JavaDoc map) {
194         for (Iterator JavaDoc iterator = map.keySet().iterator(); (iterator.hasNext()); ) {
195             writer.println(iterator.next());
196         }
197     }
198 }
199
Popular Tags