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 String[] args;
33
34 public CommandLineOptions(String[] args) {
35
36 if (args == null || args.length < 3) {
37 throw new RuntimeException(usage());
38 }
39
40 inputPath = args[0];
41 reportFormat = args[1];
42 ruleSets = new SimpleRuleSetNameMapper(args[2]).getRuleSets();
43
44 this.args = args;
45
46 for (int i = 0; i < args.length; i++) {
47 if (args[i].equals("-debug")) {
48 debugEnabled = true;
49 } else if (args[i].equals("-shortnames")) {
50 shortNamesEnabled = true;
51 } else if (args[i].equals("-encoding")) {
52 encoding = args[i + 1];
53 } else if (args[i].equals("-targetjdk")) {
54 targetJDK = args[i + 1];
55 } else if (args[i].equals("-excludemarker")) {
56 excludeMarker = args[i + 1];
57 }
58 }
59 }
60
61 public Renderer createRenderer() {
62 if (reportFormat.equals("xml")) {
63 return new XMLRenderer();
64 } else if (reportFormat.equals("ideaj")) {
65 return new IDEAJRenderer(args);
66 } else if (reportFormat.equals("papari")) {
67 return new PapariTextRenderer();
68 } else if (reportFormat.equals("text")) {
69 return new TextRenderer();
70 } else if (reportFormat.equals("emacs")) {
71 return new EmacsRenderer();
72 } else if (reportFormat.equals("csv")) {
73 return new CSVRenderer();
74 } else if (reportFormat.equals("html")) {
75 return new HTMLRenderer();
76 } else if (reportFormat.equals("yahtml")) {
77 return new YAHTMLRenderer();
78 } else if (reportFormat.equals("summaryhtml")) {
79 return new SummaryHTMLRenderer();
80 } else if (reportFormat.equals("vbhtml")) {
81 return new VBHTMLRenderer();
82 }
83 if (!reportFormat.equals("")) {
84 try {
85 return (Renderer) Class.forName(reportFormat).newInstance();
86 } catch (Exception e) {
87 throw new IllegalArgumentException("Can't find the custom format " + reportFormat + ": " + e.getClass().getName());
88 }
89 }
90
91 throw new IllegalArgumentException("Can't create report with format of " + reportFormat);
92 }
93
94 public boolean containsCommaSeparatedFileList() {
95 return inputPath.indexOf(',') != -1;
96 }
97
98 public String getInputPath() {
99 return this.inputPath;
100 }
101
102 public String getEncoding() {
103 return this.encoding;
104 }
105
106 public String getReportFormat() {
107 return this.reportFormat;
108 }
109
110 public String getRulesets() {
111 return this.ruleSets;
112 }
113
114 public String getExcludeMarker() {
115 return this.excludeMarker;
116 }
117
118 public boolean debugEnabled() {
119 return debugEnabled;
120 }
121
122 public String getTargetJDK() {
123 return targetJDK;
124 }
125
126 public boolean shortNamesEnabled() {
127 return shortNamesEnabled;
128 }
129
130 public String usage() {
131 return PMD.EOL + PMD.EOL +
132 "Mandatory arguments:" + PMD.EOL +
133 "1) A java source code filename or directory" + PMD.EOL +
134 "2) A report format " + PMD.EOL +
135 "3) A ruleset filename or a comma-delimited string of ruleset filenames" + PMD.EOL +
136 PMD.EOL +
137 "For example: " + PMD.EOL +
138 "c://> java -jar pmd-" + PMD.VERSION + ".jar c://my//source//code html unusedcode" + PMD.EOL +
139 PMD.EOL +
140 "Optional arguments that may be put after the mandatory arguments are: " + PMD.EOL +
141 "-debug: prints debugging information " + PMD.EOL +
142 "-targetjdk: specifies a language version to target - 1.3, 1.4, or 1.5" + PMD.EOL +
143 "-encoding: specifies the character set encoding of the source code files PMD is reading (i.e., UTF-8)" + PMD.EOL +
144 "-excludemarker: specifies the String that marks the a line which PMD should ignore; default is NOPMD" + PMD.EOL +
145 "-shortnames: prints shortened filenames in the report" + PMD.EOL +
146 PMD.EOL +
147 "For example: " + PMD.EOL +
148 "c://> java -jar pmd-" + PMD.VERSION + ".jar c://my//source//code text unusedcode,imports -targetjdk 1.5 -debug" + PMD.EOL +
149 "c://> java -jar pmd-" + PMD.VERSION + ".jar c://my//source//code xml basic,design -encoding UTF-8" + PMD.EOL +
150 PMD.EOL;
151 }
152 }
153
154