use of net.sf.saxon.sxpath.XPathDynamicContext 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.XPathDynamicContext in project pmd by pmd.
the class SaxonXPathRuleQuery method evaluate.
@Override
@SuppressWarnings("unchecked")
public List<Node> evaluate(final Node node, final RuleContext data) {
initializeXPathExpression();
try {
final DocumentNode documentNode = getDocumentNodeForRootNode(node);
// Map AST Node -> Saxon Node
final ElementNode rootElementNode = documentNode.nodeToElementNode.get(node);
final XPathDynamicContext xpathDynamicContext = createDynamicContext(rootElementNode);
final List<ElementNode> nodes = xpathExpression.evaluate(xpathDynamicContext);
/*
Map List of Saxon Nodes -> List of AST Nodes, which were detected to match the XPath expression
(i.e. violation found)
*/
final List<Node> results = new ArrayList<>();
for (final ElementNode elementNode : nodes) {
results.add((Node) elementNode.getUnderlyingNode());
}
return results;
} catch (final XPathException e) {
throw new RuntimeException(super.xpath + " had problem: " + e.getMessage(), e);
}
}
use of net.sf.saxon.sxpath.XPathDynamicContext in project checkstyle by checkstyle.
the class MatchXpathCheck method findMatchingNodesByXpathQuery.
/**
* Find nodes that match query.
*
* @param rootAST root node
* @return list of matching nodes
* @throws IllegalStateException if evaluation of xpath query fails
*/
private List<DetailAST> findMatchingNodesByXpathQuery(DetailAST rootAST) {
try {
final RootNode rootNode = new RootNode(rootAST);
final XPathDynamicContext xpathDynamicContext = xpathExpression.createDynamicContext(rootNode);
final List<Item> matchingItems = xpathExpression.evaluate(xpathDynamicContext);
return matchingItems.stream().map(item -> ((AbstractNode) item).getUnderlyingNode()).collect(Collectors.toList());
} catch (XPathException ex) {
throw new IllegalStateException("Evaluation of Xpath query failed: " + query, ex);
}
}
use of net.sf.saxon.sxpath.XPathDynamicContext in project checkstyle by checkstyle.
the class XpathFilterElement method getItems.
/**
* Returns list of nodes matching xpath expression given event.
*
* @param event {@code TreeWalkerAuditEvent} object
* @return list of nodes matching xpath expression given event
* @throws IllegalStateException if the xpath query could not be evaluated.
*/
private List<Item> getItems(TreeWalkerAuditEvent event) {
final RootNode rootNode;
if (event.getRootAst() == null) {
rootNode = null;
} else {
rootNode = new RootNode(event.getRootAst());
}
final List<Item> items;
try {
final XPathDynamicContext xpathDynamicContext = xpathExpression.createDynamicContext(rootNode);
items = xpathExpression.evaluate(xpathDynamicContext);
} catch (XPathException ex) {
throw new IllegalStateException("Cannot initialize context and evaluate query: " + xpathQuery, ex);
}
return items;
}
use of net.sf.saxon.sxpath.XPathDynamicContext in project teiid by teiid.
the class XMLTableNode method processRow.
private List<?> processRow() throws ExpressionEvaluationException, BlockedException, TeiidComponentException, TeiidProcessingException {
List<Object> tuple = new ArrayList<Object>(projectedColumns.size());
for (XMLColumn proColumn : projectedColumns) {
if (proColumn.isOrdinal()) {
if (rowCount > Integer.MAX_VALUE) {
throw new TeiidRuntimeException(new TeiidProcessingException(QueryPlugin.Event.TEIID31174, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31174)));
}
tuple.add((int) rowCount);
} else {
try {
XPathExpression path = proColumn.getPathExpression();
XPathDynamicContext dynamicContext = path.createDynamicContext(item);
final SequenceIterator pathIter = path.iterate(dynamicContext);
Item colItem = pathIter.next();
if (colItem == null) {
if (proColumn.getDefaultExpression() != null) {
tuple.add(getEvaluator(Collections.emptyMap()).evaluate(proColumn.getDefaultExpression(), null));
} else {
tuple.add(null);
}
continue;
}
if (proColumn.getSymbol().getType() == DataTypeManager.DefaultDataClasses.XML) {
SequenceIterator pushBack = new PushBackSequenceIterator(pathIter, colItem);
XMLType value = table.getXQueryExpression().createXMLType(pushBack, this.getBufferManager(), false, getContext());
tuple.add(value);
continue;
}
if (proColumn.getSymbol().getType().isArray()) {
ArrayList<Object> vals = new ArrayList<Object>();
vals.add(getValue(proColumn.getSymbol().getType().getComponentType(), colItem, this.table.getXQueryExpression().getConfig(), getContext()));
Item next = null;
while ((next = pathIter.next()) != null) {
vals.add(getValue(proColumn.getSymbol().getType().getComponentType(), next, this.table.getXQueryExpression().getConfig(), getContext()));
}
Object value = new ArrayImpl(vals.toArray((Object[]) Array.newInstance(proColumn.getSymbol().getType().getComponentType(), vals.size())));
tuple.add(value);
continue;
} else if (pathIter.next() != null) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30171, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30171, proColumn.getName()));
}
Object value = getValue(proColumn.getSymbol().getType(), colItem, this.table.getXQueryExpression().getConfig(), getContext());
tuple.add(value);
} catch (XPathException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30172, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30172, proColumn.getName()));
}
}
}
item = null;
return tuple;
}
Aggregations