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.dependencyfinder.*; 42 import com.jeantessier.diff.*; 43 44 public class ListDiff { 45 public static void showError(CommandLineUsage clu, String msg) { 46 System.err.println(msg); 47 showError(clu); 48 } 49 50 public static void showError(CommandLineUsage clu) { 51 System.err.println(clu); 52 System.err.println(); 53 System.err.println("Defaults is text output to the console."); 54 System.err.println(); 55 } 56 57 public static void showVersion() throws IOException { 58 Version version = new Version(); 59 60 System.err.print(version.getImplementationTitle()); 61 System.err.print(" "); 62 System.err.print(version.getImplementationVersion()); 63 System.err.print(" (c) "); 64 System.err.print(version.getCopyrightDate()); 65 System.err.print(" "); 66 System.err.print(version.getCopyrightHolder()); 67 System.err.println(); 68 69 System.err.print(version.getImplementationURL()); 70 System.err.println(); 71 72 System.err.print("Compiled on "); 73 System.err.print(version.getImplementationDate()); 74 System.err.println(); 75 } 76 77 public static void main(String [] args) throws Exception { 78 CommandLine commandLine = new CommandLine(new NullParameterStrategy()); 80 commandLine.addSingleValueSwitch("name"); 81 commandLine.addSingleValueSwitch("old-label"); 82 commandLine.addSingleValueSwitch("old", true); 83 commandLine.addSingleValueSwitch("new-label"); 84 commandLine.addSingleValueSwitch("new", true); 85 commandLine.addToggleSwitch("compress"); 86 commandLine.addSingleValueSwitch("encoding", ListDiffPrinter.DEFAULT_ENCODING); 87 commandLine.addSingleValueSwitch("dtd-prefix", ListDiffPrinter.DEFAULT_DTD_PREFIX); 88 commandLine.addSingleValueSwitch("indent-text"); 89 commandLine.addToggleSwitch("time"); 90 commandLine.addSingleValueSwitch("out"); 91 commandLine.addToggleSwitch("help"); 92 commandLine.addToggleSwitch("version"); 93 94 CommandLineUsage usage = new CommandLineUsage("ListDiff"); 95 commandLine.accept(usage); 96 97 try { 98 commandLine.parse(args); 99 } catch (IllegalArgumentException ex) { 100 showError(usage, ex.toString()); 101 System.exit(1); 102 } catch (CommandLineException ex) { 103 showError(usage, ex.toString()); 104 System.exit(1); 105 } 106 107 if (commandLine.getToggleSwitch("help")) { 108 showError(usage); 109 } 110 111 if (commandLine.getToggleSwitch("version")) { 112 showVersion(); 113 } 114 115 if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) { 116 System.exit(1); 117 } 118 119 122 123 Date start = new Date(); 124 125 String line; 126 127 Logger.getLogger(ListDiff.class).info("Loading data ..."); 128 129 Collection oldAPI = new TreeSet(); 130 BufferedReader oldIn = new BufferedReader(new FileReader(commandLine.getSingleSwitch("old"))); 131 while((line = oldIn.readLine()) != null) { 132 oldAPI.add(line); 133 } 134 135 Collection newAPI = new TreeSet(); 136 BufferedReader newIn = new BufferedReader(new FileReader(commandLine.getSingleSwitch("new"))); 137 while((line = newIn.readLine()) != null) { 138 newAPI.add(line); 139 } 140 141 ListDiffPrinter printer = new ListDiffPrinter(commandLine.getToggleSwitch("compress"), commandLine.getSingleSwitch("encoding"), commandLine.getSingleSwitch("dtd-prefix")); 142 printer.setName(commandLine.getSingleSwitch("name")); 143 printer.setOldVersion(commandLine.getSingleSwitch("old-label")); 144 printer.setNewVersion(commandLine.getSingleSwitch("new-label")); 145 if (commandLine.isPresent("indent-text")) { 146 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 147 } 148 149 Iterator i; 150 151 i = oldAPI.iterator(); 152 while (i.hasNext()) { 153 line = (String ) i.next(); 154 if (!newAPI.contains(line)) { 155 printer.remove(line); 156 } 157 } 158 159 i = newAPI.iterator(); 160 while (i.hasNext()) { 161 line = (String ) i.next(); 162 if (!oldAPI.contains(line)) { 163 printer.add(line); 164 } 165 } 166 167 Logger.getLogger(ListDiff.class).info("Printing results ..."); 168 169 PrintWriter out; 170 if (commandLine.isPresent("out")) { 171 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out"))); 172 } else { 173 out = new PrintWriter(new OutputStreamWriter(System.out)); 174 } 175 out.print(printer); 176 out.close(); 177 178 Date end = new Date(); 179 180 if (commandLine.getToggleSwitch("time")) { 181 System.err.println(ListDiff.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs."); 182 } 183 } 184 } 185 | Popular Tags |