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.commandline.*; 41 import com.jeantessier.dependency.*; 42 import com.jeantessier.dependencyfinder.*; 43 44 public class DependencyClosure { 45 public static final String DEFAULT_START_INCLUDES = "//"; 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("Defaults is text output to the console."); 57 System.err.println(); 58 } 59 60 public static void showVersion() throws IOException { 61 Version version = new Version(); 62 63 System.err.print(version.getImplementationTitle()); 64 System.err.print(" "); 65 System.err.print(version.getImplementationVersion()); 66 System.err.print(" (c) "); 67 System.err.print(version.getCopyrightDate()); 68 System.err.print(" "); 69 System.err.print(version.getCopyrightHolder()); 70 System.err.println(); 71 72 System.err.print(version.getImplementationURL()); 73 System.err.println(); 74 75 System.err.print("Compiled on "); 76 System.err.print(version.getImplementationDate()); 77 System.err.println(); 78 } 79 80 public static void main(String [] args) throws Exception { 81 CommandLine commandLine = new CommandLine(new AtLeastParameterStrategy(1)); 83 commandLine.addMultipleValuesSwitch("start-includes", DEFAULT_START_INCLUDES); 84 commandLine.addMultipleValuesSwitch("start-excludes"); 85 commandLine.addMultipleValuesSwitch("package-start-includes"); 86 commandLine.addMultipleValuesSwitch("package-start-excludes"); 87 commandLine.addMultipleValuesSwitch("class-start-includes"); 88 commandLine.addMultipleValuesSwitch("class-start-excludes"); 89 commandLine.addMultipleValuesSwitch("feature-start-includes"); 90 commandLine.addMultipleValuesSwitch("feature-start-excludes"); 91 commandLine.addMultipleValuesSwitch("stop-includes"); 92 commandLine.addMultipleValuesSwitch("stop-excludes"); 93 commandLine.addMultipleValuesSwitch("package-stop-includes"); 94 commandLine.addMultipleValuesSwitch("package-stop-excludes"); 95 commandLine.addMultipleValuesSwitch("class-stop-includes"); 96 commandLine.addMultipleValuesSwitch("class-stop-excludes"); 97 commandLine.addMultipleValuesSwitch("feature-stop-includes"); 98 commandLine.addMultipleValuesSwitch("feature-stop-excludes"); 99 100 commandLine.addOptionalValueSwitch("maximum-inbound-depth"); 101 commandLine.addOptionalValueSwitch("maximum-outbound-depth"); 102 103 commandLine.addToggleSwitch("xml"); 104 commandLine.addToggleSwitch("validate"); 105 commandLine.addSingleValueSwitch("encoding", XMLPrinter.DEFAULT_ENCODING); 106 commandLine.addSingleValueSwitch("dtd-prefix", XMLPrinter.DEFAULT_DTD_PREFIX); 107 commandLine.addSingleValueSwitch("indent-text"); 108 commandLine.addToggleSwitch("time"); 109 commandLine.addSingleValueSwitch("out"); 110 commandLine.addToggleSwitch("help"); 111 commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE); 112 commandLine.addToggleSwitch("version"); 113 114 CommandLineUsage usage = new CommandLineUsage("DependencyClosure"); 115 commandLine.accept(usage); 116 117 try { 118 commandLine.parse(args); 119 } catch (IllegalArgumentException ex) { 120 showError(usage, ex.toString()); 121 System.exit(1); 122 } catch (CommandLineException ex) { 123 showError(usage, ex.toString()); 124 System.exit(1); 125 } 126 127 if (commandLine.getToggleSwitch("help")) { 128 showError(usage); 129 } 130 131 if (commandLine.getToggleSwitch("version")) { 132 showVersion(); 133 } 134 135 if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) { 136 System.exit(1); 137 } 138 139 VerboseListener verboseListener = new VerboseListener(); 140 if (commandLine.isPresent("verbose")) { 141 if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) { 142 verboseListener.setWriter(System.out); 143 } else { 144 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose"))); 145 } 146 } 147 148 151 152 Date start = new Date(); 153 154 NodeFactory factory = new NodeFactory(); 155 156 Iterator i = commandLine.getParameters().iterator(); 157 while (i.hasNext()) { 158 String filename = (String ) i.next(); 159 Logger.getLogger(DependencyClosure.class).info("Reading " + filename); 160 verboseListener.print("Reading " + filename); 161 162 if (filename.endsWith(".xml")) { 163 NodeLoader loader = new NodeLoader(factory, commandLine.getToggleSwitch("validate")); 164 loader.addDependencyListener(verboseListener); 165 loader.load(filename); 166 } 167 168 Logger.getLogger(DependencyClosure.class).info("Read \"" + filename + "\"."); 169 } 170 171 RegularExpressionSelectionCriteria startCriteria = new RegularExpressionSelectionCriteria(); 172 173 if (commandLine.isPresent("start-includes") || (!commandLine.isPresent("package-start-includes") && !commandLine.isPresent("class-start-includes") && !commandLine.isPresent("feature-start-includes"))) { 174 startCriteria.setGlobalIncludes(commandLine.getMultipleSwitch("start-includes")); 176 } 177 startCriteria.setGlobalExcludes(commandLine.getMultipleSwitch("start-excludes")); 178 startCriteria.setPackageIncludes(commandLine.getMultipleSwitch("package-start-includes")); 179 startCriteria.setPackageExcludes(commandLine.getMultipleSwitch("package-start-excludes")); 180 startCriteria.setClassIncludes(commandLine.getMultipleSwitch("class-start-includes")); 181 startCriteria.setClassExcludes(commandLine.getMultipleSwitch("class-start-excludes")); 182 startCriteria.setFeatureIncludes(commandLine.getMultipleSwitch("feature-start-includes")); 183 startCriteria.setFeatureExcludes(commandLine.getMultipleSwitch("feature-start-excludes")); 184 185 RegularExpressionSelectionCriteria stopCriteria = new RegularExpressionSelectionCriteria(); 186 187 stopCriteria.setGlobalIncludes(commandLine.getMultipleSwitch("stop-includes")); 188 stopCriteria.setGlobalExcludes(commandLine.getMultipleSwitch("stop-excludes")); 189 stopCriteria.setPackageIncludes(commandLine.getMultipleSwitch("package-stop-includes")); 190 stopCriteria.setPackageExcludes(commandLine.getMultipleSwitch("package-stop-excludes")); 191 stopCriteria.setClassIncludes(commandLine.getMultipleSwitch("class-stop-includes")); 192 stopCriteria.setClassExcludes(commandLine.getMultipleSwitch("class-stop-excludes")); 193 stopCriteria.setFeatureIncludes(commandLine.getMultipleSwitch("feature-stop-includes")); 194 stopCriteria.setFeatureExcludes(commandLine.getMultipleSwitch("feature-stop-excludes")); 195 196 TransitiveClosure selector = new TransitiveClosure(startCriteria, stopCriteria); 197 198 try { 199 if (commandLine.isPresent("maximum-inbound-depth")) { 200 selector.setMaximumInboundDepth(Long.parseLong(commandLine.getSingleSwitch("maximum-inbound-depth"))); 201 } 202 } catch (NumberFormatException ex) { 203 selector.setMaximumInboundDepth(TransitiveClosure.UNBOUNDED_DEPTH); 204 } 205 206 try { 207 if (commandLine.isPresent("maximum-outbound-depth")) { 208 selector.setMaximumOutboundDepth(Long.parseLong(commandLine.getSingleSwitch("maximum-outbound-depth"))); 209 } 210 } catch (NumberFormatException ex) { 211 selector.setMaximumOutboundDepth(TransitiveClosure.UNBOUNDED_DEPTH); 212 } 213 214 Logger.getLogger(DependencyClosure.class).info("Operating on " + factory.getPackages().values().size() + " package(s) ..."); 215 216 selector.traverseNodes(factory.getPackages().values()); 217 218 Logger.getLogger(DependencyClosure.class).info("Reporting " + selector.getFactory().getPackages().values().size() + " package(s) ..."); 219 220 verboseListener.print("Printing the graph ..."); 221 222 PrintWriter out; 223 if (commandLine.isPresent("out")) { 224 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out"))); 225 } else { 226 out = new PrintWriter(System.out); 227 } 228 229 Printer printer; 230 if (commandLine.isPresent("xml")) { 231 printer = new XMLPrinter(out, commandLine.getSingleSwitch("encoding"), commandLine.getSingleSwitch("dtd-prefix")); 232 } else { 233 printer = new TextPrinter(out); 234 } 235 236 if (commandLine.isPresent("indent-text")) { 237 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 238 } 239 240 printer.traverseNodes(selector.getFactory().getPackages().values()); 241 242 out.close(); 243 244 Date end = new Date(); 245 246 if (commandLine.getToggleSwitch("time")) { 247 System.err.println(DependencyClosure.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs."); 248 } 249 250 verboseListener.close(); 251 } 252 } 253 | Popular Tags |