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);
}
}
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;
}
Aggregations