From a42e18a802e2aa45005c45be4a208f4d96f7bc16 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Fri, 6 Sep 2002 17:29:53 +0000 Subject: [PATCH] some minor refactoring; also added ability for RuleSetFactory.createRuleSet() to accept a comma-delimited set of ruleset names git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@856 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/src/net/sourceforge/pmd/PMD.java | 13 ++++++++---- .../net/sourceforge/pmd/RuleSetFactory.java | 12 ++++++++++- pmd/src/net/sourceforge/pmd/ant/PMDTask.java | 21 ++----------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/pmd/src/net/sourceforge/pmd/PMD.java b/pmd/src/net/sourceforge/pmd/PMD.java index 1575d494cb..8096b68eac 100644 --- a/pmd/src/net/sourceforge/pmd/PMD.java +++ b/pmd/src/net/sourceforge/pmd/PMD.java @@ -66,12 +66,15 @@ public class PMD { } if (args.length != 3) { - throw new RuntimeException("Please pass in a java source code filename, a report format, and a rule set file name"); + System.err.println(""); + System.err.println("Please pass in a java source code filename or directory, a report format, and a ruleset filename or a comma-delimited string of ruleset filenames." + System.getProperty("line.separator") + "For example: " + System.getProperty("line.separator") + "c:\\> java -jar pmd-0.9.jar c:\\my\\source\\code html rulesets/unusedcode.xml,rulesets/imports.xml"); + System.err.println(""); + System.exit(1); } String inputFileName = args[0]; String reportFormat = args[1]; - String ruleSetFilename = args[2]; + String ruleSets = args[2]; File inputFile = new File(inputFileName); if (!inputFile.exists()) { @@ -90,11 +93,11 @@ public class PMD { PMD pmd = new PMD(); RuleContext ctx = new RuleContext(); - RuleSetFactory ruleSetFactory = new RuleSetFactory(); - RuleSet rules = ruleSetFactory.createRuleSet(pmd.getClass().getClassLoader().getResourceAsStream(ruleSetFilename)); ctx.setReport(new Report()); try { + RuleSetFactory ruleSetFactory = new RuleSetFactory(); + RuleSet rules = ruleSetFactory.createRuleSet(ruleSets); for (Iterator i = files.iterator(); i.hasNext();) { File file = (File)i.next(); ctx.setSourceCodeFilename(file.getAbsolutePath()); @@ -102,6 +105,8 @@ public class PMD { } } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); + } catch (RuleSetNotFoundException rsnfe) { + rsnfe.printStackTrace(); } Renderer rend = null; diff --git a/pmd/src/net/sourceforge/pmd/RuleSetFactory.java b/pmd/src/net/sourceforge/pmd/RuleSetFactory.java index edd2211ece..1677d9c5e6 100644 --- a/pmd/src/net/sourceforge/pmd/RuleSetFactory.java +++ b/pmd/src/net/sourceforge/pmd/RuleSetFactory.java @@ -41,7 +41,17 @@ public class RuleSetFactory { } public RuleSet createRuleSet(String name) throws RuleSetNotFoundException { - return createRuleSet(tryToGetStreamTo(name)); + if (name.indexOf(',') == -1) { + return createRuleSet(tryToGetStreamTo(name)); + } + + RuleSet ruleSet = new RuleSet(); + for (StringTokenizer st = new StringTokenizer(name, ","); st.hasMoreTokens();) { + String ruleSetName = st.nextToken(); + RuleSet tmpRuleSet = createRuleSet(ruleSetName); + ruleSet.addRuleSet(tmpRuleSet); + } + return ruleSet; } public RuleSet createRuleSet(InputStream inputStream) { diff --git a/pmd/src/net/sourceforge/pmd/ant/PMDTask.java b/pmd/src/net/sourceforge/pmd/ant/PMDTask.java index dce85157e5..610617797a 100644 --- a/pmd/src/net/sourceforge/pmd/ant/PMDTask.java +++ b/pmd/src/net/sourceforge/pmd/ant/PMDTask.java @@ -69,7 +69,8 @@ public class PMDTask extends Task { RuleSet rules = null; try { - rules = createRuleSets(); + RuleSetFactory ruleSetFactory = new RuleSetFactory(); + rules = ruleSetFactory.createRuleSet(ruleSetFiles); } catch (RuleSetNotFoundException rsnfe) { throw new BuildException(rsnfe.getMessage()); } @@ -118,22 +119,4 @@ public class PMDTask extends Task { throw new BuildException("Stopping build since PMD found problems in the code"); } } - - private RuleSet createRuleSets() throws RuleSetNotFoundException { - RuleSetFactory ruleSetFactory = new RuleSetFactory(); - - if (ruleSetFiles.indexOf(',') == -1) { - return ruleSetFactory.createRuleSet(ruleSetFiles); - } - - RuleSet ruleSet = new RuleSet(); - for (StringTokenizer st = new StringTokenizer(ruleSetFiles, ","); st.hasMoreTokens();) { - String ruleSetName = st.nextToken(); - RuleSet tmpRuleSet = ruleSetFactory.createRuleSet(ruleSetName); - if (verbose) System.out.println("Adding " + tmpRuleSet.size() + " rules from ruleset " + ruleSetName); - ruleSet.addRuleSet(tmpRuleSet); - } - return ruleSet; - } - }