Search in sources :

Example 1 with Item

use of net.sf.saxon.om.Item in project camel by apache.

the class XQueryBuilder method createDynamicContext.

/**
     * Creates a dynamic context for the given exchange
     */
protected DynamicQueryContext createDynamicContext(Exchange exchange) throws Exception {
    Configuration config = getConfiguration();
    DynamicQueryContext dynamicQueryContext = new DynamicQueryContext(config);
    Message in = exchange.getIn();
    Item item = null;
    if (ObjectHelper.isNotEmpty(getHeaderName())) {
        item = in.getHeader(getHeaderName(), Item.class);
    } else {
        item = in.getBody(Item.class);
    }
    if (item != null) {
        dynamicQueryContext.setContextItem(item);
    } else {
        Object body = null;
        if (ObjectHelper.isNotEmpty(getHeaderName())) {
            body = in.getHeader(getHeaderName());
        } else {
            body = in.getBody();
        }
        // the underlying input stream, which we need to close to avoid locking files or other resources
        InputStream is = null;
        try {
            Source source;
            // only convert to input stream if really needed
            if (isInputStreamNeeded(exchange)) {
                if (ObjectHelper.isNotEmpty(getHeaderName())) {
                    is = exchange.getIn().getHeader(getHeaderName(), InputStream.class);
                } else {
                    is = exchange.getIn().getBody(InputStream.class);
                }
                source = getSource(exchange, is);
            } else {
                source = getSource(exchange, body);
            }
            // special for bean invocation
            if (source == null) {
                if (body instanceof BeanInvocation) {
                    // if its a null bean invocation then handle that
                    BeanInvocation bi = exchange.getContext().getTypeConverter().convertTo(BeanInvocation.class, body);
                    if (bi.getArgs() != null && bi.getArgs().length == 1 && bi.getArgs()[0] == null) {
                        // its a null argument from the bean invocation so use null as answer
                        source = null;
                    }
                }
            }
            if (source == null) {
                // indicate it was not possible to convert to a Source type
                throw new NoTypeConversionAvailableException(body, Source.class);
            }
            DocumentInfo doc = config.buildDocument(source);
            dynamicQueryContext.setContextItem(doc);
        } finally {
            // can deal if is is null
            IOHelper.close(is);
        }
    }
    configureQuery(dynamicQueryContext, exchange);
    // call the reset if the in message body is StreamCache
    MessageHelper.resetStreamCache(exchange.getIn());
    return dynamicQueryContext;
}
Also used : Item(net.sf.saxon.om.Item) Configuration(net.sf.saxon.Configuration) Message(org.apache.camel.Message) DynamicQueryContext(net.sf.saxon.query.DynamicQueryContext) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) InputStream(java.io.InputStream) BeanInvocation(org.apache.camel.component.bean.BeanInvocation) BytesSource(org.apache.camel.BytesSource) StringSource(org.apache.camel.StringSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) DocumentInfo(net.sf.saxon.om.DocumentInfo)

Example 2 with Item

use of net.sf.saxon.om.Item in project camel by apache.

the class XQueryBuilder method evaluateAsString.

public String evaluateAsString(Exchange exchange) throws Exception {
    LOG.debug("evaluateAsString: {} for exchange: {}", expression, exchange);
    initialize(exchange);
    StringWriter buffer = new StringWriter();
    SequenceIterator iter = getExpression().iterator(createDynamicContext(exchange));
    for (Item item = iter.next(); item != null; item = iter.next()) {
        buffer.append(item.getStringValueCS());
    }
    String answer = buffer.toString();
    buffer.close();
    return answer;
}
Also used : Item(net.sf.saxon.om.Item) StringWriter(java.io.StringWriter) SequenceIterator(net.sf.saxon.om.SequenceIterator)

Example 3 with Item

use of net.sf.saxon.om.Item in project camel by apache.

the class MyExtensionFunction1 method makeCallExpression.

@Override
public ExtensionFunctionCall makeCallExpression() {
    return new ExtensionFunctionCall() {

        private static final long serialVersionUID = 1L;

        @Override
        public Sequence call(XPathContext xPathContext, Sequence[] arguments) throws XPathException {
            // 1st argument (mandatory, index 0)
            Int64Value arg1 = (Int64Value) arguments[0].iterate().next();
            int arg1Int = arg1.getDecimalValue().toBigInteger().intValue();
            // 2nd argument (mandatory, index 1)
            Int64Value arg2 = (Int64Value) arguments[1].iterate().next();
            int arg2Int = arg2.getDecimalValue().toBigInteger().intValue();
            // Functionality goes here
            int resultInt = arg1Int + arg2Int;
            Item result = new Int64Value(resultInt);
            return SequenceTool.toLazySequence(SingletonIterator.makeIterator(result));
        }
    };
}
Also used : Item(net.sf.saxon.om.Item) Int64Value(net.sf.saxon.value.Int64Value) ExtensionFunctionCall(net.sf.saxon.lib.ExtensionFunctionCall) XPathContext(net.sf.saxon.expr.XPathContext)

Example 4 with Item

use of net.sf.saxon.om.Item in project pentaho-platform by pentaho.

the class XQResultSet method evaluate.

protected List evaluate() throws XPathException {
    SequenceIterator sequenceiterator = exp.iterator(dynamicContext);
    List rtn = new ArrayList(100);
    int rowCount = 0;
    int maxRows = (this.connection != null) ? this.connection.getMaxRows() : -1;
    Item item = null;
    while ((item = sequenceiterator.next()) != null) {
        if ((item == null)) {
            break;
        }
        rowCount++;
        if ((maxRows >= 0) && (rowCount > maxRows)) {
            break;
        }
        rtn.add(Value.convertToJava(item));
    }
    return rtn;
}
Also used : Item(net.sf.saxon.om.Item) SequenceIterator(net.sf.saxon.om.SequenceIterator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with Item

use of net.sf.saxon.om.Item 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)

Aggregations

Item (net.sf.saxon.om.Item)14 XPathDynamicContext (net.sf.saxon.sxpath.XPathDynamicContext)6 XPathException (net.sf.saxon.trans.XPathException)6 XPathExpression (net.sf.saxon.sxpath.XPathExpression)5 Configuration (net.sf.saxon.Configuration)4 SequenceIterator (net.sf.saxon.om.SequenceIterator)4 XPathEvaluator (net.sf.saxon.sxpath.XPathEvaluator)4 RootNode (com.puppycrawl.tools.checkstyle.xpath.RootNode)3 List (java.util.List)3 StreamSource (javax.xml.transform.stream.StreamSource)3 XMLType (org.teiid.core.types.XMLType)3 AbstractNode (com.puppycrawl.tools.checkstyle.xpath.AbstractNode)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Collectors (java.util.stream.Collectors)2 Source (javax.xml.transform.Source)2 StAXSource (javax.xml.transform.stax.StAXSource)2 XPathContext (net.sf.saxon.expr.XPathContext)2 ExtensionFunctionCall (net.sf.saxon.lib.ExtensionFunctionCall)2 NodeInfo (net.sf.saxon.om.NodeInfo)2