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  
17  import java.io.InputStreamReader;
18  
19  public class CommandLineOptions {
20  
21      private boolean debugEnabled;
22      private boolean jdk13;
23      private boolean shortNamesEnabled;
24  
25      private String inputFileName;
26      private String reportFormat;
27      private String ruleSets;
28      private String encoding = new InputStreamReader(System.in).getEncoding();
29  
30      private String[] args;
31  
32      public CommandLineOptions(String[] args) {
33  
34          if (args == null || args.length < 3) {
35              throw new RuntimeException(usage());
36          }
37  
38          inputFileName = args[0];
39          reportFormat = args[1];
40          ruleSets = args[2];
41  
42          this.args = args;
43  
44          for (int i=0; i<args.length; i++) {
45              if (args[i].equals("-debug")) {
46                  debugEnabled = true;
47              } else if (args[i].equals("-shortnames")) {
48                  shortNamesEnabled = true;
49              } else if (args[i].equals("-encoding")) {
50                  encoding = args[i+1];
51              } else if (args[i].equals("-jdk13")) {
52                  jdk13 = true;
53              }
54          }
55      }
56  
57      public Renderer createRenderer() {
58          if (reportFormat.equals("xml")) {
59              return new XMLRenderer();
60          } else if (reportFormat.equals("ideaj")) {
61              return new IDEAJRenderer(args);
62          } else if (reportFormat.equals("papari")) {
63              return new PapariTextRenderer();
64          } else if (reportFormat.equals("text")) {
65              return new TextRenderer();
66          } else if (reportFormat.equals("emacs")) {
67              return new EmacsRenderer();
68          } else if (reportFormat.equals("csv")) {
69              return new CSVRenderer();
70          } else if (reportFormat.equals("html")) {
71              return new HTMLRenderer();
72          }
73          if (reportFormat.equals("summaryhtml")) {
74              return new SummaryHTMLRenderer();
75          }
76          if (reportFormat.equals("vbhtml")) {
77              return new VBHTMLRenderer();
78          }
79          if (!reportFormat.equals("")) {
80              try {
81                  return (Renderer)Class.forName(reportFormat).newInstance();
82              } catch (Exception e) {
83                  throw new IllegalArgumentException("Can't find the custom format " + reportFormat + ": " + e.getClass().getName());
84              }
85          }
86  
87          throw new IllegalArgumentException("Can't create report with format of " + reportFormat);
88      }
89  
90      public boolean containsCommaSeparatedFileList() {
91          return inputFileName.indexOf(',') != -1;
92      }
93  
94      public String getInputFileName() {
95          return this.inputFileName;
96      }
97  
98      public String getEncoding() {
99          return this.encoding;
100     }
101 
102     public String getReportFormat() {
103         return this.reportFormat;
104     }
105 
106     public String getRulesets() {
107         return this.ruleSets;
108     }
109 
110     public boolean debugEnabled() {
111         return debugEnabled;
112     }
113 
114     public boolean jdk13() {
115         return jdk13;
116     }
117 
118     public boolean shortNamesEnabled() {
119         return shortNamesEnabled;
120     }
121 
122     public String usage() {
123         return PMD.EOL + PMD.EOL +
124             "Mandatory arguments:" + PMD.EOL +
125             "1) A java source code filename or directory" + PMD.EOL +
126             "2) A report format " + PMD.EOL +
127             "3) A ruleset filename or a comma-delimited string of ruleset filenames" + PMD.EOL +
128             PMD.EOL +
129             "For example: " + PMD.EOL +
130             "c://> java -jar pmd-1.8.jar c://my//source//code html rulesets/unusedcode.xml,rulesets/imports.xml" + PMD.EOL +
131             PMD.EOL +
132             "Optional arguments that may be put after the mandatory arguments are: " + PMD.EOL +
133             "-debug: prints debugging information " + PMD.EOL +
134             "-jdk13: enables PMD to parse source code written using 'assert' as an identifier" + PMD.EOL +
135             "-encoding: specifies the character set encoding of the source code files PMD is reading (i.e., UTF-8)" + PMD.EOL +
136             "-shortnames: prints shortened filenames in the report" + PMD.EOL +
137             PMD.EOL +
138             "For example: " + PMD.EOL +
139             "c://> java -jar pmd-1.8.jar c://my//source//code html rulesets/unusedcode.xml,rulesets/imports.xml -jdk13 -debug" + PMD.EOL +
140             "c://> java -jar pmd-1.8.jar c://my//source//code html rulesets/unusedcode.xml,rulesets/imports.xml -encoding UTF-8" + PMD.EOL +
141             PMD.EOL;
142     }
143 }
144 
145