Search in sources :

Example 46 with StreamResult

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

the class XStreamMarshallerTests method annotatedMarshalStreamResultWriter.

@Test
public void annotatedMarshalStreamResultWriter() throws Exception {
    marshaller.setAnnotatedClasses(Flight.class);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    Flight flight = new Flight();
    flight.setFlightNumber(42);
    marshaller.marshal(flight, result);
    String expected = "<flight><number>42</number></flight>";
    assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(expected));
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Test(org.junit.Test)

Example 47 with StreamResult

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

the class XStreamMarshallerTests method omitFields.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void omitFields() throws Exception {
    Map omittedFieldsMap = Collections.singletonMap(Flight.class, "flightNumber");
    marshaller.setOmittedFields(omittedFieldsMap);
    Writer writer = new StringWriter();
    marshaller.marshal(flight, new StreamResult(writer));
    assertXpathNotExists("/flight/flightNumber", writer.toString());
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Map(java.util.Map) HashMap(java.util.HashMap) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) JsonWriter(com.thoughtworks.xstream.io.json.JsonWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer) StringWriter(java.io.StringWriter) Test(org.junit.Test)

Example 48 with StreamResult

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

the class XStreamMarshallerTests method fieldAliases.

@Test
public void fieldAliases() throws Exception {
    marshaller.setFieldAliases(Collections.singletonMap("org.springframework.oxm.xstream.Flight.flightNumber", "flightNo"));
    Writer writer = new StringWriter();
    marshaller.marshal(flight, new StreamResult(writer));
    String expected = "<flight><flightNo>42</flightNo></flight>";
    assertThat("Marshaller does not use aliases", writer.toString(), isSimilarTo(expected));
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) JsonWriter(com.thoughtworks.xstream.io.json.JsonWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer) StringWriter(java.io.StringWriter) Test(org.junit.Test)

Example 49 with StreamResult

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

the class JibxMarshallerTests method encodingAndStandalone.

@Test
public void encodingAndStandalone() throws Exception {
    marshaller.setEncoding("ISO-8859-1");
    marshaller.setStandalone(Boolean.TRUE);
    StringWriter writer = new StringWriter();
    marshaller.marshal(flights, new StreamResult(writer));
    assertTrue("Encoding and standalone not set", writer.toString().startsWith("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>"));
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Test(org.junit.Test)

Example 50 with StreamResult

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

the class JibxMarshallerTests method indentation.

@Test
public void indentation() throws Exception {
    marshaller.setIndent(4);
    StringWriter writer = new StringWriter();
    marshaller.marshal(flights, new StreamResult(writer));
    String expected = "<?xml version=\"1.0\"?>\n" + "<flights xmlns=\"http://samples.springframework.org/flight\">\n" + "    <flight>\n" + "        <number>42</number>\n" + "    </flight>\n" + "</flights>";
    assertThat(writer.toString(), isSimilarTo(expected).ignoreWhitespace());
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Test(org.junit.Test)

Aggregations

StreamResult (javax.xml.transform.stream.StreamResult)448 Transformer (javax.xml.transform.Transformer)267 DOMSource (javax.xml.transform.dom.DOMSource)234 StringWriter (java.io.StringWriter)206 TransformerFactory (javax.xml.transform.TransformerFactory)138 TransformerException (javax.xml.transform.TransformerException)125 Document (org.w3c.dom.Document)103 IOException (java.io.IOException)94 StreamSource (javax.xml.transform.stream.StreamSource)88 Source (javax.xml.transform.Source)74 Test (org.junit.Test)73 DocumentBuilder (javax.xml.parsers.DocumentBuilder)65 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 Element (org.w3c.dom.Element)59 File (java.io.File)58 Result (javax.xml.transform.Result)57 StringReader (java.io.StringReader)56 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)50 ByteArrayInputStream (java.io.ByteArrayInputStream)44