001: package org.acm.seguin.pmd;
002:
003: import org.acm.seguin.pmd.renderers.Renderer;
004: import org.acm.seguin.pmd.renderers.XMLRenderer;
005: import org.acm.seguin.pmd.renderers.IDEAJRenderer;
006: import org.acm.seguin.pmd.renderers.TextRenderer;
007: import org.acm.seguin.pmd.renderers.EmacsRenderer;
008: import org.acm.seguin.pmd.renderers.CSVRenderer;
009: import org.acm.seguin.pmd.renderers.HTMLRenderer;
010:
011: public class CommandLineOptions {
012:
013: private boolean debugEnabled;
014: private boolean shortNamesEnabled;
015:
016: private String inputFileName;
017: private String reportFormat;
018: private String ruleSets;
019:
020: private String[] args;
021:
022: public CommandLineOptions(String[] args) {
023:
024: if (args == null || args.length < 3) {
025: throw new RuntimeException(usage());
026: }
027:
028: inputFileName = args[0];
029: reportFormat = args[1];
030: ruleSets = args[2];
031:
032: this .args = args;
033:
034: for (int i = 0; i < args.length; i++) {
035: if (args[i].equals("-debug")) {
036: debugEnabled = true;
037: }
038: if (args[i].equals("-shortnames")) {
039: shortNamesEnabled = true;
040: }
041: }
042: }
043:
044: public Renderer createRenderer() {
045: if (reportFormat.equals("xml")) {
046: return new XMLRenderer();
047: }
048: if (reportFormat.equals("ideaj")) {
049: return new IDEAJRenderer(args);
050: }
051: if (reportFormat.equals("text")) {
052: return new TextRenderer();
053: }
054: if (reportFormat.equals("emacs")) {
055: return new EmacsRenderer();
056: }
057: if (reportFormat.equals("csv")) {
058: return new CSVRenderer();
059: }
060: if (reportFormat.equals("html")) {
061: return new HTMLRenderer();
062: }
063: if (!reportFormat.equals("")) {
064: try {
065: return (Renderer) Class.forName(reportFormat)
066: .newInstance();
067: } catch (Exception e) {
068: throw new IllegalArgumentException(
069: "Can't find the custom format " + reportFormat
070: + ": " + e.getClass().getName());
071: }
072: }
073:
074: throw new IllegalArgumentException(
075: "Can't create report with format of " + reportFormat);
076: }
077:
078: public boolean containsCommaSeparatedFileList() {
079: return inputFileName.indexOf(',') != -1;
080: }
081:
082: public String getInputFileName() {
083: return this .inputFileName;
084: }
085:
086: public String getReportFormat() {
087: return this .reportFormat;
088: }
089:
090: public String getRulesets() {
091: return this .ruleSets;
092: }
093:
094: public boolean debugEnabled() {
095: return debugEnabled;
096: }
097:
098: public boolean shortNamesEnabled() {
099: return shortNamesEnabled;
100: }
101:
102: public String usage() {
103: return PMD.EOL
104: + "Please pass in a java source code filename or directory, a report format, "
105: + PMD.EOL
106: + "and a ruleset filename or a comma-delimited string of ruleset filenames."
107: + PMD.EOL
108: + "For example: "
109: + PMD.EOL
110: + "c:\\> java -jar pmd-1.2.1.jar c:\\my\\source\\code html rulesets/unusedcode.xml,rulesets/imports.xml"
111: + PMD.EOL;
112: }
113: }
|