Search in sources :

Example 1 with DocumentInfo

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

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

the class SaxonConverter method toDOMDocument.

@Converter
public static Document toDOMDocument(NodeInfo node) throws XPathException {
    switch(node.getNodeKind()) {
        case Type.DOCUMENT:
            // DOCUMENT type nodes can be wrapped directly
            return (Document) NodeOverNodeInfo.wrap(node);
        case Type.ELEMENT:
            // ELEMENT nodes need to build a new DocumentInfo before wrapping
            Configuration config = node.getConfiguration();
            DocumentInfo documentInfo = config.buildDocument(node);
            return (Document) NodeOverNodeInfo.wrap(documentInfo);
        default:
            return null;
    }
}
Also used : Configuration(net.sf.saxon.Configuration) Document(org.w3c.dom.Document) DocumentInfo(net.sf.saxon.om.DocumentInfo) FallbackConverter(org.apache.camel.FallbackConverter) Converter(org.apache.camel.Converter) TypeConverter(org.apache.camel.TypeConverter)

Aggregations

Configuration (net.sf.saxon.Configuration)2 DocumentInfo (net.sf.saxon.om.DocumentInfo)2 InputStream (java.io.InputStream)1 Source (javax.xml.transform.Source)1 DOMSource (javax.xml.transform.dom.DOMSource)1 SAXSource (javax.xml.transform.sax.SAXSource)1 StAXSource (javax.xml.transform.stax.StAXSource)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Item (net.sf.saxon.om.Item)1 DynamicQueryContext (net.sf.saxon.query.DynamicQueryContext)1 BytesSource (org.apache.camel.BytesSource)1 Converter (org.apache.camel.Converter)1 FallbackConverter (org.apache.camel.FallbackConverter)1 Message (org.apache.camel.Message)1 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)1 StringSource (org.apache.camel.StringSource)1 TypeConverter (org.apache.camel.TypeConverter)1 BeanInvocation (org.apache.camel.component.bean.BeanInvocation)1 Document (org.w3c.dom.Document)1