Search in sources :

Example 46 with StreamSource

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

the class AbstractUnmarshallerTests method unmarshalStreamSourceReader.

@Test
public void unmarshalStreamSourceReader() throws Exception {
    StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
    Object flights = unmarshaller.unmarshal(source);
    testFlights(flights);
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 47 with StreamSource

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

the class CastorUnmarshallerTests method setBothTargetClassesAndMapping.

@Test
public void setBothTargetClassesAndMapping() throws IOException {
    CastorMarshaller unmarshaller = new CastorMarshaller();
    unmarshaller.setMappingLocation(new ClassPathResource("order-mapping.xml", CastorMarshaller.class));
    unmarshaller.setTargetClasses(new Class[] { Order.class });
    unmarshaller.afterPropertiesSet();
    String xml = "<order>" + "<order-item id=\"1\" quantity=\"15\"/>" + "<order-item id=\"3\" quantity=\"20\"/>" + "</order>";
    Order order = (Order) unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
    assertEquals("Invalid amount of items", 2, order.getOrderItemCount());
    OrderItem item = order.getOrderItem(0);
    assertEquals("Invalid items", "1", item.getId());
    assertThat("Invalid items", item.getQuantity(), equalTo(15));
    item = order.getOrderItem(1);
    assertEquals("Invalid items", "3", item.getId());
    assertThat("Invalid items", item.getQuantity(), equalTo(20));
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 48 with StreamSource

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

the class CastorUnmarshallerTests method unmarshalStreamSourceWithXmlOptions.

@Test
public void unmarshalStreamSourceWithXmlOptions() throws Exception {
    final AtomicReference<XMLReader> result = new AtomicReference<>();
    CastorMarshaller marshaller = new CastorMarshaller() {

        @Override
        protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) {
            result.set(xmlReader);
            return null;
        }
    };
    // 1. external-general-entities and dtd support disabled (default)
    marshaller.unmarshal(new StreamSource("1"));
    assertNotNull(result.get());
    assertEquals(true, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(false, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
    // 2. external-general-entities and dtd support enabled
    result.set(null);
    marshaller.setSupportDtd(true);
    marshaller.setProcessExternalEntities(true);
    marshaller.unmarshal(new StreamSource("1"));
    assertNotNull(result.get());
    assertEquals(false, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(true, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
}
Also used : InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) AtomicReference(java.util.concurrent.atomic.AtomicReference) XMLReader(org.xml.sax.XMLReader) Test(org.junit.Test)

Example 49 with StreamSource

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

the class XStreamMarshallerTests method jettisonDriver.

@Test
public void jettisonDriver() throws Exception {
    marshaller.setStreamDriver(new JettisonMappedXmlDriver());
    Writer writer = new StringWriter();
    marshaller.marshal(flight, new StreamResult(writer));
    assertEquals("Invalid result", "{\"flight\":{\"flightNumber\":42}}", writer.toString());
    Object o = marshaller.unmarshal(new StreamSource(new StringReader(writer.toString())));
    assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
    Flight unflight = (Flight) o;
    assertNotNull("Flight is null", unflight);
    assertEquals("Number is invalid", 42L, unflight.getFlightNumber());
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) JettisonMappedXmlDriver(com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver) 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 50 with StreamSource

use of javax.xml.transform.stream.StreamSource in project nokogiri by sparklemotion.

the class XmlRelaxng method getSchema.

private Schema getSchema(Source source, ThreadContext context) {
    InputStream is;
    VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
    if (source instanceof StreamSource) {
        StreamSource ss = (StreamSource) source;
        is = ss.getInputStream();
    } else {
        //if (this.source instanceof DOMSource)
        DOMSource ds = (DOMSource) source;
        StringWriter xmlAsWriter = new StringWriter();
        StreamResult result = new StreamResult(xmlAsWriter);
        try {
            TransformerFactory.newInstance().newTransformer().transform(ds, result);
        } catch (TransformerConfigurationException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        } catch (TransformerException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
        try {
            is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
    }
    try {
        return factory.compileSchema(is);
    } catch (VerifierConfigurationException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (SAXException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (IOException ex) {
        throw context.getRuntime().newIOError(ex.getMessage());
    }
}
Also used : VerifierFactory(org.iso_relax.verifier.VerifierFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) TransformerException(javax.xml.transform.TransformerException)

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