Search in sources :

Example 1 with XPathDynamicContext

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

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);
    }
}
Also used : DocumentNode(net.sourceforge.pmd.lang.ast.xpath.saxon.DocumentNode) XPathException(net.sf.saxon.trans.XPathException) DocumentNode(net.sourceforge.pmd.lang.ast.xpath.saxon.DocumentNode) Node(net.sourceforge.pmd.lang.ast.Node) ElementNode(net.sourceforge.pmd.lang.ast.xpath.saxon.ElementNode) ArrayList(java.util.ArrayList) ElementNode(net.sourceforge.pmd.lang.ast.xpath.saxon.ElementNode) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Example 3 with XPathDynamicContext

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

Example 4 with XPathDynamicContext

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

Example 5 with XPathDynamicContext

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

Aggregations

XPathDynamicContext (net.sf.saxon.sxpath.XPathDynamicContext)8 Item (net.sf.saxon.om.Item)6 XPathException (net.sf.saxon.trans.XPathException)6 XPathExpression (net.sf.saxon.sxpath.XPathExpression)5 XPathEvaluator (net.sf.saxon.sxpath.XPathEvaluator)4 RootNode (com.puppycrawl.tools.checkstyle.xpath.RootNode)3 AbstractNode (com.puppycrawl.tools.checkstyle.xpath.AbstractNode)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Configuration (net.sf.saxon.Configuration)2 StatelessCheck (com.puppycrawl.tools.checkstyle.StatelessCheck)1 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)1 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)1 CommonUtil (com.puppycrawl.tools.checkstyle.utils.CommonUtil)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Source (javax.xml.transform.Source)1 StAXSource (javax.xml.transform.stax.StAXSource)1