View Javadoc

    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd;
    5
    6   import net.sourceforge.pmd.renderers.CSVRenderer;
    7   import net.sourceforge.pmd.renderers.EmacsRenderer;
    8   import net.sourceforge.pmd.renderers.HTMLRenderer;
    9   import net.sourceforge.pmd.renderers.IDEAJRenderer;
    10  import net.sourceforge.pmd.renderers.PapariTextRenderer;
    11  import net.sourceforge.pmd.renderers.Renderer;
    12  import net.sourceforge.pmd.renderers.SummaryHTMLRenderer;
    13  import net.sourceforge.pmd.renderers.TextRenderer;
    14  import net.sourceforge.pmd.renderers.VBHTMLRenderer;
    15  import net.sourceforge.pmd.renderers.XMLRenderer;
    16  import net.sourceforge.pmd.renderers.YAHTMLRenderer;
    17
    18  import java.io.InputStreamReader;
    19
    20  public class CommandLineOptions {
    21
    22      private boolean debugEnabled;
    23      private String targetJDK = "1.4";
    24      private boolean shortNamesEnabled;
    25
    26      private String excludeMarker = ExcludeLines.EXCLUDE_MARKER;
    27      private String inputPath;
    28      private String reportFormat;
    29      private String ruleSets;
    30      private String encoding = new InputStreamReader(System.in).getEncoding();
    31
    32      private boolean checkJavaFiles = true;
    33      private boolean checkJspFiles = false;
    34
    35      private String[] args;
    36
    37      public CommandLineOptions(String[] args) {
    38
    39          if (args == null || args.length < 3) {
    40              throw new RuntimeException(usage());
    41          }
    42
    43          inputPath = args[0];
    44          reportFormat = args[1];
    45          ruleSets = new SimpleRuleSetNameMapper(args[2]).getRuleSets();
    46
    47          this.args = args;
    48
    49          for (int i = 0; i < args.length; i++) {
    50              if (args[i].equals("-debug")) {
    51                  debugEnabled = true;
    52              } else if (args[i].equals("-shortnames")) {
    53                  shortNamesEnabled = true;
    54              } else if (args[i].equals("-encoding")) {
    55                  encoding = args[i + 1];
    56              } else if (args[i].equals("-targetjdk")) {
    57                  targetJDK = args[i + 1];
    58              } else if (args[i].equals("-excludemarker")) {
    59                  excludeMarker = args[i + 1];
    60              } else if (args[i].equals("-jsp")) {
    61                  checkJspFiles = true;
    62              } else if (args[i].equals("-nojava")) {
    63                  checkJavaFiles = false;
    64              }
    65          }
    66      }
    67
    68      public Renderer createRenderer() {
    69          if (reportFormat.equals("xml")) {
    70              return new XMLRenderer();
    71          } else if (reportFormat.equals("ideaj")) {
    72              return new IDEAJRenderer(args);
    73          } else if (reportFormat.equals("papari")) {
    74              return new PapariTextRenderer();
    75          } else if (reportFormat.equals("text")) {
    76              return new TextRenderer();
    77          } else if (reportFormat.equals("emacs")) {
    78              return new EmacsRenderer();
    79          } else if (reportFormat.equals("csv")) {
    80              return new CSVRenderer();
    81          } else if (reportFormat.equals("html")) {
    82              return new HTMLRenderer();
    83          } else if (reportFormat.equals("yahtml")) {
    84              return new YAHTMLRenderer();
    85          } else if (reportFormat.equals("summaryhtml")) {
    86              return new SummaryHTMLRenderer();
    87          } else if (reportFormat.equals("vbhtml")) {
    88              return new VBHTMLRenderer();
    89          }
    90          if (!reportFormat.equals("")) {
    91              try {
    92                  return (Renderer) Class.forName(reportFormat).newInstance();
    93              } catch (Exception e) {
    94                  throw new IllegalArgumentException("Can't find the custom format " + reportFormat + ": " + e.getClass().getName());
    95              }
    96          }
    97
    98          throw new IllegalArgumentException("Can't create report with format of " + reportFormat);
    99      }
    100
    101     public boolean containsCommaSeparatedFileList() {
    102         return inputPath.indexOf(',') != -1;
    103     }
    104
    105     public String getInputPath() {
    106         return this.inputPath;
    107     }
    108
    109     public String getEncoding() {
    110         return this.encoding;
    111     }
    112
    113     public String getReportFormat() {
    114         return this.reportFormat;
    115     }
    116
    117     public String getRulesets() {
    118         return this.ruleSets;
    119     }
    120
    121     public String getExcludeMarker() {
    122         return this.excludeMarker;
    123     }
    124
    125     public boolean debugEnabled() {
    126         return debugEnabled;
    127     }
    128
    129     public String getTargetJDK() {
    130         return targetJDK;
    131     }
    132
    133     public boolean shortNamesEnabled() {
    134         return shortNamesEnabled;
    135     }
    136
    137     public String usage() {
    138         return PMD.EOL + PMD.EOL +
    139                 "Mandatory arguments:" + PMD.EOL +
    140                 "1) A java source code filename or directory" + PMD.EOL +
    141                 "2) A report format " + PMD.EOL +
    142                 "3) A ruleset filename or a comma-delimited string of ruleset filenames" + PMD.EOL +
    143                 PMD.EOL +
    144                 "For example: " + PMD.EOL +
    145                 "c://> java -jar pmd-" + PMD.VERSION + ".jar c://my//source//code html unusedcode" + PMD.EOL +
    146                 PMD.EOL +
    147                 "Optional arguments that may be put after the mandatory arguments are: " + PMD.EOL +
    148                 "-debug: prints debugging information " + PMD.EOL +
    149                 "-targetjdk: specifies a language version to target - 1.3, 1.4, or 1.5" + PMD.EOL +
    150                 "-encoding: specifies the character set encoding of the source code files PMD is reading (i.e., UTF-8)" + PMD.EOL +
    151                 "-excludemarker: specifies the String that marks the a line which PMD should ignore; default is NOPMD" + PMD.EOL +
    152                 "-shortnames: prints shortened filenames in the report" + PMD.EOL +
    153                 PMD.EOL +
    154                 "For example: " + PMD.EOL +
    155                 "c://> java -jar pmd-" + PMD.VERSION + ".jar c://my//source//code text unusedcode,imports -targetjdk 1.5 -debug" + PMD.EOL +
    156                 "c://> java -jar pmd-" + PMD.VERSION + ".jar c://my//source//code xml basic,design -encoding UTF-8" + PMD.EOL +
    157                 PMD.EOL;
    158     }
    159
    160     /***
    161      * @return Returns the checkJavaFiles.
    162      */
    163     public boolean isCheckJavaFiles() {
    164         return checkJavaFiles;
    165     }
    166
    167     /***
    168      * @return Returns the checkJspFiles.
    169      */
    170     public boolean isCheckJspFiles() {
    171         return checkJspFiles;
    172     }
    173 }
    174
    175