Search in sources :

Example 16 with Source

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

the class FallbackTypeConverter method unmarshal.

protected Object unmarshal(Unmarshaller unmarshaller, Exchange exchange, Object value) throws JAXBException, UnsupportedEncodingException, XMLStreamException {
    try {
        XMLStreamReader xmlReader;
        if (value instanceof XMLStreamReader) {
            xmlReader = (XMLStreamReader) value;
        } else if (value instanceof InputStream) {
            if (needFiltering(exchange)) {
                xmlReader = staxConverter.createXMLStreamReader(new NonXmlFilterReader(new InputStreamReader((InputStream) value, IOHelper.getCharsetName(exchange))));
            } else {
                xmlReader = staxConverter.createXMLStreamReader((InputStream) value, exchange);
            }
        } else if (value instanceof Reader) {
            Reader reader = (Reader) value;
            if (needFiltering(exchange)) {
                if (!(value instanceof NonXmlFilterReader)) {
                    reader = new NonXmlFilterReader((Reader) value);
                }
            }
            xmlReader = staxConverter.createXMLStreamReader(reader);
        } else if (value instanceof Source) {
            xmlReader = staxConverter.createXMLStreamReader((Source) value);
        } else {
            throw new IllegalArgumentException("Cannot convert from " + value.getClass());
        }
        return unmarshaller.unmarshal(xmlReader);
    } finally {
        if (value instanceof Closeable) {
            IOHelper.close((Closeable) value, "Unmarshalling", LOG);
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Closeable(java.io.Closeable) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Source(javax.xml.transform.Source)

Example 17 with Source

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

the class JingValidator method process.

public void process(Exchange exchange) throws Exception {
    Jaxp11XMLReaderCreator xmlCreator = new Jaxp11XMLReaderCreator();
    DefaultValidationErrorHandler errorHandler = new DefaultValidationErrorHandler();
    PropertyMapBuilder mapBuilder = new PropertyMapBuilder();
    mapBuilder.put(ValidateProperty.XML_READER_CREATOR, xmlCreator);
    mapBuilder.put(ValidateProperty.ERROR_HANDLER, errorHandler);
    PropertyMap propertyMap = mapBuilder.toPropertyMap();
    Validator validator = getSchema().createValidator(propertyMap);
    Message in = exchange.getIn();
    SAXSource saxSource = in.getBody(SAXSource.class);
    if (saxSource == null) {
        Source source = exchange.getIn().getMandatoryBody(Source.class);
        saxSource = ExchangeHelper.convertToMandatoryType(exchange, SAXSource.class, source);
    }
    InputSource bodyInput = saxSource.getInputSource();
    // now lets parse the body using the validator
    XMLReader reader = xmlCreator.createXMLReader();
    reader.setContentHandler(validator.getContentHandler());
    reader.setDTDHandler(validator.getDTDHandler());
    reader.setErrorHandler(errorHandler);
    reader.parse(bodyInput);
    errorHandler.handleErrors(exchange, schema);
}
Also used : InputSource(org.xml.sax.InputSource) Jaxp11XMLReaderCreator(com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator) PropertyMap(com.thaiopensource.util.PropertyMap) SAXSource(javax.xml.transform.sax.SAXSource) Message(org.apache.camel.Message) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder) DefaultValidationErrorHandler(org.apache.camel.processor.validation.DefaultValidationErrorHandler) Validator(com.thaiopensource.validate.Validator) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader)

Example 18 with Source

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

the class JmsXMLRouteTest method testLondonWithFileStreamAsObject.

@Test
public void testLondonWithFileStreamAsObject() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:london");
    mock.expectedMessageCount(1);
    mock.message(0).body(String.class).contains("James");
    Source source = new StreamSource(new FileInputStream(TEST_LONDON));
    assertNotNull(source);
    template.sendBody("direct:object", source);
    assertMockEndpointsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StreamSource(javax.xml.transform.stream.StreamSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StringSource(org.apache.camel.StringSource) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 19 with Source

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

the class JmsXMLRouteTest method testLondonWithFileStreamAsDefault.

@Test
public void testLondonWithFileStreamAsDefault() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:london");
    mock.expectedMessageCount(1);
    mock.message(0).body(String.class).contains("James");
    Source source = new StreamSource(new FileInputStream(TEST_LONDON));
    assertNotNull(source);
    template.sendBody("direct:default", source);
    assertMockEndpointsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StreamSource(javax.xml.transform.stream.StreamSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StringSource(org.apache.camel.StringSource) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 20 with Source

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

the class JmsXMLRouteTest method testTampaWithFileStreamAsDefault.

@Test
public void testTampaWithFileStreamAsDefault() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:tampa");
    mock.expectedMessageCount(1);
    mock.message(0).body(String.class).contains("Hiram");
    Source source = new StreamSource(new FileInputStream(TEST_TAMPA));
    assertNotNull(source);
    template.sendBody("direct:default", source);
    assertMockEndpointsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StreamSource(javax.xml.transform.stream.StreamSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StringSource(org.apache.camel.StringSource) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

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