public static void main(String[] args) { CommandLineOptions opts = new CommandLineOptions(args); List files; if (opts.containsCommaSeparatedFileList()) { files = collectFromCommaDelimitedString(opts.getInputFileName()); } else { files = collectFilesFromOneName(opts.getInputFileName()); } PMD pmd; if (opts.jdk13()) { pmd = new PMD(new TargetJDK1_3()); } else { pmd = new PMD(); } RuleContext ctx = new RuleContext(); ctx.setReport(new Report()); try { RuleSetFactory ruleSetFactory = new RuleSetFactory(); RuleSet rules = ruleSetFactory.createRuleSet(opts.getRulesets()); for (Iterator i = files.iterator(); i.hasNext(); ) { File file = (File) i.next(); ctx.setSourceCodeFilename( glomName(opts.shortNamesEnabled(), opts.getInputFileName(), file)); try { pmd.processFile(new FileInputStream(file), opts.getEncoding(), rules, ctx); } catch (PMDException pmde) { if (opts.debugEnabled()) { pmde.getReason().printStackTrace(); } ctx.getReport() .addError( new Report.ProcessingError( pmde.getMessage(), glomName(opts.shortNamesEnabled(), opts.getInputFileName(), file))); } } } catch (FileNotFoundException fnfe) { System.out.println(opts.usage()); fnfe.printStackTrace(); } catch (RuleSetNotFoundException rsnfe) { System.out.println(opts.usage()); rsnfe.printStackTrace(); } try { Renderer r = opts.createRenderer(); System.out.println(r.render(ctx.getReport())); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println(opts.usage()); if (opts.debugEnabled()) { e.printStackTrace(); } } }
public void testPluginname() throws Throwable { Rule rule = new XPathRule(); rule.addProperty("xpath", "//VariableDeclaratorId[string-length(@Image) < 3]"); rule.setMessage("{0}"); rule.addProperty("pluginname", "true"); PMD p = new PMD(); RuleContext ctx = new RuleContext(); Report report = new Report(); ctx.setReport(report); ctx.setSourceCodeFilename("n/a"); RuleSet rules = new RuleSet(); rules.addRule(rule); p.processFile(new StringReader(TEST1), rules, ctx); RuleViolation rv = (RuleViolation) report.iterator().next(); assertEquals("a", rv.getDescription()); }