Implemented Gael Marziou's exclude rule feature

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1854 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2003-04-23 19:45:45 +00:00
parent 9c196ef783
commit 0d313fa5dc
6 changed files with 46 additions and 12 deletions

View File

@ -1,5 +1,6 @@
????? - 1.06:
Removed "verbose" attribute from PMD and CPD Ant tasks; now they use built in logging so you can do a "ant -verbose cpd" or "ant -verbose pmd". Thanks to Philippe T'Seyen for the code.
Added "excludes" feature to rulesets; thanks to Gael Marziou for the suggestion.
TODO - fix it so tests and rules don't duplicate the xpath expressions
April 17 - 1.05:

View File

@ -1,4 +1,6 @@
--------> Update ant docs - remove verbose attribute and make a note about -verbose
--------> Update custom ruleset docs - add exclude example
update run.bat to point to pmd-1.05.jar
update run.sh to point to pmd-1.05.jar
update cpdgui.bat to point to pmd-1.05.jar

View File

@ -4,11 +4,8 @@ import junit.framework.TestCase;
import net.sourceforge.pmd.ExternalRuleID;
public class ExternalRuleIDTest extends TestCase {
public ExternalRuleIDTest(String name) {
super(name);
}
public void testParse() {
public void testSimpleRef() {
String xrefString = "rulesets/basic.xml/EmptyCatchBlock";
ExternalRuleID xref = new ExternalRuleID(xrefString);
assertEquals("Filename mismatch!", "rulesets/basic.xml", xref.getFilename());

View File

@ -102,7 +102,6 @@ public class RuleSetFactoryTest extends TestCase {
"<priority>3</priority>" + EOL +
"</rule></ruleset>";
public void testSingleRuleWithPriority() {
RuleSetFactory rsf = new RuleSetFactory();
RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(SINGLE_RULE_SET_WITH_PRIORITY.getBytes()));

View File

@ -15,6 +15,8 @@ import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Set;
import java.util.HashSet;
public class RuleSetFactory {
@ -114,15 +116,48 @@ public class RuleSetFactory {
ruleSet.addRule(rule);
}
/**
* Here's how to exclude rules:
*
* <rule ref="rulesets/braces.xml">
* <exclude name="WhileLoopsMustUseBracesRule"/>
* <exclude name="IfElseStmtsMustUseBracesRule"/>
* </rule>
*
*/
private void parseExternallyDefinedRule(RuleSet ruleSet, Node ruleNode) throws RuleSetNotFoundException {
String referenceValue = ruleNode.getAttributes().getNamedItem("ref").getNodeValue();
if (!referenceValue.endsWith("xml")) {
ExternalRuleID externalRuleID = new ExternalRuleID(referenceValue);
RuleSetFactory rsf = new RuleSetFactory();
RuleSet externalRuleSet = rsf.createRuleSet(ResourceLoader.loadResourceAsStream(externalRuleID.getFilename()));
ruleSet.addRule(externalRuleSet.getRuleByName(externalRuleID.getRuleName()));
if (referenceValue.endsWith("xml")) {
parseWithExcludes(ruleNode, referenceValue, ruleSet);
} else {
parseSimpleReference(referenceValue, ruleSet);
}
}
private void parseSimpleReference(String referenceValue, RuleSet ruleSet) throws RuleSetNotFoundException {
RuleSetFactory rsf = new RuleSetFactory();
ExternalRuleID externalRuleID = new ExternalRuleID(referenceValue);
RuleSet externalRuleSet = rsf.createRuleSet(ResourceLoader.loadResourceAsStream(externalRuleID.getFilename()));
ruleSet.addRule(externalRuleSet.getRuleByName(externalRuleID.getRuleName()));
}
private void parseWithExcludes(Node ruleNode, String referenceValue, RuleSet ruleSet) throws RuleSetNotFoundException {
NodeList excludeNodes = ruleNode.getChildNodes();
Set excludes = new HashSet();
for (int i=0; i<excludeNodes.getLength(); i++) {
Node node = excludeNodes.item(i);
if (node.getAttributes() != null) {
excludes.add(node.getAttributes().getNamedItem("name").getNodeValue());
}
}
RuleSetFactory rsf = new RuleSetFactory();
RuleSet externalRuleSet = rsf.createRuleSet(ResourceLoader.loadResourceAsStream(referenceValue));
for (Iterator i = externalRuleSet.getRules().iterator(); i.hasNext();) {
Rule rule = (Rule)i.next();
if (!excludes.contains(rule.getName())) {
ruleSet.addRule(rule);
}
}
throw new RuntimeException("Not impl yet!");
}
private void parseProperties(Node node, Rule rule) {

View File

@ -10,6 +10,7 @@
<section name="Credits">
<subsection name="Individuals">
<ul>
<li>Gael Marziou - "exclude" rule feature request, bug reports</li>
<li>Philippe T'Seyen - refactoring and cleanup of the CPD Ant task, an XML renderer (with unit tests!) for CPD</li>
<li>Michael Montuori - bug reports on the Gel IDE plugin</li>
<li>Michael Hosier - bug reports on the Gel IDE plugin</li>
@ -52,7 +53,6 @@
<li>Radim Kubacki - bug reports, pmd-netbeans feedback</li>
<li>Stefan Bodewig - bug report</li>
<li>Paul Kendall - enhanced EmptyCatchBlock rule</li>
<li>Gael Marziou - bug reports</li>
<li>Sean Sullivan - rule suggestions</li>
<li>Dale Vissar - rule suggestions</li>
<li>Alina Copeland - pmd-web formulas, pmd-dcpd optimizations</li>