1 32 33 package com.jeantessier.dependencyfinder.cli; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.log4j.*; 39 40 import com.jeantessier.classreader.*; 41 import com.jeantessier.commandline.*; 42 import com.jeantessier.dependency.*; 43 import com.jeantessier.dependencyfinder.*; 44 45 public class DependencyExtractor { 46 public static final String DEFAULT_LOGFILE = "System.out"; 47 48 public static void showError(CommandLineUsage clu, String msg) { 49 System.err.println(msg); 50 showError(clu); 51 } 52 53 public static void showError(CommandLineUsage clu) { 54 System.err.println(clu); 55 System.err.println(); 56 System.err.println("If no files are specified, it processes the current directory."); 57 System.err.println(); 58 System.err.println("If file is a directory, it is recusively scanned for files"); 59 System.err.println("ending in \".class\"."); 60 System.err.println(); 61 System.err.println("Defaults is text output to the console."); 62 System.err.println(); 63 } 64 65 public static void showVersion() throws IOException { 66 Version version = new Version(); 67 68 System.err.print(version.getImplementationTitle()); 69 System.err.print(" "); 70 System.err.print(version.getImplementationVersion()); 71 System.err.print(" (c) "); 72 System.err.print(version.getCopyrightDate()); 73 System.err.print(" "); 74 System.err.print(version.getCopyrightHolder()); 75 System.err.println(); 76 77 System.err.print(version.getImplementationURL()); 78 System.err.println(); 79 80 System.err.print("Compiled on "); 81 System.err.print(version.getImplementationDate()); 82 System.err.println(); 83 } 84 85 public static void main(String [] args) throws Exception { 86 CommandLine commandLine = new CommandLine(); 88 commandLine.addToggleSwitch("xml"); 89 commandLine.addToggleSwitch("maximize"); 90 commandLine.addToggleSwitch("minimize"); 91 commandLine.addSingleValueSwitch("encoding", com.jeantessier.dependency.XMLPrinter.DEFAULT_ENCODING); 92 commandLine.addSingleValueSwitch("dtd-prefix", com.jeantessier.dependency.XMLPrinter.DEFAULT_DTD_PREFIX); 93 commandLine.addSingleValueSwitch("indent-text"); 94 commandLine.addToggleSwitch("time"); 95 commandLine.addSingleValueSwitch("out"); 96 commandLine.addToggleSwitch("help"); 97 commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE); 98 commandLine.addToggleSwitch("version"); 99 100 CommandLineUsage usage = new CommandLineUsage("DependencyExtractor"); 101 commandLine.accept(usage); 102 103 try { 104 commandLine.parse(args); 105 } catch (IllegalArgumentException ex) { 106 showError(usage, ex.toString()); 107 System.exit(1); 108 } catch (CommandLineException ex) { 109 showError(usage, ex.toString()); 110 System.exit(1); 111 } 112 113 if (commandLine.getToggleSwitch("help")) { 114 showError(usage); 115 } 116 117 if (commandLine.getToggleSwitch("version")) { 118 showVersion(); 119 } 120 121 if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) { 122 System.exit(1); 123 } 124 125 VerboseListener verboseListener = new VerboseListener(); 126 if (commandLine.isPresent("verbose")) { 127 if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) { 128 verboseListener.setWriter(System.out); 129 } else { 130 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose"))); 131 } 132 } 133 134 137 138 Date start = new Date(); 139 140 List parameters = commandLine.getParameters(); 141 if (parameters.size() == 0) { 142 parameters.add("."); 143 } 144 145 NodeFactory factory = new NodeFactory(); 146 CodeDependencyCollector collector = new CodeDependencyCollector(factory); 147 148 ClassfileLoader loader = new TransientClassfileLoader(); 149 loader.addLoadListener(new LoadListenerVisitorAdapter(collector)); 150 loader.addLoadListener(verboseListener); 151 loader.load(parameters); 152 153 if (commandLine.isPresent("minimize")) { 154 LinkMinimizer minimizer = new LinkMinimizer(); 155 minimizer.traverseNodes(factory.getPackages().values()); 156 } else if (commandLine.isPresent("maximize")) { 157 LinkMaximizer maximizer = new LinkMaximizer(); 158 maximizer.traverseNodes(factory.getPackages().values()); 159 } 160 161 verboseListener.print("Printing the graph ..."); 162 163 PrintWriter out; 164 if (commandLine.isPresent("out")) { 165 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out"))); 166 } else { 167 out = new PrintWriter(System.out); 168 } 169 170 com.jeantessier.dependency.Printer printer; 171 if (commandLine.getToggleSwitch("xml")) { 172 printer = new com.jeantessier.dependency.XMLPrinter(out, commandLine.getSingleSwitch("encoding"), commandLine.getSingleSwitch("dtd-prefix")); 173 } else { 174 printer = new com.jeantessier.dependency.TextPrinter(out); 175 } 176 177 if (commandLine.isPresent("indent-text")) { 178 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 179 } 180 181 printer.traverseNodes(factory.getPackages().values()); 182 183 out.close(); 184 185 Date end = new Date(); 186 187 if (commandLine.getToggleSwitch("time")) { 188 System.err.println(DependencyExtractor.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs."); 189 } 190 191 verboseListener.close(); 192 } 193 } 194 | Popular Tags |