Search in sources :

Example 1 with XPathStaticContext

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

Aggregations

AbstractStaticContext (net.sf.saxon.sxpath.AbstractStaticContext)1 XPathEvaluator (net.sf.saxon.sxpath.XPathEvaluator)1 XPathStaticContext (net.sf.saxon.sxpath.XPathStaticContext)1 XPathVariable (net.sf.saxon.sxpath.XPathVariable)1 XPathException (net.sf.saxon.trans.XPathException)1