1 package com.puppycrawl.tools.checkstyle; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.util.List ; 28 import java.util.Properties ; 29 import java.util.LinkedList ; 30 31 import org.apache.commons.cli.CommandLine; 32 import org.apache.commons.cli.CommandLineParser; 33 import org.apache.commons.cli.HelpFormatter; 34 import org.apache.commons.cli.Options; 35 import org.apache.commons.cli.ParseException; 36 import org.apache.commons.cli.PosixParser; 37 38 import com.puppycrawl.tools.checkstyle.api.AuditListener; 39 import com.puppycrawl.tools.checkstyle.api.Configuration; 40 41 import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 42 43 47 public final class Main 48 { 49 50 private static final Options OPTS = new Options(); 51 static { 52 OPTS.addOption("c", true, "The check configuration file to use."); 53 OPTS.addOption("r", true, "Traverse the directory for source files"); 54 OPTS.addOption("o", true, "Sets the output file. Defaults to stdout"); 55 OPTS.addOption("p", true, "Loads the properties file"); 56 OPTS.addOption("n", true, "Loads the package names file"); 57 OPTS.addOption( 58 "f", 59 true, 60 "Sets the output format. (plain|xml). Defaults to plain"); 61 } 62 63 68 public static void main(String [] aArgs) 69 { 70 final CommandLineParser clp = new PosixParser(); 72 CommandLine line = null; 73 try { 74 line = clp.parse(OPTS, aArgs); 75 } 76 catch (final ParseException e) { 77 e.printStackTrace(); 78 usage(); 79 } 80 assert line != null; 81 82 final Properties props = 84 line.hasOption("p") 85 ? loadProperties(new File (line.getOptionValue("p"))) 86 : System.getProperties(); 87 88 if (!line.hasOption("c")) { 90 System.out.println("Must specify a config XML file."); 91 usage(); 92 } 93 94 final Configuration config = loadConfig(line, props); 95 96 ModuleFactory moduleFactory = null; 98 if (line.hasOption("n")) { 99 moduleFactory = loadPackages(line); 100 } 101 102 OutputStream out = null; 104 boolean closeOut = false; 105 if (line.hasOption("o")) { 106 final String fname = line.getOptionValue("o"); 107 try { 108 out = new FileOutputStream (fname); 109 closeOut = true; 110 } 111 catch (final FileNotFoundException e) { 112 System.out.println("Could not find file: '" + fname + "'"); 113 System.exit(1); 114 } 115 } 116 else { 117 out = System.out; 118 closeOut = false; 119 } 120 121 final AuditListener listener = createListener(line, out, closeOut); 122 final List files = getFilesToProcess(line); 123 final Checker c = createChecker(config, moduleFactory, listener); 124 125 final File [] processedFiles = new File [files.size()]; 126 files.toArray(processedFiles); 127 final int numErrs = c.process(processedFiles); 128 c.destroy(); 129 System.exit(numErrs); 130 } 131 132 140 private static Checker createChecker(Configuration aConfig, 141 ModuleFactory aFactory, 142 AuditListener aNosy) 143 { 144 Checker c = null; 145 try { 146 c = new Checker(); 147 c.setModuleFactory(aFactory); 148 c.configure(aConfig); 149 c.addListener(aNosy); 150 } 151 catch (final Exception e) { 152 System.out.println("Unable to create Checker: " 153 + e.getMessage()); 154 e.printStackTrace(System.out); 155 System.exit(1); 156 } 157 return c; 158 } 159 160 166 private static List getFilesToProcess(CommandLine aLine) 167 { 168 final List files = new LinkedList (); 169 if (aLine.hasOption("r")) { 170 final String [] values = aLine.getOptionValues("r"); 171 for (int i = 0; i < values.length; i++) { 172 traverse(new File (values[i]), files); 173 } 174 } 175 176 final String [] remainingArgs = aLine.getArgs(); 177 for (int i = 0; i < remainingArgs.length; i++) { 178 files.add(new File (remainingArgs[i])); 179 } 180 181 if (files.isEmpty()) { 182 System.out.println("Must specify files to process"); 183 usage(); 184 } 185 return files; 186 } 187 188 196 private static AuditListener createListener(CommandLine aLine, 197 OutputStream aOut, 198 boolean aCloseOut) 199 { 200 final String format = 201 aLine.hasOption("f") ? aLine.getOptionValue("f") : "plain"; 202 203 AuditListener listener = null; 204 if ("xml".equals(format)) { 205 listener = new XMLLogger(aOut, aCloseOut); 206 } 207 else if ("plain".equals(format)) { 208 listener = new DefaultLogger(aOut, aCloseOut); 209 } 210 else { 211 System.out.println("Invalid format: (" + format 212 + "). Must be 'plain' or 'xml'."); 213 usage(); 214 } 215 return listener; 216 } 217 218 224 private static ModuleFactory loadPackages(CommandLine aLine) 225 { 226 try { 227 return PackageNamesLoader.loadModuleFactory( 228 aLine.getOptionValue("n")); 229 } 230 catch (final CheckstyleException e) { 231 System.out.println("Error loading package names file"); 232 e.printStackTrace(System.out); 233 System.exit(1); 234 return null; } 236 } 237 238 245 private static Configuration loadConfig(CommandLine aLine, 246 Properties aProps) 247 { 248 try { 249 return ConfigurationLoader.loadConfiguration( 250 aLine.getOptionValue("c"), new PropertiesExpander(aProps)); 251 } 252 catch (final CheckstyleException e) { 253 System.out.println("Error loading configuration file"); 254 e.printStackTrace(System.out); 255 System.exit(1); 256 return null; } 258 } 259 260 261 private static void usage() 262 { 263 final HelpFormatter hf = new HelpFormatter(); 264 hf.printHelp( 265 "java " 266 + Main.class.getName() 267 + " [options] -c <config.xml> file...", 268 OPTS); 269 System.exit(1); 270 } 271 272 280 private static void traverse(File aNode, List aFiles) 281 { 282 if (aNode.canRead()) { 283 if (aNode.isDirectory()) { 284 final File [] nodes = aNode.listFiles(); 285 for (int i = 0; i < nodes.length; i++) { 286 traverse(nodes[i], aFiles); 287 } 288 } 289 else if (aNode.isFile()) { 290 aFiles.add(aNode); 291 } 292 } 293 } 294 295 300 private static Properties loadProperties(File aFile) 301 { 302 final Properties properties = new Properties (); 303 try { 304 FileInputStream fis = null; 305 fis = new FileInputStream (aFile); 306 properties.load(fis); 307 fis.close(); 308 } 309 catch (final IOException ex) { 310 System.out.println("Unable to load properties from file: " 311 + aFile.getAbsolutePath()); 312 ex.printStackTrace(System.out); 313 System.exit(1); 314 } 315 return properties; 316 } 317 } 318 | Popular Tags |