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
This commit is contained in:
Tom Copeland
2002-09-06 17:29:53 +00:00
parent 46ee7ed77d
commit a42e18a802
3 changed files with 22 additions and 24 deletions

View File

@ -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;

View File

@ -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) {

View File

@ -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;
}
}