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);
}
}
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;
}
Aggregations