use of net.sourceforge.pmd.RuleSet.RuleSetBuilder in project pmd by pmd.
the class RuleSetFactory method createSingleRuleRuleSet.
/**
* Creates a new RuleSet for a single rule
*
* @param rule
* The rule being created
* @return The newly created RuleSet
*/
public RuleSet createSingleRuleRuleSet(final Rule rule) {
final long checksum;
if (rule instanceof XPathRule) {
checksum = rule.getProperty(XPathRule.XPATH_DESCRIPTOR).hashCode();
} else {
// TODO : Is this good enough? all properties' values + rule name
checksum = rule.getPropertiesByPropertyDescriptor().values().hashCode() * 31 + rule.getName().hashCode();
}
final RuleSetBuilder builder = new RuleSetBuilder(checksum).withName(rule.getName()).withDescription("RuleSet for " + rule.getName());
builder.addRule(rule);
return builder.build();
}
use of net.sourceforge.pmd.RuleSet.RuleSetBuilder in project pmd by pmd.
the class RuleSetTest method testEquals3.
@Test
public void testEquals3() {
RuleSet s = new RuleSetBuilder(new Random().nextLong()).withName("basic rules").withDescription("desc").build();
assertFalse("A ruleset cannot be equals to another kind of object", s.equals("basic rules"));
}
use of net.sourceforge.pmd.RuleSet.RuleSetBuilder in project pmd by pmd.
the class RuleSetFactory method parseRuleSetNode.
/**
* Parse a ruleset node to construct a RuleSet.
*
* @param ruleSetReferenceId
* The RuleSetReferenceId of the RuleSet being parsed.
* @param withDeprecatedRuleReferences
* whether rule references that are deprecated should be ignored
* or not
* @return The new RuleSet.
*/
private RuleSet parseRuleSetNode(RuleSetReferenceId ruleSetReferenceId, boolean withDeprecatedRuleReferences) throws RuleSetNotFoundException {
try (CheckedInputStream inputStream = new CheckedInputStream(ruleSetReferenceId.getInputStream(resourceLoader), new Adler32())) {
if (!ruleSetReferenceId.isExternal()) {
throw new IllegalArgumentException("Cannot parse a RuleSet from a non-external reference: <" + ruleSetReferenceId + ">.");
}
DocumentBuilder builder = createDocumentBuilder();
InputSource inputSource;
if (compatibilityFilter != null) {
inputSource = new InputSource(compatibilityFilter.filterRuleSetFile(inputStream));
} else {
inputSource = new InputSource(inputStream);
}
Document document = builder.parse(inputSource);
Element ruleSetElement = document.getDocumentElement();
RuleSetBuilder ruleSetBuilder = new RuleSetBuilder(inputStream.getChecksum().getValue()).withFileName(ruleSetReferenceId.getRuleSetFileName());
if (ruleSetElement.hasAttribute("name")) {
ruleSetBuilder.withName(ruleSetElement.getAttribute("name"));
} else {
LOG.warning("RuleSet name is missing. Future versions of PMD will require it.");
ruleSetBuilder.withName("Missing RuleSet Name");
}
NodeList nodeList = ruleSetElement.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
String nodeName = node.getNodeName();
if (DESCRIPTION.equals(nodeName)) {
ruleSetBuilder.withDescription(parseTextNode(node));
} else if ("include-pattern".equals(nodeName)) {
ruleSetBuilder.addIncludePattern(parseTextNode(node));
} else if ("exclude-pattern".equals(nodeName)) {
ruleSetBuilder.addExcludePattern(parseTextNode(node));
} else if ("rule".equals(nodeName)) {
parseRuleNode(ruleSetReferenceId, ruleSetBuilder, node, withDeprecatedRuleReferences);
} else {
throw new IllegalArgumentException(UNEXPECTED_ELEMENT + node.getNodeName() + "> encountered as child of <ruleset> element.");
}
}
}
if (!ruleSetBuilder.hasDescription()) {
LOG.warning("RuleSet description is missing. Future versions of PMD will require it.");
ruleSetBuilder.withDescription("Missing description");
}
ruleSetBuilder.filterRulesByPriority(minimumPriority);
return ruleSetBuilder.build();
} catch (ClassNotFoundException cnfe) {
return classNotFoundProblem(cnfe);
} catch (InstantiationException ie) {
return classNotFoundProblem(ie);
} catch (IllegalAccessException iae) {
return classNotFoundProblem(iae);
} catch (ParserConfigurationException pce) {
return classNotFoundProblem(pce);
} catch (IOException ioe) {
return classNotFoundProblem(ioe);
} catch (SAXException se) {
return classNotFoundProblem(se);
}
}
use of net.sourceforge.pmd.RuleSet.RuleSetBuilder in project pmd by pmd.
the class RuleSetFactory method createNewRuleSet.
/**
* Creates a new ruleset with the given metadata such as name, description,
* fileName, exclude/include patterns are used. The rules are taken from the given
* collection.
*
* <p><strong>Note:</strong> The rule instances are shared between the collection
* and the new ruleset (copy-by-reference). This might lead to concurrency issues,
* if the rules of the collection are also referenced by other rulesets and used
* in different threads.
* </p>
*
* @param name the name of the ruleset
* @param description the description
* @param fileName the filename
* @param excludePatterns list of exclude patterns
* @param includePatterns list of include patterns
* @param rules the collection with the rules to add to the new ruleset
* @return the new ruleset
*/
public RuleSet createNewRuleSet(String name, String description, String fileName, Collection<String> excludePatterns, Collection<String> includePatterns, Collection<Rule> rules) {
// TODO: checksum missing
RuleSetBuilder builder = new RuleSetBuilder(0L);
builder.withName(name).withDescription(description).withFileName(fileName).setExcludePatterns(excludePatterns).setIncludePatterns(includePatterns);
for (Rule rule : rules) {
builder.addRule(rule);
}
return builder.build();
}
Aggregations