Search in sources :

Example 41 with StreamSource

use of javax.xml.transform.stream.StreamSource in project spring-boot by spring-projects.

the class SampleWsApplicationTests method testSendingHolidayRequest.

@Test
public void testSendingHolidayRequest() {
    final String request = "<hr:HolidayRequest xmlns:hr=\"http://mycompany.com/hr/schemas\">" + "   <hr:Holiday>" + "      <hr:StartDate>2013-10-20</hr:StartDate>" + "      <hr:EndDate>2013-11-22</hr:EndDate>" + "   </hr:Holiday>" + "   <hr:Employee>" + "      <hr:Number>1</hr:Number>" + "      <hr:FirstName>John</hr:FirstName>" + "      <hr:LastName>Doe</hr:LastName>" + "   </hr:Employee>" + "</hr:HolidayRequest>";
    StreamSource source = new StreamSource(new StringReader(request));
    StreamResult result = new StreamResult(System.out);
    this.webServiceTemplate.sendSourceAndReceiveToResult(source, result);
    assertThat(this.output.toString()).contains("Booking holiday for");
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 42 with StreamSource

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

the class StaxResultTests method eventWriterSource.

@Test
public void eventWriterSource() throws Exception {
    StringWriter stringWriter = new StringWriter();
    XMLEventWriter eventWriter = inputFactory.createXMLEventWriter(stringWriter);
    Reader reader = new StringReader(XML);
    Source source = new StreamSource(reader);
    StaxResult result = new StaxResult(eventWriter);
    assertEquals("Invalid eventWriter returned", eventWriter, result.getXMLEventWriter());
    assertNull("StreamWriter returned", result.getXMLStreamWriter());
    transformer.transform(source, result);
    assertThat("Invalid result", stringWriter.toString(), isSimilarTo(XML));
}
Also used : StringWriter(java.io.StringWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 43 with StreamSource

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

the class JibxMarshaller method transformAndMarshal.

private void transformAndMarshal(Object graph, Result result) throws IOException {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        marshalOutputStream(graph, os);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        Transformer transformer = this.transformerFactory.newTransformer();
        transformer.transform(new StreamSource(is), result);
    } catch (TransformerException ex) {
        throw new MarshallingFailureException("Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) MarshallingFailureException(org.springframework.oxm.MarshallingFailureException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TransformerException(javax.xml.transform.TransformerException)

Example 44 with StreamSource

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

the class XStreamUnmarshallerTests method unmarshalStreamSourceInputStream.

@Test
public void unmarshalStreamSourceInputStream() throws Exception {
    StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
    Object flights = unmarshaller.unmarshal(source);
    testFlight(flights);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) Test(org.junit.Test)

Example 45 with StreamSource

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

the class Jaxb2MarshallerTests method unmarshalStreamSourceWithXmlOptions.

// SPR-10806
@Test
public void unmarshalStreamSourceWithXmlOptions() throws Exception {
    final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class);
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {

        @Override
        protected javax.xml.bind.Unmarshaller createUnmarshaller() {
            return unmarshaller;
        }
    };
    // 1. external-general-entities and dtd support disabled (default)
    marshaller.unmarshal(new StreamSource("1"));
    ArgumentCaptor<SAXSource> sourceCaptor = ArgumentCaptor.forClass(SAXSource.class);
    verify(unmarshaller).unmarshal(sourceCaptor.capture());
    SAXSource result = sourceCaptor.getValue();
    assertEquals(true, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(false, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities"));
    // 2. external-general-entities and dtd support enabled
    reset(unmarshaller);
    marshaller.setProcessExternalEntities(true);
    marshaller.setSupportDtd(true);
    marshaller.unmarshal(new StreamSource("1"));
    verify(unmarshaller).unmarshal(sourceCaptor.capture());
    result = sourceCaptor.getValue();
    assertEquals(false, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(true, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities"));
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) StreamSource(javax.xml.transform.stream.StreamSource) Test(org.junit.Test)

Aggregations

StreamSource (javax.xml.transform.stream.StreamSource)338 Source (javax.xml.transform.Source)115 StringReader (java.io.StringReader)101 StreamResult (javax.xml.transform.stream.StreamResult)85 Transformer (javax.xml.transform.Transformer)74 Test (org.junit.Test)73 InputStream (java.io.InputStream)68 TransformerFactory (javax.xml.transform.TransformerFactory)58 ByteArrayInputStream (java.io.ByteArrayInputStream)56 IOException (java.io.IOException)52 DOMSource (javax.xml.transform.dom.DOMSource)49 TransformerException (javax.xml.transform.TransformerException)48 StringWriter (java.io.StringWriter)45 SchemaFactory (javax.xml.validation.SchemaFactory)44 InputSource (org.xml.sax.InputSource)44 Schema (javax.xml.validation.Schema)43 SAXException (org.xml.sax.SAXException)42 SAXSource (javax.xml.transform.sax.SAXSource)33 Validator (javax.xml.validation.Validator)31 File (java.io.File)27