Search in sources :

Example 36 with Source

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

the class StaxUtilsTests method isStaxSource.

@Test
public void isStaxSource() throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    String expected = "<element/>";
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
    Source source = StaxUtils.createCustomStaxSource(streamReader);
    assertTrue("Not a StAX Source", StaxUtils.isStaxSource(source));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.Test)

Example 37 with Source

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

the class MarshallingMessageConverter method convertFromInternal.

@Override
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
    Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required");
    try {
        Source source = getSource(message.getPayload());
        Object result = this.unmarshaller.unmarshal(source);
        if (!targetClass.isInstance(result)) {
            throw new TypeMismatchException(result, targetClass);
        }
        return result;
    } catch (Exception ex) {
        throw new MessageConversionException(message, "Could not unmarshal XML: " + ex.getMessage(), ex);
    }
}
Also used : TypeMismatchException(org.springframework.beans.TypeMismatchException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TypeMismatchException(org.springframework.beans.TypeMismatchException)

Example 38 with Source

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

the class Jaxb2Marshaller method loadSchema.

// on JDK 9
@SuppressWarnings("deprecation")
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
    if (logger.isDebugEnabled()) {
        logger.debug("Setting validation schema to " + StringUtils.arrayToCommaDelimitedString(this.schemaResources));
    }
    Assert.notEmpty(resources, "No resources given");
    Assert.hasLength(schemaLanguage, "No schema language provided");
    Source[] schemaSources = new Source[resources.length];
    XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
    xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    for (int i = 0; i < resources.length; i++) {
        Assert.notNull(resources[i], "Resource is null");
        Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
        InputSource inputSource = SaxResourceUtils.createInputSource(resources[i]);
        schemaSources[i] = new SAXSource(xmlReader, inputSource);
    }
    SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
    if (this.schemaResourceResolver != null) {
        schemaFactory.setResourceResolver(this.schemaResourceResolver);
    }
    return schemaFactory.newSchema(schemaSources);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) DataSource(javax.activation.DataSource) XMLReader(org.xml.sax.XMLReader)

Example 39 with Source

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

the class XStreamUnmarshallerTests method unmarshalStaxSourceXmlStreamReader.

@Test
public void unmarshalStaxSourceXmlStreamReader() throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
    Source source = StaxUtils.createStaxSource(streamReader);
    Object flights = unmarshaller.unmarshal(source);
    testFlight(flights);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 40 with Source

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

the class AbstractUnmarshallerTests method unmarshalPartialStaxSourceXmlStreamReader.

@Test
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
    // skip to flights
    streamReader.nextTag();
    assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flights"), streamReader.getName());
    // skip to flight
    streamReader.nextTag();
    assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flight"), streamReader.getName());
    Source source = StaxUtils.createStaxSource(streamReader);
    Object flight = unmarshaller.unmarshal(source);
    testFlight(flight);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.Test)

Aggregations

Source (javax.xml.transform.Source)238 StreamSource (javax.xml.transform.stream.StreamSource)161 DOMSource (javax.xml.transform.dom.DOMSource)108 Transformer (javax.xml.transform.Transformer)76 StreamResult (javax.xml.transform.stream.StreamResult)74 InputSource (org.xml.sax.InputSource)67 SAXSource (javax.xml.transform.sax.SAXSource)56 StringReader (java.io.StringReader)52 IOException (java.io.IOException)46 TransformerException (javax.xml.transform.TransformerException)45 Result (javax.xml.transform.Result)42 TransformerFactory (javax.xml.transform.TransformerFactory)41 Test (org.junit.Test)39 StringWriter (java.io.StringWriter)35 InputStream (java.io.InputStream)32 SAXException (org.xml.sax.SAXException)32 Schema (javax.xml.validation.Schema)29 Validator (javax.xml.validation.Validator)29 SchemaFactory (javax.xml.validation.SchemaFactory)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26