Search in sources :

Example 6 with StreamSource

use of javax.xml.transform.stream.StreamSource in project camel by apache.

the class CacheInputStreamInDeadLetterIssue520Test method testSendingSource.

public void testSendingSource() throws Exception {
    StreamSource message = new StreamSource(new StringReader("<hello>Willem</hello>"));
    sendingMessage(message);
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader)

Example 7 with StreamSource

use of javax.xml.transform.stream.StreamSource in project camel by apache.

the class ValidatingProcessor method doProcess.

protected void doProcess(Exchange exchange) throws Exception {
    Schema schema;
    if (isUseSharedSchema()) {
        schema = getSchema();
    } else {
        schema = createSchema();
    }
    Validator validator = schema.newValidator();
    // the underlying input stream, which we need to close to avoid locking files or other resources
    Source source = null;
    InputStream is = null;
    try {
        Result result = null;
        // only convert to input stream if really needed
        if (isInputStreamNeeded(exchange)) {
            is = getContentToValidate(exchange, InputStream.class);
            if (is != null) {
                source = getSource(exchange, is);
            }
        } else {
            Object content = getContentToValidate(exchange);
            if (content != null) {
                source = getSource(exchange, content);
            }
        }
        if (shouldUseHeader()) {
            if (source == null && isFailOnNullHeader()) {
                throw new NoXmlHeaderValidationException(exchange, headerName);
            }
        } else {
            if (source == null && isFailOnNullBody()) {
                throw new NoXmlBodyValidationException(exchange);
            }
        }
        //CAMEL-7036 We don't need to set the result if the source is an instance of StreamSource
        if (source instanceof DOMSource) {
            result = new DOMResult();
        } else if (source instanceof SAXSource) {
            result = new SAXResult();
        } else if (source instanceof StAXSource || source instanceof StreamSource) {
            result = null;
        }
        if (source != null) {
            // create a new errorHandler and set it on the validator
            // must be a local instance to avoid problems with concurrency (to be
            // thread safe)
            ValidatorErrorHandler handler = errorHandler.getClass().newInstance();
            validator.setErrorHandler(handler);
            try {
                LOG.trace("Validating {}", source);
                validator.validate(source, result);
                handler.handleErrors(exchange, schema, result);
            } catch (SAXParseException e) {
                // can be thrown for non well formed XML
                throw new SchemaValidationException(exchange, schema, Collections.singletonList(e), Collections.<SAXParseException>emptyList(), Collections.<SAXParseException>emptyList());
            }
        }
    } finally {
        IOHelper.close(is);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) 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) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) DOMResult(javax.xml.transform.dom.DOMResult) SAXSource(javax.xml.transform.sax.SAXSource) SAXResult(javax.xml.transform.sax.SAXResult) SAXParseException(org.xml.sax.SAXParseException) Validator(javax.xml.validation.Validator)

Example 8 with StreamSource

use of javax.xml.transform.stream.StreamSource in project camel by apache.

the class StreamCachingInterceptorTest method testConvertStreamSourceWithRouteOnlyStreamCaching.

public void testConvertStreamSourceWithRouteOnlyStreamCaching() throws Exception {
    b.expectedMessageCount(1);
    StreamSource message = new StreamSource(new StringReader(MESSAGE));
    template.sendBody("direct:b", message);
    assertMockEndpointsSatisfied();
    assertTrue(b.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
    assertEquals(b.assertExchangeReceived(0).getIn().getBody(String.class), MESSAGE);
}
Also used : StreamCache(org.apache.camel.StreamCache) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader)

Example 9 with StreamSource

use of javax.xml.transform.stream.StreamSource in project camel by apache.

the class XmlConverter method toStreamSourceFromSAX.

@Converter
public StreamSource toStreamSourceFromSAX(SAXSource source, Exchange exchange) throws TransformerException {
    InputSource inputSource = source.getInputSource();
    if (inputSource != null) {
        if (inputSource.getCharacterStream() != null) {
            return new StreamSource(inputSource.getCharacterStream());
        }
        if (inputSource.getByteStream() != null) {
            return new StreamSource(inputSource.getByteStream());
        }
    }
    String result = toString(source, exchange);
    return new StringSource(result);
}
Also used : InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) StringSource(org.apache.camel.StringSource) Converter(org.apache.camel.Converter)

Example 10 with StreamSource

use of javax.xml.transform.stream.StreamSource in project camel by apache.

the class XmlConverterTest method testToStreamSourceByFile.

public void testToStreamSourceByFile() throws Exception {
    XmlConverter conv = new XmlConverter();
    File file = new File("org/apache/camel/converter/stream/test.xml");
    StreamSource source = conv.toStreamSource(file);
    StreamSource out = conv.toStreamSource(source, null);
    assertSame(source, out);
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) File(java.io.File)

Aggregations

StreamSource (javax.xml.transform.stream.StreamSource)338 Source (javax.xml.transform.Source)115 StringReader (java.io.StringReader)101 StreamResult (javax.xml.transform.stream.StreamResult)85 Transformer (javax.xml.transform.Transformer)74 Test (org.junit.Test)73 InputStream (java.io.InputStream)68 TransformerFactory (javax.xml.transform.TransformerFactory)58 ByteArrayInputStream (java.io.ByteArrayInputStream)56 IOException (java.io.IOException)52 DOMSource (javax.xml.transform.dom.DOMSource)49 TransformerException (javax.xml.transform.TransformerException)48 StringWriter (java.io.StringWriter)45 SchemaFactory (javax.xml.validation.SchemaFactory)44 InputSource (org.xml.sax.InputSource)44 Schema (javax.xml.validation.Schema)43 SAXException (org.xml.sax.SAXException)42 SAXSource (javax.xml.transform.sax.SAXSource)33 Validator (javax.xml.validation.Validator)31 File (java.io.File)27