Search in sources :

Example 1 with ElementNode

use of net.sourceforge.pmd.lang.ast.xpath.saxon.ElementNode 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)

Aggregations

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