1 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 import java.text.MessageFormat ; 20 21 public class CommandLineOptions { 22 23 private boolean debugEnabled; 24 private String targetJDK = "1.4"; 25 private boolean shortNamesEnabled; 26 private int cpus = Runtime.getRuntime().availableProcessors(); 27 28 private String excludeMarker = PMD.EXCLUDE_MARKER; 29 private String inputPath; 30 private String reportFormat; 31 private String reportFile; 32 private String ruleSets; 33 private String encoding = new InputStreamReader (System.in).getEncoding(); 34 private String linePrefix; 35 private String linkPrefix; 36 private int minPriority = Rule.LOWEST_PRIORITY; 37 38 39 private boolean checkJavaFiles = true; 40 private boolean checkJspFiles = false; 41 42 private String [] args; 43 44 public CommandLineOptions(String [] args) { 45 46 if (args == null || args.length < 3) { 47 throw new RuntimeException (usage()); 48 } 49 int optIndex = 0; 50 if (args[0].charAt(0) == '-') { 51 optIndex = args.length - 3; 52 } 53 54 inputPath = args[optIndex]; 55 reportFormat = args[optIndex+1]; 56 ruleSets = new SimpleRuleSetNameMapper(args[optIndex+2]).getRuleSets(); 57 58 this.args = args; 59 60 for (int i = 0; i < args.length; i++) { 61 if (args[i].equals("-debug")) { 62 debugEnabled = true; 63 } else if (args[i].equals("-shortnames")) { 64 shortNamesEnabled = true; 65 } else if (args[i].equals("-encoding")) { 66 encoding = args[++i]; 67 } else if (args[i].equals("-cpus")) { 68 try { 69 cpus = Integer.parseInt(args[++i]); 70 } catch (NumberFormatException e) { 71 throw new RuntimeException (MessageFormat.format( 72 "cpus parameter must be a whole number, {0} received", 73 new String [] { args[i] })); 74 } 75 } else if (args[i].equals("-targetjdk")) { 76 targetJDK = args[++i]; 77 } else if (args[i].equals("-excludemarker")) { 78 excludeMarker = args[++i]; 79 } else if (args[i].equals("-jsp")) { 80 checkJspFiles = true; 81 } else if (args[i].equals("-nojava")) { 82 checkJavaFiles = false; 83 } else if (args[i].equals("-lineprefix")) { 84 linePrefix = args[++i]; 85 } else if (args[i].equals("-linkprefix")) { 86 linkPrefix = args[++i]; 87 } else if (args[i].equals("-minimumpriority")) { 88 try { 89 minPriority = Integer.parseInt(args[++i]); 90 } catch (NumberFormatException e) { 91 throw new RuntimeException (MessageFormat.format( 92 "minimumpriority parameter must be a whole number, {0} received", 93 new String [] { args[i] })); 94 } 95 } else if (args[i].equals("-reportfile")) { 96 reportFile = args[++i]; 97 } 98 } 99 } 100 101 public Renderer createRenderer() { 102 if (reportFormat.equals("xml")) { 103 return new XMLRenderer(); 104 } else if (reportFormat.equals("ideaj")) { 105 return new IDEAJRenderer(args); 106 } else if (reportFormat.equals("papari")) { 107 return new PapariTextRenderer(); 108 } else if (reportFormat.equals("text")) { 109 return new TextRenderer(); 110 } else if (reportFormat.equals("emacs")) { 111 return new EmacsRenderer(); 112 } else if (reportFormat.equals("csv")) { 113 return new CSVRenderer(); 114 } else if (reportFormat.equals("html")) { 115 return new HTMLRenderer(); 116 } else if (reportFormat.equals("yahtml")) { 117 return new YAHTMLRenderer(); 118 } else if (reportFormat.equals("summaryhtml")) { 119 return new SummaryHTMLRenderer(linkPrefix, linePrefix); 120 } else if (reportFormat.equals("vbhtml")) { 121 return new VBHTMLRenderer(); 122 } 123 if (!reportFormat.equals("")) { 124 try { 125 return (Renderer) Class.forName(reportFormat).newInstance(); 126 } catch (Exception e) { 127 throw new IllegalArgumentException ("Can't find the custom format " + reportFormat + ": " + e.getClass().getName()); 128 } 129 } 130 131 throw new IllegalArgumentException ("Can't create report with format of " + reportFormat); 132 } 133 134 public boolean containsCommaSeparatedFileList() { 135 return inputPath.indexOf(',') != -1; 136 } 137 138 public String getInputPath() { 139 return this.inputPath; 140 } 141 142 public String getEncoding() { 143 return this.encoding; 144 } 145 146 public String getReportFormat() { 147 return this.reportFormat; 148 } 149 150 public String getReportFile() { 151 return this.reportFile; 152 } 153 154 public String getRulesets() { 155 return this.ruleSets; 156 } 157 158 public String getExcludeMarker() { 159 return this.excludeMarker; 160 } 161 162 public boolean debugEnabled() { 163 return debugEnabled; 164 } 165 166 public int getCpus() { 167 return cpus; 168 } 169 170 public String getTargetJDK() { 171 return targetJDK; 172 } 173 174 public boolean shortNamesEnabled() { 175 return shortNamesEnabled; 176 } 177 178 public int getMinPriority() { 179 return minPriority; 180 } 181 182 public String usage() { 183 return PMD.EOL + PMD.EOL + 184 "Mandatory arguments:" + PMD.EOL + 185 "1) A java source code filename or directory" + PMD.EOL + 186 "2) A report format " + PMD.EOL + 187 "3) A ruleset filename or a comma-delimited string of ruleset filenames" + PMD.EOL + 188 PMD.EOL + 189 "For example: " + PMD.EOL + 190 "c:\\> java -jar pmd-" + PMD.VERSION + ".jar c:\\my\\source\\code html unusedcode" + PMD.EOL + 191 PMD.EOL + 192 "Optional arguments that may be put before or after the mandatory arguments: " + PMD.EOL + 193 "-debug: prints debugging information" + PMD.EOL + 194 "-targetjdk: specifies a language version to target - 1.3, 1.4, 1.5 or 1.6" + PMD.EOL + 195 "-cpus: specifies the number of threads to create" + PMD.EOL + 196 "-encoding: specifies the character set encoding of the source code files PMD is reading (i.e., UTF-8)" + PMD.EOL + 197 "-excludemarker: specifies the String that marks the a line which PMD should ignore; default is NOPMD" + PMD.EOL + 198 "-shortnames: prints shortened filenames in the report" + PMD.EOL + 199 "-linkprefix: path to HTML source, for summary html renderer only" + PMD.EOL + 200 "-lineprefix: custom anchor to affected line in the source file, for summary html renderer only" + PMD.EOL + 201 "-minimumpriority: rule priority threshold; rules with lower priority than they will not be used" + PMD.EOL + 202 "-reportfile: send report output to a file; default to System.out" + PMD.EOL + 203 PMD.EOL + 204 "For example: " + PMD.EOL + 205 "c:\\> java -jar pmd-" + PMD.VERSION + ".jar c:\\my\\source\\code text unusedcode,imports -targetjdk 1.5 -debug" + PMD.EOL + 206 "c:\\> java -jar pmd-" + PMD.VERSION + ".jar c:\\my\\source\\code xml basic,design -encoding UTF-8" + PMD.EOL + 207 PMD.EOL; 208 } 209 210 213 public boolean isCheckJavaFiles() { 214 return checkJavaFiles; 215 } 216 217 220 public boolean isCheckJspFiles() { 221 return checkJspFiles; 222 } 223 } 224 225 226 227 228 | Popular Tags |