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