use of net.sf.saxon.sxpath.XPathEvaluator in project teiid by teiid.
the class XMLSystemFunctions method xpathValue.
public static String xpathValue(Object doc, String xpath) throws XPathException, TeiidProcessingException {
Source s = null;
try {
s = convertToSource(doc);
XPathEvaluator eval = new XPathEvaluator();
// Wrap the string() function to force a string return
XPathExpression expr = eval.createExpression(xpath);
XPathDynamicContext context = expr.createDynamicContext(eval.getConfiguration().buildDocumentTree(s).getRootNode());
Object o = expr.evaluateSingle(context);
if (o == null) {
return null;
}
// Return string value of node type
if (o instanceof Item) {
Item i = (Item) o;
if (isNull(i)) {
return null;
}
return i.getStringValue();
}
// Return string representation of non-node value
return o.toString();
} finally {
Util.closeSource(s);
}
}
use of net.sf.saxon.sxpath.XPathEvaluator in project teiid by teiid.
the class SaxonXQueryExpression method processColumns.
private void processColumns(List<XMLTable.XMLColumn> columns, IndependentContext ic) throws QueryResolverException {
if (columns == null) {
return;
}
XPathEvaluator eval = new XPathEvaluator(config);
eval.setStaticContext(ic);
for (XMLColumn xmlColumn : columns) {
if (xmlColumn.isOrdinal()) {
continue;
}
String path = xmlColumn.getPath();
if (path == null) {
path = xmlColumn.getName();
}
path = path.trim();
if (path.startsWith("/")) {
// $NON-NLS-1$
if (path.startsWith("//")) {
// $NON-NLS-1$
path = '.' + path;
} else {
path = path.substring(1);
}
}
XPathExpression exp;
try {
exp = eval.createExpression(path);
} catch (XPathException e) {
throw new QueryResolverException(QueryPlugin.Event.TEIID30155, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30155, xmlColumn.getName(), xmlColumn.getPath()));
}
xmlColumn.setPathExpression(exp);
}
}
use of net.sf.saxon.sxpath.XPathEvaluator 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