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 ClassList { 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(); 82 commandLine.addToggleSwitch("time"); 83 commandLine.addSingleValueSwitch("out"); 84 commandLine.addToggleSwitch("help"); 85 commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE); 86 commandLine.addToggleSwitch("version"); 87 88 CommandLineUsage usage = new CommandLineUsage("ClassList"); 89 commandLine.accept(usage); 90 91 try { 92 commandLine.parse(args); 93 } catch (IllegalArgumentException ex) { 94 showError(usage, ex.toString()); 95 System.exit(1); 96 } catch (CommandLineException ex) { 97 showError(usage, ex.toString()); 98 System.exit(1); 99 } 100 101 if (commandLine.getToggleSwitch("help")) { 102 showError(usage); 103 } 104 105 if (commandLine.getToggleSwitch("version")) { 106 showVersion(); 107 } 108 109 if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) { 110 System.exit(1); 111 } 112 113 VerboseListener verboseListener = new VerboseListener(); 114 if (commandLine.isPresent("verbose")) { 115 if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) { 116 verboseListener.setWriter(System.out); 117 } else { 118 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose"))); 119 } 120 } 121 122 125 126 Date start = new Date(); 127 128 PrintWriter out; 129 if (commandLine.isPresent("out")) { 130 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out"))); 131 } else { 132 out = new PrintWriter(new OutputStreamWriter(System.out)); 133 } 134 135 List parameters = commandLine.getParameters(); 136 if (parameters.size() == 0) { 137 parameters.add("."); 138 } 139 140 Iterator i = parameters.iterator(); 141 while (i.hasNext()) { 142 String filename = (String ) i.next(); 143 144 out.println(filename + ":"); 145 146 ClassfileLoader loader = new AggregatingClassfileLoader(); 147 loader.addLoadListener(verboseListener); 148 loader.load(Collections.singleton(filename)); 149 150 Iterator j = loader.getAllClassfiles().iterator(); 151 while (j.hasNext()) { 152 out.println(j.next()); 153 } 154 155 out.println(); 156 } 157 158 Date end = new Date(); 159 160 if (commandLine.getToggleSwitch("time")) { 161 System.err.println(ClassList.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs."); 162 } 163 164 out.close(); 165 166 verboseListener.close(); 167 } 168 } 169 | Popular Tags |