Search in sources :

Example 46 with Source

use of javax.xml.transform.Source 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 47 with Source

use of javax.xml.transform.Source in project camel by apache.

the class SaxonUriResolverTest method test.

@Test
public void test() throws Exception {
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    Source xsl = fromClasspath(XSL_PATH);
    xsl.setSystemId("classpath:/" + XSL_PATH);
    Source xml = fromString(XML_DATA);
    TransformerFactory factory = new TransformerFactoryImpl();
    Transformer transformer = factory.newTransformer(xsl);
    transformer.setURIResolver(new XsltUriResolver(context(), XSL_PATH));
    transformer.transform(xml, result);
    Assert.assertEquals(XML_RESP, writer.toString());
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerFactoryImpl(net.sf.saxon.TransformerFactoryImpl) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) XsltUriResolver(org.apache.camel.builder.xml.XsltUriResolver) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 48 with Source

use of javax.xml.transform.Source in project camel by apache.

the class XAdESSignaturePropertiesTest method validateAgainstSchema.

private void validateAgainstSchema(Document doc) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schema1 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/XAdES.xsd"));
    Source schema2 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/xmldsig-core-schema.xsd"));
    Schema schema = factory.newSchema(new Source[] { schema2, schema1 });
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(doc));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) File(java.io.File) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 49 with Source

use of javax.xml.transform.Source in project camel by apache.

the class ProducerLocalRouteTest method consumeStockQuoteWebserviceWithCamelStringSourceInput.

@Test
public void consumeStockQuoteWebserviceWithCamelStringSourceInput() throws Exception {
    Object result = template.requestBody("direct:stockQuoteWebservice", new StringSource(xmlRequestForGoogleStockQuote));
    assertNotNull(result);
    assertTrue(result instanceof Source);
}
Also used : StringSource(org.apache.camel.StringSource) Source(javax.xml.transform.Source) StringSource(org.apache.camel.StringSource) Test(org.junit.Test)

Example 50 with Source

use of javax.xml.transform.Source in project asciidoctor-fopub by asciidoctor.

the class InputHandler method transformTo.

/**
     * Transforms the input document to the input format expected by FOP using XSLT.
     * @param result the Result object where the result of the XSL transformation is sent to
     * @throws FOPException in case of an error during processing
     */
protected void transformTo(Result result) throws FOPException {
    try {
        // Setup XSLT
        TransformerFactory factory = TransformerFactory.newInstance();
        if (uriResolver != null) {
            factory.setURIResolver(uriResolver);
        }
        factory.setErrorListener(this);
        Transformer transformer;
        Source xsltSource = createXSLTSource();
        if (xsltSource == null) {
            // FO Input
            transformer = factory.newTransformer();
        } else {
            // XML/XSLT input
            transformer = factory.newTransformer(xsltSource);
            // Set the value of parameters, if any, defined for stylesheet
            if (xsltParams != null) {
                for (int i = 0; i < xsltParams.size(); i += 2) {
                    transformer.setParameter((String) xsltParams.elementAt(i), (String) xsltParams.elementAt(i + 1));
                }
            }
        }
        transformer.setErrorListener(this);
        // Create a SAXSource from the input Source file
        Source src = createMainSource();
        // Start XSLT transformation and FOP processing
        transformer.transform(src, result);
    } catch (Exception e) {
        throw new FOPException(e);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) TransformerException(javax.xml.transform.TransformerException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Aggregations

Source (javax.xml.transform.Source)238 StreamSource (javax.xml.transform.stream.StreamSource)161 DOMSource (javax.xml.transform.dom.DOMSource)108 Transformer (javax.xml.transform.Transformer)76 StreamResult (javax.xml.transform.stream.StreamResult)74 InputSource (org.xml.sax.InputSource)67 SAXSource (javax.xml.transform.sax.SAXSource)56 StringReader (java.io.StringReader)52 IOException (java.io.IOException)46 TransformerException (javax.xml.transform.TransformerException)45 Result (javax.xml.transform.Result)42 TransformerFactory (javax.xml.transform.TransformerFactory)41 Test (org.junit.Test)39 StringWriter (java.io.StringWriter)35 InputStream (java.io.InputStream)32 SAXException (org.xml.sax.SAXException)32 Schema (javax.xml.validation.Schema)29 Validator (javax.xml.validation.Validator)29 SchemaFactory (javax.xml.validation.SchemaFactory)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26