Search in sources :

Example 1 with XPathExpression

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);
    }
}
Also used : XPathExpression(net.sf.saxon.sxpath.XPathExpression) Item(net.sf.saxon.om.Item) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource)

Example 2 with XPathExpression

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;
}
Also used : XPathExpression(net.sf.saxon.sxpath.XPathExpression) XPathException(net.sf.saxon.trans.XPathException) ArrayImpl(org.teiid.core.types.ArrayImpl) ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) XMLColumn(org.teiid.query.sql.lang.XMLTable.XMLColumn) Item(net.sf.saxon.om.Item) XMLType(org.teiid.core.types.XMLType) PushBackSequenceIterator(org.teiid.query.xquery.saxon.PushBackSequenceIterator) SequenceIterator(net.sf.saxon.om.SequenceIterator) LanguageObject(org.teiid.query.sql.LanguageObject) PushBackSequenceIterator(org.teiid.query.xquery.saxon.PushBackSequenceIterator) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Example 3 with XPathExpression

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);
    }
}
Also used : XMLColumn(org.teiid.query.sql.lang.XMLTable.XMLColumn) XPathExpression(net.sf.saxon.sxpath.XPathExpression) XPathException(net.sf.saxon.trans.XPathException) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 4 with XPathExpression

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());
}
Also used : XPathExpression(net.sf.saxon.sxpath.XPathExpression) Item(net.sf.saxon.om.Item) List(java.util.List) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext) AbstractNode(com.puppycrawl.tools.checkstyle.xpath.AbstractNode) Configuration(net.sf.saxon.Configuration) XPathException(net.sf.saxon.trans.XPathException) Collectors(java.util.stream.Collectors) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) NodeInfo(net.sf.saxon.om.NodeInfo) XPathExpression(net.sf.saxon.sxpath.XPathExpression) Item(net.sf.saxon.om.Item) NodeInfo(net.sf.saxon.om.NodeInfo) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Example 5 with XPathExpression

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);
    }
}
Also used : XPathExpression(net.sf.saxon.sxpath.XPathExpression) RootNode(com.puppycrawl.tools.checkstyle.xpath.RootNode) Item(net.sf.saxon.om.Item) XPathException(net.sf.saxon.trans.XPathException) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Aggregations

XPathExpression (net.sf.saxon.sxpath.XPathExpression)6 XPathEvaluator (net.sf.saxon.sxpath.XPathEvaluator)5 Item (net.sf.saxon.om.Item)4 XPathDynamicContext (net.sf.saxon.sxpath.XPathDynamicContext)4 XPathException (net.sf.saxon.trans.XPathException)4 XMLColumn (org.teiid.query.sql.lang.XMLTable.XMLColumn)2 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 AbstractNode (com.puppycrawl.tools.checkstyle.xpath.AbstractNode)1 RootNode (com.puppycrawl.tools.checkstyle.xpath.RootNode)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Source (javax.xml.transform.Source)1 StAXSource (javax.xml.transform.stax.StAXSource)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Configuration (net.sf.saxon.Configuration)1 NodeInfo (net.sf.saxon.om.NodeInfo)1 SequenceIterator (net.sf.saxon.om.SequenceIterator)1 EqualsVerifierReport (nl.jqno.equalsverifier.EqualsVerifierReport)1 Test (org.junit.jupiter.api.Test)1