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