Search in sources :

Example 6 with StAXSource

use of javax.xml.transform.stax.StAXSource in project camel by apache.

the class XmlConverterTest method testToStreamSourceByStAXSource.

public void testToStreamSourceByStAXSource() throws Exception {
    XmlConverter conv = new XmlConverter();
    StAXSource source = conv.toStAXSource("<foo>bar</foo>", null);
    StreamSource out = conv.toStreamSource(source, null);
    assertNotSame(source, out);
    assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource)

Example 7 with StAXSource

use of javax.xml.transform.stax.StAXSource in project camel by apache.

the class XmlConverterTest method testToStAXSourceByInputStream.

public void testToStAXSourceByInputStream() throws Exception {
    XmlConverter conv = new XmlConverter();
    InputStream is = context.getTypeConverter().convertTo(InputStream.class, "<foo>bar</foo>");
    StAXSource out = conv.toStAXSource(is, null);
    assertNotNull(out);
    assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
Also used : InputStream(java.io.InputStream) StAXSource(javax.xml.transform.stax.StAXSource)

Example 8 with StAXSource

use of javax.xml.transform.stax.StAXSource in project camel by apache.

the class CxfPayloadConverter method convertTo.

@SuppressWarnings("unchecked")
@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    // CxfPayloads from other types
    if (type.isAssignableFrom(CxfPayload.class)) {
        try {
            if (!value.getClass().isArray()) {
                Source src = null;
                // directly
                if (value instanceof InputStream) {
                    src = new StreamSource((InputStream) value);
                } else if (value instanceof Reader) {
                    src = new StreamSource((Reader) value);
                } else if (value instanceof String) {
                    src = new StreamSource(new StringReader((String) value));
                } else if (value instanceof Node) {
                    src = new DOMSource((Node) value);
                } else if (value instanceof Source) {
                    src = (Source) value;
                }
                if (src == null) {
                    // assuming staxsource is preferred, otherwise use the
                    // one preferred
                    TypeConverter tc = registry.lookup(javax.xml.transform.stax.StAXSource.class, value.getClass());
                    if (tc == null) {
                        tc = registry.lookup(Source.class, value.getClass());
                    }
                    if (tc != null) {
                        src = tc.convertTo(Source.class, exchange, value);
                    }
                }
                if (src != null) {
                    return (T) sourceToCxfPayload(src, exchange);
                }
            }
            TypeConverter tc = registry.lookup(NodeList.class, value.getClass());
            if (tc != null) {
                NodeList nodeList = tc.convertTo(NodeList.class, exchange, value);
                return (T) nodeListToCxfPayload(nodeList, exchange);
            }
            tc = registry.lookup(Document.class, value.getClass());
            if (tc != null) {
                Document document = tc.convertTo(Document.class, exchange, value);
                return (T) documentToCxfPayload(document, exchange);
            }
            // maybe we can convert via an InputStream
            CxfPayload<?> p;
            p = convertVia(InputStream.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
            // String is the converter of last resort
            p = convertVia(String.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
        } catch (RuntimeCamelException e) {
        // the internal conversion to XML can throw an exception if the content is not XML
        // ignore this and return Void.TYPE to indicate that we cannot convert this
        }
        // no we could not do it currently
        return (T) Void.TYPE;
    }
    // Convert a CxfPayload into something else
    if (CxfPayload.class.isAssignableFrom(value.getClass())) {
        CxfPayload<?> payload = (CxfPayload<?>) value;
        int size = payload.getBodySources().size();
        if (size == 1) {
            if (type.isAssignableFrom(Document.class)) {
                Source s = payload.getBodySources().get(0);
                Document d;
                try {
                    d = StaxUtils.read(s);
                } catch (XMLStreamException e) {
                    throw new RuntimeException(e);
                }
                return type.cast(d);
            }
            // CAMEL-8410 Just make sure we get the Source object directly from the payload body source
            Source s = payload.getBodySources().get(0);
            if (type.isInstance(s)) {
                return type.cast(s);
            }
            TypeConverter tc = registry.lookup(type, XMLStreamReader.class);
            if (tc != null && (s instanceof StaxSource || s instanceof StAXSource)) {
                XMLStreamReader r = (s instanceof StAXSource) ? ((StAXSource) s).getXMLStreamReader() : ((StaxSource) s).getXMLStreamReader();
                if (payload.getNsMap() != null) {
                    r = new DelegatingXMLStreamReader(r, payload.getNsMap());
                }
                return tc.convertTo(type, exchange, r);
            }
            tc = registry.lookup(type, Source.class);
            if (tc != null) {
                XMLStreamReader r = null;
                if (payload.getNsMap() != null) {
                    if (s instanceof StaxSource) {
                        r = ((StaxSource) s).getXMLStreamReader();
                    } else if (s instanceof StAXSource) {
                        r = ((StAXSource) s).getXMLStreamReader();
                    }
                    if (r != null) {
                        s = new StAXSource(new DelegatingXMLStreamReader(r, payload.getNsMap()));
                    }
                }
                return tc.convertTo(type, exchange, s);
            }
        }
        TypeConverter tc = registry.lookup(type, NodeList.class);
        if (tc != null) {
            Object result = tc.convertTo(type, exchange, cxfPayloadToNodeList((CxfPayload<?>) value, exchange));
            if (result == null) {
                // no we could not do it currently, and we just abort the convert here
                return (T) Void.TYPE;
            } else {
                return (T) result;
            }
        }
        // we cannot convert a node list, so we try the first item from the
        // node list
        tc = registry.lookup(type, Node.class);
        if (tc != null) {
            NodeList nodeList = cxfPayloadToNodeList((CxfPayload<?>) value, exchange);
            if (nodeList.getLength() > 0) {
                return tc.convertTo(type, exchange, nodeList.item(0));
            } else {
                // no we could not do it currently
                return (T) Void.TYPE;
            }
        } else {
            if (size == 0) {
                // empty size so we cannot convert
                return (T) Void.TYPE;
            }
        }
    }
    return null;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) CxfPayload(org.apache.camel.component.cxf.CxfPayload) Node(org.w3c.dom.Node) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StaxSource(org.apache.cxf.staxutils.StaxSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) StringReader(java.io.StringReader) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) StAXSource(javax.xml.transform.stax.StAXSource) TypeConverter(org.apache.camel.TypeConverter) XMLStreamException(javax.xml.stream.XMLStreamException) StaxSource(org.apache.cxf.staxutils.StaxSource) RuntimeCamelException(org.apache.camel.RuntimeCamelException) FallbackConverter(org.apache.camel.FallbackConverter)

Example 9 with StAXSource

use of javax.xml.transform.stax.StAXSource in project camel by apache.

the class CxfPayloadConverterTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    File file = new File("src/test/resources/org/apache/camel/component/cxf/converter/test.xml");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    document = documentBuilder.parse(file);
    document.getDocumentElement().normalize();
    List<Source> body = new ArrayList<Source>();
    body.add(new DOMSource(document.getDocumentElement()));
    List<Source> staxbody = new ArrayList<Source>();
    staxbody.add(new StAXSource(StaxUtils.createXMLStreamReader(new FileInputStream(file), "utf-8")));
    payload = new CxfPayload<String[]>(new ArrayList<String[]>(), body, null);
    emptyPayload = new CxfPayload<String[]>(new ArrayList<String[]>(), new ArrayList<Source>(), null);
    staxpayload = new CxfPayload<String[]>(new ArrayList<String[]>(), staxbody, null);
    inputStream = new FileInputStream(file);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) StAXSource(javax.xml.transform.stax.StAXSource) File(java.io.File) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) FileInputStream(java.io.FileInputStream) Before(org.junit.Before)

Example 10 with StAXSource

use of javax.xml.transform.stax.StAXSource in project hale by halestudio.

the class JsonXML method toJson.

public static void toJson(Reader xmlReader, Writer jsonWriter) throws XMLStreamException, FactoryConfigurationError, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError {
    /*
		 * If we want to insert JSON array boundaries for multiple elements, we
		 * need to set the <code>autoArray</code> property. If our XML source
		 * was decorated with <code>&lt;?xml-multiple?&gt;</code> processing
		 * instructions, we'd set the <code>multiplePI</code> property instead.
		 * With the <code>autoPrimitive</code> property set, element text gets
		 * automatically converted to JSON primitives (number, boolean, null).
		 */
    JsonXMLConfig config = new JsonXMLConfigBuilder().namespaceDeclarations(true).autoArray(true).autoPrimitive(true).prettyPrint(false).build();
    /*
		 * Create source (XML).
		 */
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(xmlReader);
    Source source = new StAXSource(reader);
    /*
		 * Create result (JSON).
		 */
    // create stream factory manually due to class loading issues
    XMLStreamWriter writer = new JsonXMLOutputFactory(config, new JsonStreamFactoryImpl()).createXMLStreamWriter(jsonWriter);
    Result result = new StAXResult(writer);
    /*
		 * Copy source to result via "identity transform".
		 */
    TransformerFactory.newInstance().newTransformer().transform(source, result);
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) JsonXMLConfigBuilder(de.odysseus.staxon.json.JsonXMLConfigBuilder) JsonXMLConfig(de.odysseus.staxon.json.JsonXMLConfig) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PrettyXMLStreamWriter(de.odysseus.staxon.xml.util.PrettyXMLStreamWriter) JsonXMLOutputFactory(de.odysseus.staxon.json.JsonXMLOutputFactory) JsonStreamFactoryImpl(de.odysseus.staxon.json.stream.impl.JsonStreamFactoryImpl) StAXSource(javax.xml.transform.stax.StAXSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) Result(javax.xml.transform.Result) StAXResult(javax.xml.transform.stax.StAXResult)

Aggregations

StAXSource (javax.xml.transform.stax.StAXSource)66 XMLStreamReader (javax.xml.stream.XMLStreamReader)27 XMLStreamException (javax.xml.stream.XMLStreamException)24 StringReader (java.io.StringReader)19 XMLInputFactory (javax.xml.stream.XMLInputFactory)19 XMLEventReader (javax.xml.stream.XMLEventReader)16 StreamSource (javax.xml.transform.stream.StreamSource)15 DOMSource (javax.xml.transform.dom.DOMSource)14 Source (javax.xml.transform.Source)13 SAXSource (javax.xml.transform.sax.SAXSource)12 Test (org.junit.Test)12 StreamResult (javax.xml.transform.stream.StreamResult)10 InputStream (java.io.InputStream)9 TransformerException (javax.xml.transform.TransformerException)8 IOException (java.io.IOException)7 Transformer (javax.xml.transform.Transformer)7 Test (org.junit.jupiter.api.Test)7 Document (org.w3c.dom.Document)7 InputSource (org.xml.sax.InputSource)7 StringWriter (java.io.StringWriter)6