Search in sources :

Example 1 with XPathEvaluator

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

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);
    }
}
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 3 with XPathEvaluator

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);
    }
}
Also used : XPathStaticContext(net.sf.saxon.sxpath.XPathStaticContext) XPathVariable(net.sf.saxon.sxpath.XPathVariable) XPathException(net.sf.saxon.trans.XPathException) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) AbstractStaticContext(net.sf.saxon.sxpath.AbstractStaticContext)

Aggregations

XPathEvaluator (net.sf.saxon.sxpath.XPathEvaluator)3 XPathExpression (net.sf.saxon.sxpath.XPathExpression)2 XPathException (net.sf.saxon.trans.XPathException)2 Source (javax.xml.transform.Source)1 StAXSource (javax.xml.transform.stax.StAXSource)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Item (net.sf.saxon.om.Item)1 AbstractStaticContext (net.sf.saxon.sxpath.AbstractStaticContext)1 XPathDynamicContext (net.sf.saxon.sxpath.XPathDynamicContext)1 XPathStaticContext (net.sf.saxon.sxpath.XPathStaticContext)1 XPathVariable (net.sf.saxon.sxpath.XPathVariable)1 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)1 XMLColumn (org.teiid.query.sql.lang.XMLTable.XMLColumn)1