Search in sources :

Example 86 with SAXSource

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

the class AbstractUnmarshallerTests method unmarshalSAXSource.

@Test
public void unmarshalSAXSource() throws Exception {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING)));
    Object flights = unmarshaller.unmarshal(source);
    testFlights(flights);
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader) Test(org.junit.Test)

Example 87 with SAXSource

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

the class SourceHttpMessageConverterTests method readSAXSourceExternal.

@Test
public void readSAXSourceExternal() throws Exception {
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
    converter.setSupportDtd(true);
    SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
    InputSource inputSource = result.getInputSource();
    XMLReader reader = result.getXMLReader();
    reader.setContentHandler(new DefaultHandler() {

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            String s = new String(ch, start, length);
            assertNotEquals("Invalid result", "Foo Bar", s);
        }
    });
    reader.parse(inputSource);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) MediaType(org.springframework.http.MediaType) XMLReader(org.xml.sax.XMLReader) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) Test(org.junit.Test)

Example 88 with SAXSource

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

the class SourceHttpMessageConverterTests method readSAXSource.

@Test
public void readSAXSource() throws Exception {
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
    SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
    InputSource inputSource = result.getInputSource();
    String s = FileCopyUtils.copyToString(new InputStreamReader(inputSource.getByteStream()));
    assertThat("Invalid result", s, isSimilarTo(BODY));
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) InputStreamReader(java.io.InputStreamReader) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 89 with SAXSource

use of javax.xml.transform.sax.SAXSource in project opennms by OpenNMS.

the class XmlTest method validateJaxbXmlAgainstSchema.

@Test
public void validateJaxbXmlAgainstSchema() throws Exception {
    final String schemaFile = getSchemaFile();
    if (schemaFile == null) {
        LOG.warn("Skipping validation.");
        return;
    }
    LOG.debug("Validating against XSD: {}", schemaFile);
    javax.xml.bind.Unmarshaller unmarshaller = JaxbUtils.getUnmarshallerFor(getSampleClass(), null, true);
    final SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    final Schema schema = factory.newSchema(new StreamSource(schemaFile));
    unmarshaller.setSchema(schema);
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        @Override
        public boolean handleEvent(final ValidationEvent event) {
            LOG.warn("Received validation event: {}", event, event.getLinkedException());
            return false;
        }
    });
    try {
        final InputSource inputSource = new InputSource(getSampleXmlInputStream());
        final XMLFilter filter = JaxbUtils.getXMLFilterForClass(getSampleClass());
        final SAXSource source = new SAXSource(filter, inputSource);
        @SuppressWarnings("unchecked") T obj = (T) unmarshaller.unmarshal(source);
        assertNotNull(obj);
    } finally {
        unmarshaller.setSchema(null);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) InputSource(org.xml.sax.InputSource) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) XMLFilter(org.xml.sax.XMLFilter) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) Test(org.junit.Test)

Example 90 with SAXSource

use of javax.xml.transform.sax.SAXSource in project opennms by OpenNMS.

the class JaxbUtils method unmarshal.

public static <T> T unmarshal(final Class<T> clazz, final InputSource inputSource, final JAXBContext jaxbContext, final boolean validate) {
    final Unmarshaller um = getUnmarshallerFor(clazz, jaxbContext, validate);
    LOG.trace("unmarshalling class {} from input source {} with unmarshaller {}", clazz.getSimpleName(), inputSource, um);
    try {
        final XMLFilter filter = getXMLFilterForClass(clazz);
        final SAXSource source = new SAXSource(filter, inputSource);
        um.setEventHandler(new LoggingValidationEventHandler());
        final JAXBElement<T> element = um.unmarshal(source, clazz);
        return element.getValue();
    } catch (final SAXException e) {
        throw EXCEPTION_TRANSLATOR.translate("creating an XML reader object", e);
    } catch (final JAXBException e) {
        throw EXCEPTION_TRANSLATOR.translate("unmarshalling an object (" + clazz.getSimpleName() + ")", e);
    }
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) JAXBException(javax.xml.bind.JAXBException) XMLFilter(org.xml.sax.XMLFilter) Unmarshaller(javax.xml.bind.Unmarshaller) SAXException(org.xml.sax.SAXException)

Aggregations

SAXSource (javax.xml.transform.sax.SAXSource)111 InputSource (org.xml.sax.InputSource)81 XMLReader (org.xml.sax.XMLReader)38 Source (javax.xml.transform.Source)28 StreamSource (javax.xml.transform.stream.StreamSource)28 DOMSource (javax.xml.transform.dom.DOMSource)27 SAXException (org.xml.sax.SAXException)26 TransformerException (javax.xml.transform.TransformerException)24 SAXParserFactory (javax.xml.parsers.SAXParserFactory)20 Unmarshaller (javax.xml.bind.Unmarshaller)17 SAXParser (javax.xml.parsers.SAXParser)17 Transformer (javax.xml.transform.Transformer)17 StreamResult (javax.xml.transform.stream.StreamResult)16 Test (org.junit.Test)16 StringReader (java.io.StringReader)15 IOException (java.io.IOException)14 JAXBContext (javax.xml.bind.JAXBContext)14 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 ValidationEvent (javax.xml.bind.ValidationEvent)10