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