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.dependencyfinder.*; 43 44 public class ClassReader { 45 public static final String DEFAULT_LOGFILE = "System.out"; 46 47 public static void showError(CommandLineUsage clu, String msg) { 48 System.err.println(msg); 49 showError(clu); 50 } 51 52 public static void showError(CommandLineUsage clu) { 53 System.err.println(clu); 54 System.err.println(); 55 System.err.println("If no files are specified, it processes the current directory."); 56 System.err.println(); 57 } 58 59 public static void showVersion() throws IOException { 60 Version version = new Version(); 61 62 System.err.print(version.getImplementationTitle()); 63 System.err.print(" "); 64 System.err.print(version.getImplementationVersion()); 65 System.err.print(" (c) "); 66 System.err.print(version.getCopyrightDate()); 67 System.err.print(" "); 68 System.err.print(version.getCopyrightHolder()); 69 System.err.println(); 70 71 System.err.print(version.getImplementationURL()); 72 System.err.println(); 73 74 System.err.print("Compiled on "); 75 System.err.print(version.getImplementationDate()); 76 System.err.println(); 77 } 78 79 public static void main(String [] args) throws Exception { 80 CommandLine commandLine = new CommandLine(new AtLeastParameterStrategy(1)); 82 commandLine.addToggleSwitch("xml"); 83 commandLine.addSingleValueSwitch("encoding", XMLPrinter.DEFAULT_ENCODING); 84 commandLine.addSingleValueSwitch("dtd-prefix", XMLPrinter.DEFAULT_DTD_PREFIX); 85 commandLine.addSingleValueSwitch("indent-text"); 86 commandLine.addToggleSwitch("time"); 87 commandLine.addSingleValueSwitch("out"); 88 commandLine.addToggleSwitch("help"); 89 commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE); 90 commandLine.addToggleSwitch("version"); 91 92 CommandLineUsage usage = new CommandLineUsage("ClassReader"); 93 commandLine.accept(usage); 94 95 try { 96 commandLine.parse(args); 97 } catch (IllegalArgumentException ex) { 98 showError(usage, ex.toString()); 99 System.exit(1); 100 } catch (CommandLineException ex) { 101 showError(usage, ex.toString()); 102 System.exit(1); 103 } 104 105 if (commandLine.getToggleSwitch("help")) { 106 showError(usage); 107 } 108 109 if (commandLine.getToggleSwitch("version")) { 110 showVersion(); 111 } 112 113 if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) { 114 System.exit(1); 115 } 116 117 VerboseListener verboseListener = new VerboseListener(); 118 if (commandLine.isPresent("verbose")) { 119 if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) { 120 verboseListener.setWriter(System.out); 121 } else { 122 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose"))); 123 } 124 } 125 126 129 130 Date start = new Date(); 131 132 List parameters = commandLine.getParameters(); 133 if (parameters.size() == 0) { 134 parameters.add("."); 135 } 136 137 PrintWriter out; 138 if (commandLine.isPresent("out")) { 139 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out"))); 140 } else { 141 out = new PrintWriter(new OutputStreamWriter(System.out)); 142 } 143 144 ClassfileLoader loader = new AggregatingClassfileLoader(); 145 loader.addLoadListener(verboseListener); 146 loader.load(parameters); 147 148 Printer printer; 149 150 if (commandLine.getToggleSwitch("xml")) { 151 printer = new XMLPrinter(out, commandLine.getSingleSwitch("encoding"), commandLine.getSingleSwitch("dtd-prefix")); 152 } else { 153 printer = new TextPrinter(out); 154 } 155 156 if (commandLine.isPresent("indent-text")) { 157 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 158 } 159 160 printer.visitClassfiles(loader.getAllClassfiles()); 161 162 Date end = new Date(); 163 164 if (commandLine.getToggleSwitch("time")) { 165 System.err.println(ClassReader.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs."); 166 } 167 168 out.close(); 169 170 verboseListener.close(); 171 } 172 } 173 | Popular Tags |