Search in sources :

Example 1 with XPathVariable

use of net.sf.saxon.sxpath.XPathVariable in project pmd by pmd.

the class SaxonXPathRuleQuery method initializeXPathExpression.

/**
 * Initialize the {@link #xpathExpression} and the {@link #xpathVariables}.
 */
private void initializeXPathExpression() {
    if (xpathExpression != null) {
        return;
    }
    try {
        final XPathEvaluator xpathEvaluator = new XPathEvaluator();
        final XPathStaticContext xpathStaticContext = xpathEvaluator.getStaticContext();
        // Enable XPath 1.0 compatibility
        if (XPATH_1_0_COMPATIBILITY.equals(version)) {
            ((AbstractStaticContext) xpathStaticContext).setBackwardsCompatibilityMode(true);
        }
        // Register PMD functions
        Initializer.initialize((IndependentContext) xpathStaticContext);
        /*
            Create XPathVariables for later use. It is a Saxon quirk that XPathVariables must be defined on the
            static context, and reused later to associate an actual value on the dynamic context creation, in
            createDynamicContext(ElementNode).
            */
        xpathVariables = new ArrayList<>();
        for (final PropertyDescriptor<?> propertyDescriptor : super.properties.keySet()) {
            final String name = propertyDescriptor.name();
            if (!"xpath".equals(name)) {
                final XPathVariable xpathVariable = xpathStaticContext.declareVariable(null, name);
                xpathVariables.add(xpathVariable);
            }
        }
        // TODO Come up with a way to make use of RuleChain. I had hacked up
        // an approach which used Jaxen's stuff, but that only works for
        // 1.0 compatibility mode. Rather do it right instead of kludging.
        xpathExpression = xpathEvaluator.createExpression(super.xpath);
    } catch (final XPathException e) {
        throw new RuntimeException(e);
    }
}
Also used : XPathStaticContext(net.sf.saxon.sxpath.XPathStaticContext) XPathVariable(net.sf.saxon.sxpath.XPathVariable) XPathException(net.sf.saxon.trans.XPathException) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) AbstractStaticContext(net.sf.saxon.sxpath.AbstractStaticContext)

Example 2 with XPathVariable

use of net.sf.saxon.sxpath.XPathVariable 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)

Aggregations

XPathVariable (net.sf.saxon.sxpath.XPathVariable)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ValueRepresentation (net.sf.saxon.om.ValueRepresentation)1 AbstractStaticContext (net.sf.saxon.sxpath.AbstractStaticContext)1 XPathDynamicContext (net.sf.saxon.sxpath.XPathDynamicContext)1 XPathEvaluator (net.sf.saxon.sxpath.XPathEvaluator)1 XPathStaticContext (net.sf.saxon.sxpath.XPathStaticContext)1 XPathException (net.sf.saxon.trans.XPathException)1 PropertyDescriptor (net.sourceforge.pmd.properties.PropertyDescriptor)1