use of net.sf.saxon.sxpath.AbstractStaticContext 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);
}
}
Aggregations