Search in sources :

Example 1 with StAX2SAXSource

use of org.apache.camel.converter.jaxp.StAX2SAXSource in project camel by apache.

the class XsltBuilder method process.

public void process(Exchange exchange) throws Exception {
    notNull(getTemplate(), "template");
    if (isDeleteOutputFile()) {
        // add on completion so we can delete the file when the Exchange is done
        String fileName = ExchangeHelper.getMandatoryHeader(exchange, Exchange.XSLT_FILE_NAME, String.class);
        exchange.addOnCompletion(new XsltBuilderOnCompletion(fileName));
    }
    Transformer transformer = getTransformer();
    configureTransformer(transformer, exchange);
    ResultHandler resultHandler = resultHandlerFactory.createResult(exchange);
    Result result = resultHandler.getResult();
    // let's copy the headers before we invoke the transform in case they modify them
    Message out = exchange.getOut();
    out.copyFrom(exchange.getIn());
    // 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)) {
            is = exchange.getIn().getBody(InputStream.class);
            source = getSource(exchange, is);
        } else {
            Object body = exchange.getIn().getBody();
            source = getSource(exchange, body);
        }
        if (source instanceof StAXSource) {
            // Always convert StAXSource to SAXSource.
            // * Xalan and Saxon-B don't support StAXSource.
            // * The JDK default implementation (XSLTC) doesn't handle CDATA events
            //   (see com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX).
            // * Saxon-HE/PE/EE seem to support StAXSource, but don't advertise this
            //   officially (via TransformerFactory.getFeature(StAXSource.FEATURE))
            source = new StAX2SAXSource(((StAXSource) source).getXMLStreamReader());
        }
        LOG.trace("Using {} as source", source);
        transformer.transform(source, result);
        LOG.trace("Transform complete with result {}", result);
        resultHandler.setBody(out);
    } finally {
        releaseTransformer(transformer);
        // IOHelper can handle if is is null
        IOHelper.close(is);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Message(org.apache.camel.Message) InputStream(java.io.InputStream) StAX2SAXSource(org.apache.camel.converter.jaxp.StAX2SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) StAX2SAXSource(org.apache.camel.converter.jaxp.StAX2SAXSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) Result(javax.xml.transform.Result)

Aggregations

InputStream (java.io.InputStream)1 Result (javax.xml.transform.Result)1 Source (javax.xml.transform.Source)1 Transformer (javax.xml.transform.Transformer)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 Message (org.apache.camel.Message)1 StAX2SAXSource (org.apache.camel.converter.jaxp.StAX2SAXSource)1