001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package net.sourceforge.pmd;
004:
005: import net.sourceforge.pmd.renderers.CSVRenderer;
006: import net.sourceforge.pmd.renderers.EmacsRenderer;
007: import net.sourceforge.pmd.renderers.HTMLRenderer;
008: import net.sourceforge.pmd.renderers.IDEAJRenderer;
009: import net.sourceforge.pmd.renderers.PapariTextRenderer;
010: import net.sourceforge.pmd.renderers.Renderer;
011: import net.sourceforge.pmd.renderers.SummaryHTMLRenderer;
012: import net.sourceforge.pmd.renderers.TextRenderer;
013: import net.sourceforge.pmd.renderers.VBHTMLRenderer;
014: import net.sourceforge.pmd.renderers.XMLRenderer;
015: import net.sourceforge.pmd.renderers.XSLTRenderer;
016: import net.sourceforge.pmd.renderers.YAHTMLRenderer;
017:
018: import java.io.InputStreamReader;
019: import java.text.MessageFormat;
020:
021: public class CommandLineOptions {
022:
023: private boolean debugEnabled;
024: private boolean stressTestEnabled;
025: private String targetJDK = "1.5";
026: private boolean shortNamesEnabled;
027: private int cpus = Runtime.getRuntime().availableProcessors();
028:
029: private String excludeMarker = PMD.EXCLUDE_MARKER;
030: private String inputPath;
031: private String reportFormat;
032: private String reportFile;
033: private String ruleSets;
034: private String encoding = new InputStreamReader(System.in)
035: .getEncoding();
036: private String linePrefix;
037: private String linkPrefix;
038: private int minPriority = Rule.LOWEST_PRIORITY;
039: private boolean benchmark;
040:
041: private boolean checkJavaFiles = true;
042: private boolean checkJspFiles;
043:
044: private String[] args;
045: private String xsltFilename;
046:
047: public CommandLineOptions(String[] args) {
048:
049: this .args = args; // needed by createRenderer
050:
051: if (args == null || args.length < 3) {
052: throw new RuntimeException(usage());
053: }
054: int optIndex = 0;
055: if (args[0].charAt(0) == '-') {
056: optIndex = args.length - 3;
057: }
058:
059: inputPath = args[optIndex];
060: reportFormat = args[optIndex + 1];
061: ruleSets = new SimpleRuleSetNameMapper(args[optIndex + 2])
062: .getRuleSets();
063:
064: for (int i = 0; i < args.length; i++) {
065: if (args[i].equals("-debug")) {
066: debugEnabled = true;
067: } else if (args[i].equals("-stress")) {
068: stressTestEnabled = true;
069: } else if (args[i].equals("-shortnames")) {
070: shortNamesEnabled = true;
071: } else if (args[i].equals("-encoding")) {
072: encoding = args[++i];
073: } else if (args[i].equals("-cpus")) {
074: try {
075: cpus = Integer.parseInt(args[++i]);
076: } catch (NumberFormatException e) {
077: throw new RuntimeException(
078: MessageFormat
079: .format(
080: "cpus parameter must be a whole number, {0} received",
081: args[i]));
082: }
083: } else if (args[i].equals("-targetjdk")) {
084: targetJDK = args[++i];
085: } else if (args[i].equals("-excludemarker")) {
086: excludeMarker = args[++i];
087: } else if (args[i].equals("-jsp")) {
088: checkJspFiles = true;
089: } else if (args[i].equals("-nojava")) {
090: checkJavaFiles = false;
091: } else if (args[i].equals("-lineprefix")) {
092: linePrefix = args[++i];
093: } else if (args[i].equals("-linkprefix")) {
094: linkPrefix = args[++i];
095: } else if (args[i].equals("-minimumpriority")) {
096: try {
097: minPriority = Integer.parseInt(args[++i]);
098: } catch (NumberFormatException e) {
099: throw new RuntimeException(
100: MessageFormat
101: .format(
102: "minimumpriority parameter must be a whole number, {0} received",
103: args[i]));
104: }
105: } else if (args[i].equals("-reportfile")) {
106: reportFile = args[++i];
107: } else if (args[i].equals("-benchmark")) {
108: benchmark = true;
109: } else if (args[i].equals("-xslt")) {
110: i++;
111: if (i >= args.length) {
112: throw new RuntimeException(usage());
113: }
114: this .xsltFilename = args[i];
115: }
116:
117: }
118: }
119:
120: public Renderer createRenderer() {
121: if (reportFormat.equals("xml")) {
122: return new XMLRenderer();
123: } else if (reportFormat.equals("ideaj")) {
124: return new IDEAJRenderer(args);
125: } else if (reportFormat.equals("papari")) {
126: return new PapariTextRenderer();
127: } else if (reportFormat.equals("text")) {
128: return new TextRenderer();
129: } else if (reportFormat.equals("emacs")) {
130: return new EmacsRenderer();
131: } else if (reportFormat.equals("csv")) {
132: return new CSVRenderer();
133: } else if (reportFormat.equals("html")) {
134: return new HTMLRenderer();
135: } else if (reportFormat.equals("nicehtml")) {
136: return new XSLTRenderer(this .xsltFilename);
137: } else if (reportFormat.equals("yahtml")) {
138: return new YAHTMLRenderer();
139: } else if (reportFormat.equals("summaryhtml")) {
140: return new SummaryHTMLRenderer(linkPrefix, linePrefix);
141: } else if (reportFormat.equals("vbhtml")) {
142: return new VBHTMLRenderer();
143: }
144: if (!reportFormat.equals("")) {
145: try {
146: return (Renderer) Class.forName(reportFormat)
147: .newInstance();
148: } catch (Exception e) {
149: throw new IllegalArgumentException(
150: "Can't find the custom format " + reportFormat
151: + ": " + e.getClass().getName());
152: }
153: }
154:
155: throw new IllegalArgumentException(
156: "Can't create report with format of " + reportFormat);
157: }
158:
159: public boolean containsCommaSeparatedFileList() {
160: return inputPath.indexOf(',') != -1;
161: }
162:
163: public String getInputPath() {
164: return this .inputPath;
165: }
166:
167: public String getEncoding() {
168: return this .encoding;
169: }
170:
171: public String getReportFormat() {
172: return this .reportFormat;
173: }
174:
175: public String getReportFile() {
176: return this .reportFile;
177: }
178:
179: public String getRulesets() {
180: return this .ruleSets;
181: }
182:
183: public String getExcludeMarker() {
184: return this .excludeMarker;
185: }
186:
187: public boolean debugEnabled() {
188: return debugEnabled;
189: }
190:
191: public boolean stressTestEnabled() {
192: return stressTestEnabled;
193: }
194:
195: public int getCpus() {
196: return cpus;
197: }
198:
199: public String getTargetJDK() {
200: return targetJDK;
201: }
202:
203: public boolean shortNamesEnabled() {
204: return shortNamesEnabled;
205: }
206:
207: public int getMinPriority() {
208: return minPriority;
209: }
210:
211: public boolean benchmark() {
212: return benchmark;
213: }
214:
215: public String usage() {
216: return PMD.EOL
217: + PMD.EOL
218: + "Mandatory arguments:"
219: + PMD.EOL
220: + "1) A java source code filename or directory"
221: + PMD.EOL
222: + "2) A report format "
223: + PMD.EOL
224: + "3) A ruleset filename or a comma-delimited string of ruleset filenames"
225: + PMD.EOL
226: + PMD.EOL
227: + "For example: "
228: + PMD.EOL
229: + "c:\\> java -jar pmd-"
230: + PMD.VERSION
231: + ".jar c:\\my\\source\\code html unusedcode"
232: + PMD.EOL
233: + PMD.EOL
234: + "Optional arguments that may be put before or after the mandatory arguments: "
235: + PMD.EOL
236: + "-debug: prints debugging information"
237: + PMD.EOL
238: + "-targetjdk: specifies a language version to target - 1.3, 1.4, 1.5, 1.6 or 1.7; default is 1.5"
239: + PMD.EOL
240: + "-cpus: specifies the number of threads to create"
241: + PMD.EOL
242: + "-encoding: specifies the character set encoding of the source code files PMD is reading (i.e., UTF-8)"
243: + PMD.EOL
244: + "-excludemarker: specifies the String that marks the a line which PMD should ignore; default is NOPMD"
245: + PMD.EOL
246: + "-shortnames: prints shortened filenames in the report"
247: + PMD.EOL
248: + "-linkprefix: path to HTML source, for summary html renderer only"
249: + PMD.EOL
250: + "-lineprefix: custom anchor to affected line in the source file, for summary html renderer only"
251: + PMD.EOL
252: + "-minimumpriority: rule priority threshold; rules with lower priority than they will not be used"
253: + PMD.EOL
254: + "-nojava: do not check Java files; default to check Java files"
255: + PMD.EOL
256: + "-jsp: check JSP/JSF files; default to do not check JSP/JSF files"
257: + PMD.EOL
258: + "-reportfile: send report output to a file; default to System.out"
259: + PMD.EOL
260: + "-benchmark: output a benchmark report upon completion; default to System.err"
261: + PMD.EOL
262: + "-xslt: override default xslt for 'nicehtml' output."
263: + PMD.EOL
264: + PMD.EOL
265: + "For example on windows: "
266: + PMD.EOL
267: + "c:\\> java -jar pmd-"
268: + PMD.VERSION
269: + ".jar c:\\my\\source\\code text unusedcode,imports -targetjdk 1.5 -debug"
270: + PMD.EOL
271: + "c:\\> java -jar pmd-"
272: + PMD.VERSION
273: + ".jar c:\\my\\source\\code xml basic,design -encoding UTF-8"
274: + PMD.EOL
275: + PMD.EOL
276: + "For example on *nix: "
277: + PMD.EOL
278: + "$ java -jar pmd-"
279: + PMD.VERSION
280: + ".jar /home/workspace/src/main/java code nicehtml basic,design"
281: + PMD.EOL
282: + "$ java -jar pmd-"
283: + PMD.VERSION
284: + ".jar /home/workspace/src/main/java code nicehtml basic,design -xslt my-own.xsl"
285: + PMD.EOL + PMD.EOL;
286: }
287:
288: public boolean isCheckJavaFiles() {
289: return checkJavaFiles;
290: }
291:
292: public boolean isCheckJspFiles() {
293: return checkJspFiles;
294: }
295: }
|