Coverage Report - com.puppycrawl.tools.checkstyle.Main
 
Classes in this File Line Coverage Branch Coverage Complexity
Main
0%
0/106
0%
0/34
3.333
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 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  
  * Wrapper command line program for the Checker.
 44  
  * @author Oliver Burn
 45  
  **/
 46  0
 public final class Main
 47  
 {
 48  
     /** the options to the command line */
 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  
     /** Stop instances being created. */
 62  
     private Main()
 63  0
     {
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Loops over the files specified checking them for errors. The exit code
 68  
      * is the number of errors found in all the files.
 69  
      * @param aArgs the command line arguments
 70  
      **/
 71  
     public static void main(String[] aArgs)
 72  
     {
 73  
         // parse the parameters
 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  
         // setup the properties
 86  0
         final Properties props =
 87  
             line.hasOption("p")
 88  
                 ? loadProperties(new File(line.getOptionValue("p")))
 89  
                 : System.getProperties();
 90  
 
 91  
         // ensure a config file is specified
 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  
         // setup the output stream
 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  
      * Creates the Checker object.
 128  
      *
 129  
      * @param aConfig the configuration to use
 130  
      * @param aNosy the sticky beak to track what happens
 131  
      * @return a nice new fresh Checker
 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  
      * Determines the files to process.
 157  
      *
 158  
      * @param aLine the command line options specifying what files to process
 159  
      * @return list of files to process
 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  
      * Create the audit listener
 185  
      *
 186  
      * @param aLine command line options supplied
 187  
      * @param aOut the stream to log to
 188  
      * @param aCloseOut whether the stream should be closed
 189  
      * @return a fresh new <code>AuditListener</code>
 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  
      * Loads the configuration file. Will exit if unable to load.
 215  
      *
 216  
      * @param aLine specifies the location of the configuration
 217  
      * @param aProps the properties to resolve with the configuration
 218  
      * @return a fresh new configuration
 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; // can never get here
 232  
         }
 233  
     }
 234  
 
 235  
     /** Prints the usage information. **/
 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  
      * Traverses a specified node looking for files to check. Found
 249  
      * files are added to a specified list. Subdirectories are also
 250  
      * traversed.
 251  
      *
 252  
      * @param aNode the node to process
 253  
      * @param aFiles list to add found files to
 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  
      * Loads properties from a File.
 272  
      * @param aFile the properties file
 273  
      * @return the properties in aFile
 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  
 }