Search in sources :

Example 21 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project camel by apache.

the class XStreamDataFormat method createHierarchicalStreamReader.

protected HierarchicalStreamReader createHierarchicalStreamReader(Exchange exchange, InputStream stream) throws XMLStreamException {
    updateCharactorEncodingInfo(exchange);
    if (getXstreamDriver() != null) {
        return getXstreamDriver().createReader(stream);
    }
    XMLStreamReader xmlReader = getStaxConverter().createXMLStreamReader(stream, exchange);
    return new StaxReader(new QNameMap(), xmlReader);
}
Also used : StaxReader(com.thoughtworks.xstream.io.xml.StaxReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap)

Example 22 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project camel by apache.

the class JaxbDataFormat method unmarshal.

public Object unmarshal(Exchange exchange, InputStream stream) throws IOException, SAXException {
    try {
        Object answer;
        XMLStreamReader xmlReader;
        if (needFiltering(exchange)) {
            xmlReader = typeConverter.convertTo(XMLStreamReader.class, createNonXmlFilterReader(exchange, stream));
        } else {
            xmlReader = typeConverter.convertTo(XMLStreamReader.class, stream);
        }
        if (partialClass != null) {
            // partial unmarshalling
            answer = createUnmarshaller().unmarshal(xmlReader, partialClass);
        } else {
            answer = createUnmarshaller().unmarshal(xmlReader);
        }
        if (answer instanceof JAXBElement && isIgnoreJAXBElement()) {
            answer = ((JAXBElement<?>) answer).getValue();
        }
        return answer;
    } catch (JAXBException e) {
        throw new IOException(e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException)

Example 23 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader 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 24 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project camel by apache.

the class XmlConverter method toStAXSource.

/**
     * Converts the source instance to a {@link StAXSource} or returns null if the conversion is not
     * supported (making it easy to derive from this class to add new kinds of conversion).
     * @throws FileNotFoundException
     * @throws XMLStreamException
     */
@Converter
public StAXSource toStAXSource(File file, Exchange exchange) throws FileNotFoundException, XMLStreamException {
    InputStream is = IOHelper.buffered(new FileInputStream(file));
    XMLStreamReader r = new StaxConverter().createXMLStreamReader(is, exchange);
    return new StAXSource(r);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StAXSource(javax.xml.transform.stax.StAXSource) FileInputStream(java.io.FileInputStream) Converter(org.apache.camel.Converter)

Example 25 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project camel by apache.

the class StaxConverterTest method testEncodingXmlStreamReader.

public void testEncodingXmlStreamReader() throws Exception {
    TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM.reset();
    XMLStreamReader reader = null;
    XMLStreamWriter writer = null;
    ByteArrayOutputStream output = null;
    try {
        // enter text encoded with Latin1
        reader = context.getTypeConverter().mandatoryConvertTo(XMLStreamReader.class, TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM);
        output = new ByteArrayOutputStream();
        // ensure UTF-8 encoding
        Exchange exchange = new DefaultExchange(context);
        exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.name());
        writer = context.getTypeConverter().mandatoryConvertTo(XMLStreamWriter.class, exchange, output);
        // copy to writer
        while (reader.hasNext()) {
            reader.next();
            switch(reader.getEventType()) {
                case XMLStreamConstants.START_DOCUMENT:
                    writer.writeStartDocument();
                    break;
                case XMLStreamConstants.END_DOCUMENT:
                    writer.writeEndDocument();
                    break;
                case XMLStreamConstants.START_ELEMENT:
                    writer.writeStartElement(reader.getName().getLocalPart());
                    break;
                case XMLStreamConstants.CHARACTERS:
                    writer.writeCharacters(reader.getText());
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    writer.writeEndElement();
                    break;
                default:
                    break;
            }
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
    assertNotNull(output);
    String result = new String(output.toByteArray(), UTF_8.name());
    assertEquals(TEST_XML, result);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

XMLStreamReader (javax.xml.stream.XMLStreamReader)1074 Test (org.junit.Test)486 InputStream (java.io.InputStream)451 ByteArrayInputStream (java.io.ByteArrayInputStream)379 ByteArrayOutputStream (java.io.ByteArrayOutputStream)334 Document (org.w3c.dom.Document)311 XMLStreamException (javax.xml.stream.XMLStreamException)288 ArrayList (java.util.ArrayList)270 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)242 XMLInputFactory (javax.xml.stream.XMLInputFactory)211 QName (javax.xml.namespace.QName)208 DOMSource (javax.xml.transform.dom.DOMSource)206 StringReader (java.io.StringReader)196 SecretKey (javax.crypto.SecretKey)188 StreamResult (javax.xml.transform.stream.StreamResult)183 DocumentBuilder (javax.xml.parsers.DocumentBuilder)178 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)160 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)155 IOException (java.io.IOException)144 Key (java.security.Key)103