1 package org.junitdoclet; 2 3 import java.io.PrintWriter ; 4 import java.io.BufferedWriter ; 5 import java.io.FileWriter ; 6 import java.io.IOException ; 7 import java.io.File ; 8 import java.util.StringTokenizer ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 12 public class PackageLister { 13 14 private String output; 15 private String topPackage; 16 private String 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 getOutput() { 29 return output; 30 } 31 32 public void setOutput(String output) { 33 this.output = output; 34 } 35 36 public String getTopPackage() { 37 return topPackage; 38 } 39 40 public void setTopPackage(String topPackage) { 41 this.topPackage = topPackage; 42 } 43 44 public String getPath() { 45 return path; 46 } 47 48 public void setPath(String path) { 49 this.path = path; 50 } 51 52 53 public boolean processArgs(String [] 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 packages = new HashMap (); 94 String top; 95 96 try { 97 98 top = getTopPackage(); 99 100 StringTokenizer tokenizer = new StringTokenizer (getPath(), ";"); 101 102 while (tokenizer.hasMoreTokens()) { 103 104 File root = new File (tokenizer.nextToken()); 105 106 if (root.isDirectory()) { 107 collectPackages(packages, top, root, root); 108 } 109 } 110 111 writeOutput(packages); 112 113 } catch (IOException ex) { 114 ex.printStackTrace(); 115 } 116 } 117 118 private void writeOutput(HashMap packages) throws IOException { 119 PrintWriter writer; 120 121 if (getOutput() != null) { 122 writer = new PrintWriter (new BufferedWriter (new FileWriter (getOutput()))); 123 } else { 124 writer = new PrintWriter (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 [] 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 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 map, String top, File root, File dir) { 162 String files[] = dir.list(); 163 String aPackage; 164 boolean fileFound = true; 165 166 for (int i=0; i<files.length; i++) { 167 168 File file = new File (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 } 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 writer, HashMap map) { 194 for (Iterator iterator = map.keySet().iterator(); (iterator.hasNext()); ) { 195 writer.println(iterator.next()); 196 } 197 } 198 } 199 | Popular Tags |