Search in sources :

Example 1 with DocumentNode

use of net.sourceforge.pmd.lang.ast.xpath.saxon.DocumentNode in project pmd by pmd.

the class SaxonXPathRuleQuery method evaluate.

@Override
@SuppressWarnings("unchecked")
public List<Node> evaluate(final Node node, final RuleContext data) {
    initializeXPathExpression();
    try {
        final DocumentNode documentNode = getDocumentNodeForRootNode(node);
        // Map AST Node -> Saxon Node
        final ElementNode rootElementNode = documentNode.nodeToElementNode.get(node);
        final XPathDynamicContext xpathDynamicContext = createDynamicContext(rootElementNode);
        final List<ElementNode> nodes = xpathExpression.evaluate(xpathDynamicContext);
        /*
             Map List of Saxon Nodes -> List of AST Nodes, which were detected to match the XPath expression
             (i.e. violation found)
              */
        final List<Node> results = new ArrayList<>();
        for (final ElementNode elementNode : nodes) {
            results.add((Node) elementNode.getUnderlyingNode());
        }
        return results;
    } catch (final XPathException e) {
        throw new RuntimeException(super.xpath + " had problem: " + e.getMessage(), e);
    }
}
Also used : DocumentNode(net.sourceforge.pmd.lang.ast.xpath.saxon.DocumentNode) XPathException(net.sf.saxon.trans.XPathException) DocumentNode(net.sourceforge.pmd.lang.ast.xpath.saxon.DocumentNode) Node(net.sourceforge.pmd.lang.ast.Node) ElementNode(net.sourceforge.pmd.lang.ast.xpath.saxon.ElementNode) ArrayList(java.util.ArrayList) ElementNode(net.sourceforge.pmd.lang.ast.xpath.saxon.ElementNode) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Example 2 with DocumentNode

use of net.sourceforge.pmd.lang.ast.xpath.saxon.DocumentNode in project pmd by pmd.

the class SaxonXPathRuleQuery method getDocumentNodeForRootNode.

/**
 * Gets the DocumentNode representation for the whole AST in which the node is, that is, if the node is not the root
 * of the AST, then the AST is traversed all the way up until the root node is found. If the DocumentNode was
 * cached because this method was previously called, then a new DocumentNode will not be instanced.
 *
 * @param node the node from which the root node will be looked for.
 * @return the DocumentNode representing the whole AST
 */
private DocumentNode getDocumentNodeForRootNode(final Node node) {
    final Node root = getRootNode(node);
    DocumentNode documentNode;
    synchronized (CACHE) {
        documentNode = CACHE.get(root);
        if (documentNode == null) {
            documentNode = new DocumentNode(root);
            CACHE.put(root, documentNode);
        }
    }
    return documentNode;
}
Also used : DocumentNode(net.sourceforge.pmd.lang.ast.xpath.saxon.DocumentNode) DocumentNode(net.sourceforge.pmd.lang.ast.xpath.saxon.DocumentNode) Node(net.sourceforge.pmd.lang.ast.Node) ElementNode(net.sourceforge.pmd.lang.ast.xpath.saxon.ElementNode)

Aggregations

Node (net.sourceforge.pmd.lang.ast.Node)2 DocumentNode (net.sourceforge.pmd.lang.ast.xpath.saxon.DocumentNode)2 ElementNode (net.sourceforge.pmd.lang.ast.xpath.saxon.ElementNode)2 ArrayList (java.util.ArrayList)1 XPathDynamicContext (net.sf.saxon.sxpath.XPathDynamicContext)1 XPathException (net.sf.saxon.trans.XPathException)1