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 import com.jeantessier.diff.*; 45 46 public class JarJarDiff { 47 public static final String DEFAULT_OLD_DOCUMENTATION = "old_documentation.txt"; 48 public static final String DEFAULT_NEW_DOCUMENTATION = "new_documentation.txt"; 49 public static final String DEFAULT_LOGFILE = "System.out"; 50 51 public static void showError(CommandLineUsage clu, String msg) { 52 System.err.println(msg); 53 showError(clu); 54 } 55 56 public static void showError(CommandLineUsage clu) { 57 System.err.println(clu); 58 System.err.println(); 59 System.err.println("Defaults is text output to the console."); 60 System.err.println(); 61 } 62 63 public static void showVersion() throws IOException { 64 Version version = new Version(); 65 66 System.err.print(version.getImplementationTitle()); 67 System.err.print(" "); 68 System.err.print(version.getImplementationVersion()); 69 System.err.print(" (c) "); 70 System.err.print(version.getCopyrightDate()); 71 System.err.print(" "); 72 System.err.print(version.getCopyrightHolder()); 73 System.err.println(); 74 75 System.err.print(version.getImplementationURL()); 76 System.err.println(); 77 78 System.err.print("Compiled on "); 79 System.err.print(version.getImplementationDate()); 80 System.err.println(); 81 } 82 83 public static void main(String [] args) throws Exception { 84 CommandLine commandLine = new CommandLine(new NullParameterStrategy()); 86 commandLine.addSingleValueSwitch("name"); 87 commandLine.addMultipleValuesSwitch("old", true); 88 commandLine.addSingleValueSwitch("old-label"); 89 commandLine.addSingleValueSwitch("old-documentation", DEFAULT_OLD_DOCUMENTATION); 90 commandLine.addMultipleValuesSwitch("new", true); 91 commandLine.addSingleValueSwitch("new-label"); 92 commandLine.addSingleValueSwitch("new-documentation", DEFAULT_NEW_DOCUMENTATION); 93 commandLine.addSingleValueSwitch("encoding", Report.DEFAULT_ENCODING); 94 commandLine.addSingleValueSwitch("dtd-prefix", Report.DEFAULT_DTD_PREFIX); 95 commandLine.addSingleValueSwitch("indent-text"); 96 commandLine.addToggleSwitch("time"); 97 commandLine.addSingleValueSwitch("out"); 98 commandLine.addToggleSwitch("help"); 99 commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE); 100 commandLine.addToggleSwitch("version"); 101 102 CommandLineUsage usage = new CommandLineUsage("JarJarDiff"); 103 commandLine.accept(usage); 104 105 try { 106 commandLine.parse(args); 107 } catch (IllegalArgumentException ex) { 108 showError(usage, ex.toString()); 109 System.exit(1); 110 } catch (CommandLineException ex) { 111 showError(usage, ex.toString()); 112 System.exit(1); 113 } 114 115 if (commandLine.getToggleSwitch("help")) { 116 showError(usage); 117 } 118 119 if (commandLine.getToggleSwitch("version")) { 120 showVersion(); 121 } 122 123 if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) { 124 System.exit(1); 125 } 126 127 VerboseListener verboseListener = new VerboseListener(); 128 if (commandLine.isPresent("verbose")) { 129 if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) { 130 verboseListener.setWriter(System.out); 131 } else { 132 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose"))); 133 } 134 } 135 136 139 140 Date start = new Date(); 141 142 145 Validator oldValidator = new ListBasedValidator(commandLine.getSingleSwitch("old-documentation")); 146 ClassfileLoader oldJar = new AggregatingClassfileLoader(); 147 oldJar.addLoadListener(verboseListener); 148 oldJar.load(commandLine.getMultipleSwitch("old")); 149 150 Validator newValidator = new ListBasedValidator(commandLine.getSingleSwitch("new-documentation")); 151 ClassfileLoader newJar = new AggregatingClassfileLoader(); 152 newJar.addLoadListener(verboseListener); 153 newJar.load(commandLine.getMultipleSwitch("new")); 154 155 159 Logger.getLogger(JarJarDiff.class).info("Comparing ..."); 160 verboseListener.print("Comparing ..."); 161 162 String name = commandLine.getSingleSwitch("name"); 163 String oldLabel = commandLine.isPresent("old-label") ? commandLine.getSingleSwitch("old-label") : commandLine.getSwitch("old").toString(); 164 String newLabel = commandLine.isPresent("new-label") ? commandLine.getSingleSwitch("new-label") : commandLine.getSwitch("new").toString(); 165 166 DifferencesFactory factory = new DifferencesFactory(oldValidator, newValidator); 167 Differences differences = factory.createJarDifferences(name, oldLabel, oldJar, newLabel, newJar); 168 169 Logger.getLogger(JarJarDiff.class).info("Printing results ..."); 170 verboseListener.print("Printing results ..."); 171 172 PrintWriter out; 173 if (commandLine.isPresent("out")) { 174 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out"))); 175 } else { 176 out = new PrintWriter(new OutputStreamWriter(System.out)); 177 } 178 179 com.jeantessier.diff.Printer printer = new Report(commandLine.getSingleSwitch("encoding"), commandLine.getSingleSwitch("dtd-prefix")); 180 if (commandLine.isPresent("indent-text")) { 181 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 182 } 183 184 differences.accept(printer); 185 out.print(printer); 186 187 Date end = new Date(); 188 189 if (commandLine.getToggleSwitch("time")) { 190 System.err.println(JarJarDiff.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs."); 191 } 192 193 out.close(); 194 195 verboseListener.close(); 196 } 197 } 198 | Popular Tags |