Search in sources :

Example 1 with Configuration

use of net.sf.saxon.Configuration in project camel by apache.

the class XQueryBuilder method initialize.

/**
     * Initializes this builder - <b>Must be invoked before evaluation</b>.
     */
protected synchronized void initialize(Exchange exchange) throws XPathException, IOException {
    // must use synchronized for concurrency issues and only let it initialize once
    if (!initialized.get()) {
        LOG.debug("Initializing XQueryBuilder {}", this);
        if (configuration == null) {
            configuration = new Configuration();
            configuration.setStripsWhiteSpace(isStripsAllWhiteSpace() ? Whitespace.ALL : Whitespace.IGNORABLE);
            LOG.debug("Created new Configuration {}", configuration);
        } else {
            LOG.debug("Using existing Configuration {}", configuration);
        }
        if (configurationProperties != null && !configurationProperties.isEmpty()) {
            for (Map.Entry<String, Object> entry : configurationProperties.entrySet()) {
                configuration.setConfigurationProperty(entry.getKey(), entry.getValue());
            }
        }
        staticQueryContext = getConfiguration().newStaticQueryContext();
        if (moduleURIResolver != null) {
            staticQueryContext.setModuleURIResolver(moduleURIResolver);
        }
        Set<Map.Entry<String, String>> entries = namespacePrefixes.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String prefix = entry.getKey();
            String uri = entry.getValue();
            staticQueryContext.declareNamespace(prefix, uri);
            staticQueryContext.setInheritNamespaces(true);
        }
        expression = createQueryExpression(staticQueryContext);
        initialized.set(true);
    }
    // let the configuration be accessible on the exchange as its shared for this evaluation
    // and can be needed by 3rd party type converters or other situations (camel-artixds)
    exchange.setProperty("CamelSaxonConfiguration", configuration);
}
Also used : Configuration(net.sf.saxon.Configuration) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Configuration

use of net.sf.saxon.Configuration 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 3 with Configuration

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

Example 4 with Configuration

use of net.sf.saxon.Configuration in project camel by apache.

the class SaxonXsltEndpointConfigurationTest method testConfiguration.

@Test
public void testConfiguration() throws Exception {
    Configuration configuration = context.getRegistry().lookupByNameAndType("saxon-configuration", Configuration.class);
    Map<String, Object> properties = context.getRegistry().lookupByNameAndType("saxon-properties", Map.class);
    XsltComponent component = context.getComponent("xslt", XsltComponent.class);
    XsltEndpoint endpoint = null;
    assertNotNull(configuration);
    assertNotNull(properties);
    for (Endpoint ep : context.getEndpoints()) {
        if (ep instanceof XsltEndpoint) {
            endpoint = (XsltEndpoint) ep;
            break;
        }
    }
    assertNotNull(component);
    assertNotNull(endpoint);
    assertNull(component.getSaxonConfiguration());
    assertTrue(component.getSaxonConfigurationProperties().isEmpty());
    assertNotNull(endpoint.getSaxonConfiguration());
    assertNotNull(endpoint.getSaxonConfigurationProperties());
    assertEquals(configuration, endpoint.getSaxonConfiguration());
    assertEquals(properties, endpoint.getSaxonConfigurationProperties());
}
Also used : Configuration(net.sf.saxon.Configuration) Endpoint(org.apache.camel.Endpoint) Test(org.junit.Test)

Example 5 with Configuration

use of net.sf.saxon.Configuration in project sirix by sirixdb.

the class XPathEvaluator method call.

@Override
public XPathSelector call() throws Exception {
    final Processor proc = new Processor(false);
    final Configuration config = proc.getUnderlyingConfiguration();
    final NodeInfo doc = new DocumentWrapper(mSession, mRevision, config);
    final XPathCompiler xpath = proc.newXPathCompiler();
    final DocumentBuilder builder = proc.newDocumentBuilder();
    final XdmItem item = builder.wrap(doc);
    final XPathSelector selector = xpath.compile(mExpression).load();
    selector.setContextItem(item);
    return selector;
}
Also used : DocumentWrapper(org.sirix.saxon.wrapper.DocumentWrapper) Processor(net.sf.saxon.s9api.Processor) Configuration(net.sf.saxon.Configuration) XPathCompiler(net.sf.saxon.s9api.XPathCompiler) DocumentBuilder(net.sf.saxon.s9api.DocumentBuilder) NodeInfo(net.sf.saxon.om.NodeInfo) XPathSelector(net.sf.saxon.s9api.XPathSelector) XdmItem(net.sf.saxon.s9api.XdmItem)

Aggregations

Configuration (net.sf.saxon.Configuration)16 Processor (net.sf.saxon.s9api.Processor)7 NodeInfo (net.sf.saxon.om.NodeInfo)6 DocumentWrapper (org.sirix.saxon.wrapper.DocumentWrapper)5 SaxonApiException (net.sf.saxon.s9api.SaxonApiException)4 StreamSource (javax.xml.transform.stream.StreamSource)3 XQueryCompiler (net.sf.saxon.s9api.XQueryCompiler)3 XQueryExecutable (net.sf.saxon.s9api.XQueryExecutable)3 DocumentInfo (net.sf.saxon.om.DocumentInfo)2 Item (net.sf.saxon.om.Item)2 DynamicQueryContext (net.sf.saxon.query.DynamicQueryContext)2 Serializer (net.sf.saxon.s9api.Serializer)2 Test (org.junit.Test)2 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)2 ResourceConfiguration (org.sirix.access.conf.ResourceConfiguration)2 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)2 MapBasedXPathFunctionResolver (com.helger.xml.xpath.MapBasedXPathFunctionResolver)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SQLException (java.sql.SQLException)1