use of net.sf.saxon.sxpath.XPathExpression 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.XPathExpression 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;
}
use of net.sf.saxon.sxpath.XPathExpression 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.XPathExpression in project checkstyle by checkstyle.
the class XpathUtil method getXpathItems.
/**
* Returns list of nodes matching xpath expression given node context.
*
* @param xpath Xpath expression
* @param rootNode {@code NodeInfo} node context
* @return list of nodes matching xpath expression given node context
*/
public static List<NodeInfo> getXpathItems(String xpath, AbstractNode rootNode) throws XPathException {
final XPathEvaluator xpathEvaluator = new XPathEvaluator(Configuration.newConfiguration());
final XPathExpression xpathExpression = xpathEvaluator.createExpression(xpath);
final XPathDynamicContext xpathDynamicContext = xpathExpression.createDynamicContext(rootNode);
final List<Item> items = xpathExpression.evaluate(xpathDynamicContext);
return items.stream().map(item -> (NodeInfo) item).collect(Collectors.toList());
}
use of net.sf.saxon.sxpath.XPathExpression in project checkstyle by checkstyle.
the class XpathUtil method printXpathBranch.
/**
* Returns xpath query results on file as string.
*
* @param xpath query to evaluate
* @param file file to run on
* @return all results as string separated by delimiter
* @throws CheckstyleException if some parsing error happens
* @throws IOException if an error occurs
*/
public static String printXpathBranch(String xpath, File file) throws CheckstyleException, IOException {
final XPathEvaluator xpathEvaluator = new XPathEvaluator(Configuration.newConfiguration());
try {
final RootNode rootNode = new RootNode(JavaParser.parseFile(file, JavaParser.Options.WITH_COMMENTS));
final XPathExpression xpathExpression = xpathEvaluator.createExpression(xpath);
final XPathDynamicContext xpathDynamicContext = xpathExpression.createDynamicContext(rootNode);
final List<Item> matchingItems = xpathExpression.evaluate(xpathDynamicContext);
return matchingItems.stream().map(item -> ((AbstractNode) item).getUnderlyingNode()).map(AstTreeStringPrinter::printBranch).collect(Collectors.joining(DELIMITER));
} catch (XPathException ex) {
final String errMsg = String.format(Locale.ROOT, "Error during evaluation for xpath: %s, file: %s", xpath, file.getCanonicalPath());
throw new CheckstyleException(errMsg, ex);
}
}
Aggregations