Search in sources :

Example 1 with RuleSetBuilder

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();
}
Also used : RuleSetBuilder(net.sourceforge.pmd.RuleSet.RuleSetBuilder) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule)

Example 2 with RuleSetBuilder

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"));
}
Also used : Random(java.util.Random) RuleSetBuilder(net.sourceforge.pmd.RuleSet.RuleSetBuilder) Test(org.junit.Test)

Example 3 with RuleSetBuilder

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) CheckedInputStream(java.util.zip.CheckedInputStream) Adler32(java.util.zip.Adler32) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) RuleSetBuilder(net.sourceforge.pmd.RuleSet.RuleSetBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 4 with RuleSetBuilder

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();
}
Also used : RuleSetBuilder(net.sourceforge.pmd.RuleSet.RuleSetBuilder) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule)

Aggregations

RuleSetBuilder (net.sourceforge.pmd.RuleSet.RuleSetBuilder)4 XPathRule (net.sourceforge.pmd.lang.rule.XPathRule)2 IOException (java.io.IOException)1 Random (java.util.Random)1 Adler32 (java.util.zip.Adler32)1 CheckedInputStream (java.util.zip.CheckedInputStream)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 MockRule (net.sourceforge.pmd.lang.rule.MockRule)1 Test (org.junit.Test)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1