Search in sources :

Example 6 with XPathDynamicContext

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;
}
Also used : XPathVariable(net.sf.saxon.sxpath.XPathVariable) ValueRepresentation(net.sf.saxon.om.ValueRepresentation) PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with XPathDynamicContext

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());
}
Also used : XPathExpression(net.sf.saxon.sxpath.XPathExpression) Item(net.sf.saxon.om.Item) List(java.util.List) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext) AbstractNode(com.puppycrawl.tools.checkstyle.xpath.AbstractNode) Configuration(net.sf.saxon.Configuration) XPathException(net.sf.saxon.trans.XPathException) Collectors(java.util.stream.Collectors) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) NodeInfo(net.sf.saxon.om.NodeInfo) XPathExpression(net.sf.saxon.sxpath.XPathExpression) Item(net.sf.saxon.om.Item) NodeInfo(net.sf.saxon.om.NodeInfo) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Example 8 with XPathDynamicContext

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);
    }
}
Also used : XPathExpression(net.sf.saxon.sxpath.XPathExpression) RootNode(com.puppycrawl.tools.checkstyle.xpath.RootNode) Item(net.sf.saxon.om.Item) XPathException(net.sf.saxon.trans.XPathException) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Aggregations

XPathDynamicContext (net.sf.saxon.sxpath.XPathDynamicContext)8 Item (net.sf.saxon.om.Item)6 XPathException (net.sf.saxon.trans.XPathException)6 XPathExpression (net.sf.saxon.sxpath.XPathExpression)5 XPathEvaluator (net.sf.saxon.sxpath.XPathEvaluator)4 RootNode (com.puppycrawl.tools.checkstyle.xpath.RootNode)3 AbstractNode (com.puppycrawl.tools.checkstyle.xpath.AbstractNode)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Configuration (net.sf.saxon.Configuration)2 StatelessCheck (com.puppycrawl.tools.checkstyle.StatelessCheck)1 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)1 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)1 CommonUtil (com.puppycrawl.tools.checkstyle.utils.CommonUtil)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Source (javax.xml.transform.Source)1 StAXSource (javax.xml.transform.stax.StAXSource)1