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));
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations