| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package com.puppycrawl.tools.checkstyle; |
| 20 | |
|
| 21 | |
import com.puppycrawl.tools.checkstyle.api.Utils; |
| 22 | |
|
| 23 | |
import com.google.common.collect.Lists; |
| 24 | |
import com.puppycrawl.tools.checkstyle.api.AuditListener; |
| 25 | |
import com.puppycrawl.tools.checkstyle.api.CheckstyleException; |
| 26 | |
import com.puppycrawl.tools.checkstyle.api.Configuration; |
| 27 | |
import java.io.File; |
| 28 | |
import java.io.FileInputStream; |
| 29 | |
import java.io.FileNotFoundException; |
| 30 | |
import java.io.FileOutputStream; |
| 31 | |
import java.io.IOException; |
| 32 | |
import java.io.OutputStream; |
| 33 | |
import java.util.List; |
| 34 | |
import java.util.Properties; |
| 35 | |
import org.apache.commons.cli.CommandLine; |
| 36 | |
import org.apache.commons.cli.CommandLineParser; |
| 37 | |
import org.apache.commons.cli.HelpFormatter; |
| 38 | |
import org.apache.commons.cli.Options; |
| 39 | |
import org.apache.commons.cli.ParseException; |
| 40 | |
import org.apache.commons.cli.PosixParser; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 0 | public final class Main |
| 47 | |
{ |
| 48 | |
|
| 49 | 0 | private static final Options OPTS = new Options(); |
| 50 | |
static { |
| 51 | 0 | OPTS.addOption("c", true, "The check configuration file to use."); |
| 52 | 0 | OPTS.addOption("r", true, "Traverse the directory for source files"); |
| 53 | 0 | OPTS.addOption("o", true, "Sets the output file. Defaults to stdout"); |
| 54 | 0 | OPTS.addOption("p", true, "Loads the properties file"); |
| 55 | 0 | OPTS.addOption( |
| 56 | |
"f", |
| 57 | |
true, |
| 58 | |
"Sets the output format. (plain|xml). Defaults to plain"); |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
|
| 62 | |
private Main() |
| 63 | 0 | { |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public static void main(String[] aArgs) |
| 72 | |
{ |
| 73 | |
|
| 74 | 0 | final CommandLineParser clp = new PosixParser(); |
| 75 | 0 | CommandLine line = null; |
| 76 | |
try { |
| 77 | 0 | line = clp.parse(OPTS, aArgs); |
| 78 | |
} |
| 79 | 0 | catch (final ParseException e) { |
| 80 | 0 | e.printStackTrace(); |
| 81 | 0 | usage(); |
| 82 | 0 | } |
| 83 | 0 | assert line != null; |
| 84 | |
|
| 85 | |
|
| 86 | 0 | final Properties props = |
| 87 | |
line.hasOption("p") |
| 88 | |
? loadProperties(new File(line.getOptionValue("p"))) |
| 89 | |
: System.getProperties(); |
| 90 | |
|
| 91 | |
|
| 92 | 0 | if (!line.hasOption("c")) { |
| 93 | 0 | System.out.println("Must specify a config XML file."); |
| 94 | 0 | usage(); |
| 95 | |
} |
| 96 | |
|
| 97 | 0 | final Configuration config = loadConfig(line, props); |
| 98 | |
|
| 99 | |
|
| 100 | 0 | OutputStream out = null; |
| 101 | 0 | boolean closeOut = false; |
| 102 | 0 | if (line.hasOption("o")) { |
| 103 | 0 | final String fname = line.getOptionValue("o"); |
| 104 | |
try { |
| 105 | 0 | out = new FileOutputStream(fname); |
| 106 | 0 | closeOut = true; |
| 107 | |
} |
| 108 | 0 | catch (final FileNotFoundException e) { |
| 109 | 0 | System.out.println("Could not find file: '" + fname + "'"); |
| 110 | 0 | System.exit(1); |
| 111 | 0 | } |
| 112 | 0 | } |
| 113 | |
else { |
| 114 | 0 | out = System.out; |
| 115 | 0 | closeOut = false; |
| 116 | |
} |
| 117 | |
|
| 118 | 0 | final AuditListener listener = createListener(line, out, closeOut); |
| 119 | 0 | final List<File> files = getFilesToProcess(line); |
| 120 | 0 | final Checker c = createChecker(config, listener); |
| 121 | 0 | final int numErrs = c.process(files); |
| 122 | 0 | c.destroy(); |
| 123 | 0 | System.exit(numErrs); |
| 124 | 0 | } |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
private static Checker createChecker(Configuration aConfig, |
| 134 | |
AuditListener aNosy) |
| 135 | |
{ |
| 136 | 0 | Checker c = null; |
| 137 | |
try { |
| 138 | 0 | c = new Checker(); |
| 139 | |
|
| 140 | 0 | final ClassLoader moduleClassLoader = |
| 141 | |
Checker.class.getClassLoader(); |
| 142 | 0 | c.setModuleClassLoader(moduleClassLoader); |
| 143 | 0 | c.configure(aConfig); |
| 144 | 0 | c.addListener(aNosy); |
| 145 | |
} |
| 146 | 0 | catch (final Exception e) { |
| 147 | 0 | System.out.println("Unable to create Checker: " |
| 148 | |
+ e.getMessage()); |
| 149 | 0 | e.printStackTrace(System.out); |
| 150 | 0 | System.exit(1); |
| 151 | 0 | } |
| 152 | 0 | return c; |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
private static List<File> getFilesToProcess(CommandLine aLine) |
| 162 | |
{ |
| 163 | 0 | final List<File> files = Lists.newLinkedList(); |
| 164 | 0 | if (aLine.hasOption("r")) { |
| 165 | 0 | final String[] values = aLine.getOptionValues("r"); |
| 166 | 0 | for (String element : values) { |
| 167 | 0 | traverse(new File(element), files); |
| 168 | |
} |
| 169 | |
} |
| 170 | |
|
| 171 | 0 | final String[] remainingArgs = aLine.getArgs(); |
| 172 | 0 | for (String element : remainingArgs) { |
| 173 | 0 | files.add(new File(element)); |
| 174 | |
} |
| 175 | |
|
| 176 | 0 | if (files.isEmpty() && !aLine.hasOption("r")) { |
| 177 | 0 | System.out.println("Must specify files to process"); |
| 178 | 0 | usage(); |
| 179 | |
} |
| 180 | 0 | return files; |
| 181 | |
} |
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
private static AuditListener createListener(CommandLine aLine, |
| 192 | |
OutputStream aOut, |
| 193 | |
boolean aCloseOut) |
| 194 | |
{ |
| 195 | 0 | final String format = |
| 196 | |
aLine.hasOption("f") ? aLine.getOptionValue("f") : "plain"; |
| 197 | |
|
| 198 | 0 | AuditListener listener = null; |
| 199 | 0 | if ("xml".equals(format)) { |
| 200 | 0 | listener = new XMLLogger(aOut, aCloseOut); |
| 201 | |
} |
| 202 | 0 | else if ("plain".equals(format)) { |
| 203 | 0 | listener = new DefaultLogger(aOut, aCloseOut); |
| 204 | |
} |
| 205 | |
else { |
| 206 | 0 | System.out.println("Invalid format: (" + format |
| 207 | |
+ "). Must be 'plain' or 'xml'."); |
| 208 | 0 | usage(); |
| 209 | |
} |
| 210 | 0 | return listener; |
| 211 | |
} |
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
private static Configuration loadConfig(CommandLine aLine, |
| 221 | |
Properties aProps) |
| 222 | |
{ |
| 223 | |
try { |
| 224 | 0 | return ConfigurationLoader.loadConfiguration( |
| 225 | |
aLine.getOptionValue("c"), new PropertiesExpander(aProps)); |
| 226 | |
} |
| 227 | 0 | catch (final CheckstyleException e) { |
| 228 | 0 | System.out.println("Error loading configuration file"); |
| 229 | 0 | e.printStackTrace(System.out); |
| 230 | 0 | System.exit(1); |
| 231 | 0 | return null; |
| 232 | |
} |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
private static void usage() |
| 237 | |
{ |
| 238 | 0 | final HelpFormatter hf = new HelpFormatter(); |
| 239 | 0 | hf.printHelp( |
| 240 | |
"java " |
| 241 | |
+ Main.class.getName() |
| 242 | |
+ " [options] -c <config.xml> file...", |
| 243 | |
OPTS); |
| 244 | 0 | System.exit(1); |
| 245 | 0 | } |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
private static void traverse(File aNode, List<File> aFiles) |
| 256 | |
{ |
| 257 | 0 | if (aNode.canRead()) { |
| 258 | 0 | if (aNode.isDirectory()) { |
| 259 | 0 | final File[] nodes = aNode.listFiles(); |
| 260 | 0 | for (File element : nodes) { |
| 261 | 0 | traverse(element, aFiles); |
| 262 | |
} |
| 263 | 0 | } |
| 264 | 0 | else if (aNode.isFile()) { |
| 265 | 0 | aFiles.add(aNode); |
| 266 | |
} |
| 267 | |
} |
| 268 | 0 | } |
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
private static Properties loadProperties(File aFile) |
| 276 | |
{ |
| 277 | 0 | final Properties properties = new Properties(); |
| 278 | 0 | FileInputStream fis = null; |
| 279 | |
try { |
| 280 | 0 | fis = new FileInputStream(aFile); |
| 281 | 0 | properties.load(fis); |
| 282 | |
} |
| 283 | 0 | catch (final IOException ex) { |
| 284 | 0 | System.out.println("Unable to load properties from file: " |
| 285 | |
+ aFile.getAbsolutePath()); |
| 286 | 0 | ex.printStackTrace(System.out); |
| 287 | 0 | System.exit(1); |
| 288 | |
} |
| 289 | |
finally { |
| 290 | 0 | Utils.closeQuietly(fis); |
| 291 | 0 | } |
| 292 | |
|
| 293 | 0 | return properties; |
| 294 | |
} |
| 295 | |
} |