use of net.sf.saxon.sxpath.XPathDynamicContext in project pmd by pmd.
the class SaxonXPathRuleQuery method createDynamicContext.
/**
* Attempt to create a dynamic context on which to evaluate the {@link #xpathExpression}.
*
* @param elementNode the node on which to create the context; generally this node is the root node of the Saxon
* Tree
* @return the dynamic context on which to run the query
* @throws XPathException if the supplied value does not conform to the required type of the
* variable, when setting up the dynamic context; or if the supplied value contains a node that does not belong to
* this Configuration (or another Configuration that shares the same namePool)
*/
private XPathDynamicContext createDynamicContext(final ElementNode elementNode) throws XPathException {
final XPathDynamicContext dynamicContext = xpathExpression.createDynamicContext(elementNode);
// Set variable values on the dynamic context
for (final XPathVariable xpathVariable : xpathVariables) {
final String variableName = xpathVariable.getVariableQName().getLocalName();
for (final Map.Entry<PropertyDescriptor<?>, Object> entry : super.properties.entrySet()) {
if (variableName.equals(entry.getKey().name())) {
final ValueRepresentation valueRepresentation = getRepresentation(entry.getKey(), entry.getValue());
dynamicContext.setVariable(xpathVariable, valueRepresentation);
}
}
}
return dynamicContext;
}
use of net.sf.saxon.sxpath.XPathDynamicContext in project checkstyle by checkstyle.
the class XpathUtil method getXpathItems.
/**
* Returns list of nodes matching xpath expression given node context.
*
* @param xpath Xpath expression
* @param rootNode {@code NodeInfo} node context
* @return list of nodes matching xpath expression given node context
*/
public static List<NodeInfo> getXpathItems(String xpath, AbstractNode rootNode) throws XPathException {
final XPathEvaluator xpathEvaluator = new XPathEvaluator(Configuration.newConfiguration());
final XPathExpression xpathExpression = xpathEvaluator.createExpression(xpath);
final XPathDynamicContext xpathDynamicContext = xpathExpression.createDynamicContext(rootNode);
final List<Item> items = xpathExpression.evaluate(xpathDynamicContext);
return items.stream().map(item -> (NodeInfo) item).collect(Collectors.toList());
}
use of net.sf.saxon.sxpath.XPathDynamicContext in project checkstyle by checkstyle.
the class XpathUtil method printXpathBranch.
/**
* Returns xpath query results on file as string.
*
* @param xpath query to evaluate
* @param file file to run on
* @return all results as string separated by delimiter
* @throws CheckstyleException if some parsing error happens
* @throws IOException if an error occurs
*/
public static String printXpathBranch(String xpath, File file) throws CheckstyleException, IOException {
final XPathEvaluator xpathEvaluator = new XPathEvaluator(Configuration.newConfiguration());
try {
final RootNode rootNode = new RootNode(JavaParser.parseFile(file, JavaParser.Options.WITH_COMMENTS));
final XPathExpression xpathExpression = xpathEvaluator.createExpression(xpath);
final XPathDynamicContext xpathDynamicContext = xpathExpression.createDynamicContext(rootNode);
final List<Item> matchingItems = xpathExpression.evaluate(xpathDynamicContext);
return matchingItems.stream().map(item -> ((AbstractNode) item).getUnderlyingNode()).map(AstTreeStringPrinter::printBranch).collect(Collectors.joining(DELIMITER));
} catch (XPathException ex) {
final String errMsg = String.format(Locale.ROOT, "Error during evaluation for xpath: %s, file: %s", xpath, file.getCanonicalPath());
throw new CheckstyleException(errMsg, ex);
}
}
Aggregations