Minor refactoring - ExtractMethod

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1853 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2003-04-23 19:13:44 +00:00
parent 50667a690c
commit 9c196ef783

View File

@ -75,52 +75,11 @@ public class RuleSetFactory {
NodeList rules = root.getElementsByTagName("rule");
for (int i = 0; i < rules.getLength(); i++) {
Node ruleNode = rules.item(i);
Rule rule;
if (ruleNode.getAttributes().getNamedItem("ref") != null) {
ExternalRuleID externalRuleID = new ExternalRuleID(ruleNode.getAttributes().getNamedItem("ref").getNodeValue());
RuleSetFactory rsf = new RuleSetFactory();
RuleSet externalRuleSet = rsf.createRuleSet(ResourceLoader.loadResourceAsStream(externalRuleID.getFilename()));
rule = externalRuleSet.getRuleByName(externalRuleID.getRuleName());
parseExternallyDefinedRule(ruleSet, ruleNode);
} else {
rule = (Rule) Class.forName(ruleNode.getAttributes().getNamedItem("class").getNodeValue(), true, classLoader).newInstance();
rule.setName(ruleNode.getAttributes().getNamedItem("name").getNodeValue());
rule.setMessage(ruleNode.getAttributes().getNamedItem("message").getNodeValue());
parseInternallyDefinedRule(ruleSet, ruleNode, classLoader);
}
// get the description, priority, example and properties (if any)
Node node = ruleNode.getFirstChild();
while (node != null) {
if (node.getNodeName() != null && node.getNodeName().equals("description")) {
rule.setDescription(node.getFirstChild().getNodeValue());
}
if (node.getNodeName() != null && node.getNodeName().equals("priority")) {
rule.setPriority(Integer.parseInt(node.getFirstChild().getNodeValue().trim()));
}
if (node.getNodeName() != null && node.getNodeName().equals("example")) {
rule.setExample(node.getFirstChild().getNextSibling().getNodeValue());
}
if (node.getNodeName().equals("properties")) {
Node propNode = node.getFirstChild().getNextSibling();
while (propNode != null && propNode.getAttributes() != null) {
String propName = propNode.getAttributes().getNamedItem("name").getNodeValue();
String propValue;
if (propName.equals("xpath")) {
Node xpathExprNode = propNode.getFirstChild().getNextSibling();
propValue = xpathExprNode.getFirstChild().getNextSibling().getNodeValue();
} else {
propValue = propNode.getAttributes().getNamedItem("value").getNodeValue();
}
rule.addProperty(propName, propValue);
propNode = propNode.getNextSibling().getNextSibling();
}
}
node = node.getNextSibling();
}
ruleSet.addRule(rule);
}
return ruleSet;
} catch (Exception e) {
@ -129,6 +88,61 @@ public class RuleSetFactory {
}
}
private void parseInternallyDefinedRule(RuleSet ruleSet, Node ruleNode, ClassLoader classLoader) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Rule rule = (Rule) Class.forName(ruleNode.getAttributes().getNamedItem("class").getNodeValue(), true, classLoader).newInstance();
rule.setName(ruleNode.getAttributes().getNamedItem("name").getNodeValue());
rule.setMessage(ruleNode.getAttributes().getNamedItem("message").getNodeValue());
// get the description, priority, example and properties (if any)
Node node = ruleNode.getFirstChild();
while (node != null) {
if (node.getNodeName() != null && node.getNodeName().equals("description")) {
rule.setDescription(node.getFirstChild().getNodeValue());
}
if (node.getNodeName() != null && node.getNodeName().equals("priority")) {
rule.setPriority(Integer.parseInt(node.getFirstChild().getNodeValue().trim()));
}
if (node.getNodeName() != null && node.getNodeName().equals("example")) {
rule.setExample(node.getFirstChild().getNextSibling().getNodeValue());
}
parseProperties(node, rule);
node = node.getNextSibling();
}
ruleSet.addRule(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()));
}
throw new RuntimeException("Not impl yet!");
}
private void parseProperties(Node node, Rule rule) {
if (node.getNodeName().equals("properties")) {
Node propNode = node.getFirstChild().getNextSibling();
while (propNode != null && propNode.getAttributes() != null) {
String propName = propNode.getAttributes().getNamedItem("name").getNodeValue();
String propValue;
if (propName.equals("xpath")) {
Node xpathExprNode = propNode.getFirstChild().getNextSibling();
propValue = xpathExprNode.getFirstChild().getNextSibling().getNodeValue();
} else {
propValue = propNode.getAttributes().getNamedItem("value").getNodeValue();
}
rule.addProperty(propName, propValue);
propNode = propNode.getNextSibling().getNextSibling();
}
}
}
private InputStream tryToGetStreamTo(String name, ClassLoader loader) throws RuleSetNotFoundException {
InputStream in = ResourceLoader.loadResourceAsStream(name, loader);
if (in == null) {