Search in sources :

Example 51 with Result

use of javax.xml.transform.Result in project spring-framework by spring-projects.

the class Jaxb2MarshallerTests method marshalInvalidClass.

@Test(expected = XmlMappingException.class)
public void marshalInvalidClass() throws Exception {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(FlightType.class);
    marshaller.afterPropertiesSet();
    Result result = new StreamResult(new StringWriter());
    Flights flights = new Flights();
    marshaller.marshal(flights, result);
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) StringWriter(java.io.StringWriter) Flights(org.springframework.oxm.jaxb.test.Flights) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) Test(org.junit.Test)

Example 52 with Result

use of javax.xml.transform.Result in project spring-framework by spring-projects.

the class XStreamMarshallerTests method marshalStaxResultXMLStreamWriter.

@Test
public void marshalStaxResultXMLStreamWriter() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    StringWriter writer = new StringWriter();
    XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
    Result result = StaxUtils.createStaxResult(streamWriter);
    marshaller.marshal(flight, result);
    assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) DOMResult(javax.xml.transform.dom.DOMResult) Test(org.junit.Test)

Example 53 with Result

use of javax.xml.transform.Result in project spring-framework by spring-projects.

the class MarshallingMessageConverter method convertToInternal.

@Override
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
    Assert.notNull(this.marshaller, "Property 'marshaller' is required");
    try {
        if (byte[].class == getSerializedPayloadClass()) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            Result result = new StreamResult(out);
            this.marshaller.marshal(payload, result);
            payload = out.toByteArray();
        } else {
            Writer writer = new StringWriter();
            Result result = new StreamResult(writer);
            this.marshaller.marshal(payload, result);
            payload = writer.toString();
        }
    } catch (Exception ex) {
        throw new MessageConversionException("Could not marshal XML: " + ex.getMessage(), ex);
    }
    return payload;
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) StringWriter(java.io.StringWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StringWriter(java.io.StringWriter) Writer(java.io.Writer) TypeMismatchException(org.springframework.beans.TypeMismatchException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 54 with Result

use of javax.xml.transform.Result in project spring-framework by spring-projects.

the class AbstractMarshallerTests method marshalStaxResultEventWriter.

@Test
public void marshalStaxResultEventWriter() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    StringWriter writer = new StringWriter();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
    Result result = StaxUtils.createStaxResult(eventWriter);
    marshaller.marshal(flights, result);
    assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) StAXResult(javax.xml.transform.stax.StAXResult) DOMResult(javax.xml.transform.dom.DOMResult) Test(org.junit.Test)

Example 55 with Result

use of javax.xml.transform.Result in project cubrid-manager by CUBRID.

the class XMLMemento method save.

/**
	 * Save this Memento to a stream.
	 * 
	 * @param os OutputStream the output stream
	 * @throws IOException if anything goes wrong
	 */
public void save(OutputStream os) throws IOException {
    Result result = new StreamResult(os);
    Source source = new DOMSource(document);
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
        transformer.transform(source, result);
    } catch (Exception e) {
        throw (IOException) (new IOException().initCause(e));
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Aggregations

Result (javax.xml.transform.Result)80 StreamResult (javax.xml.transform.stream.StreamResult)72 Source (javax.xml.transform.Source)52 Transformer (javax.xml.transform.Transformer)52 DOMSource (javax.xml.transform.dom.DOMSource)42 TransformerFactory (javax.xml.transform.TransformerFactory)33 StringWriter (java.io.StringWriter)24 TransformerException (javax.xml.transform.TransformerException)21 IOException (java.io.IOException)20 StreamSource (javax.xml.transform.stream.StreamSource)20 SAXResult (javax.xml.transform.sax.SAXResult)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)15 Document (org.w3c.dom.Document)14 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)11 DOMResult (javax.xml.transform.dom.DOMResult)11 InputSource (org.xml.sax.InputSource)11 SAXException (org.xml.sax.SAXException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9