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 ClassFinder { 45 public static final String DEFAULT_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("If no files are specified, it processes the current directory."); 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(); 83 commandLine.addMultipleValuesSwitch("includes", DEFAULT_INCLUDES); 84 commandLine.addMultipleValuesSwitch("excludes"); 85 commandLine.addToggleSwitch("time"); 86 commandLine.addSingleValueSwitch("out"); 87 commandLine.addToggleSwitch("help"); 88 commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE); 89 commandLine.addToggleSwitch("version"); 90 91 CommandLineUsage usage = new CommandLineUsage("ClassFinder"); 92 commandLine.accept(usage); 93 94 try { 95 commandLine.parse(args); 96 } catch (IllegalArgumentException ex) { 97 showError(usage, ex.toString()); 98 System.exit(1); 99 } catch (CommandLineException ex) { 100 showError(usage, ex.toString()); 101 System.exit(1); 102 } 103 104 if (commandLine.getToggleSwitch("help")) { 105 showError(usage); 106 } 107 108 if (commandLine.getToggleSwitch("version")) { 109 showVersion(); 110 } 111 112 if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) { 113 System.exit(1); 114 } 115 116 VerboseListener verboseListener = new VerboseListener(); 117 if (commandLine.isPresent("verbose")) { 118 if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) { 119 verboseListener.setWriter(System.out); 120 } else { 121 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose"))); 122 } 123 } 124 125 128 129 Date start = new Date(); 130 131 PrintWriter out; 132 if (commandLine.isPresent("out")) { 133 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out"))); 134 } else { 135 out = new PrintWriter(new OutputStreamWriter(System.out)); 136 } 137 138 List parameters = commandLine.getParameters(); 139 if (parameters.size() == 0) { 140 parameters.add("."); 141 } 142 143 ClassMatcher matcher = new ClassMatcher(commandLine.getMultipleSwitch("includes"), commandLine.getMultipleSwitch("excludes")); 144 145 ClassfileLoader loader = new TransientClassfileLoader(); 146 loader.addLoadListener(matcher); 147 loader.addLoadListener(verboseListener); 148 loader.load(parameters); 149 150 Iterator i = matcher.getResults().entrySet().iterator(); 151 while (i.hasNext()) { 152 Map.Entry entry = (Map.Entry) i.next(); 153 out.print(entry.getKey()); 154 out.print(": "); 155 156 Iterator j = ((List) entry.getValue()).iterator(); 157 while (j.hasNext()) { 158 out.print(j.next()); 159 if (j.hasNext()) { 160 out.print(", "); 161 } 162 } 163 164 out.println(); 165 } 166 167 Date end = new Date(); 168 169 if (commandLine.getToggleSwitch("time")) { 170 System.err.println(ClassFinder.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs."); 171 } 172 173 out.close(); 174 175 verboseListener.close(); 176 } 177 } 178 | Popular Tags |