下面列出了怎么用javax.xml.transform.stax.StAXSource的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
InputStream body = inputMessage.getBody();
if (DOMSource.class == clazz) {
return (T) readDOMSource(body, inputMessage);
}
else if (SAXSource.class == clazz) {
return (T) readSAXSource(body, inputMessage);
}
else if (StAXSource.class == clazz) {
return (T) readStAXSource(body, inputMessage);
}
else if (StreamSource.class == clazz || Source.class == clazz) {
return (T) readStreamSource(body);
}
else {
throw new HttpMessageNotReadableException("Could not read class [" + clazz +
"]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.", inputMessage);
}
}
@Test
public void readStAXSourceExternal() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
converter.setSupportDtd(true);
StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
XMLStreamReader streamReader = result.getXMLStreamReader();
assertTrue(streamReader.hasNext());
streamReader.next();
streamReader.next();
String s = streamReader.getLocalName();
assertEquals("root", s);
try {
s = streamReader.getElementText();
assertNotEquals("Foo Bar", s);
}
catch (XMLStreamException ex) {
// Some parsers raise a parse exception
}
streamReader.close();
}
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
InputStream body = inputMessage.getBody();
if (DOMSource.class == clazz) {
return (T) readDOMSource(body, inputMessage);
}
else if (SAXSource.class == clazz) {
return (T) readSAXSource(body, inputMessage);
}
else if (StAXSource.class == clazz) {
return (T) readStAXSource(body, inputMessage);
}
else if (StreamSource.class == clazz || Source.class == clazz) {
return (T) readStreamSource(body);
}
else {
throw new HttpMessageNotReadableException("Could not read class [" + clazz +
"]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.", inputMessage);
}
}
@Test
public void readStAXSourceExternal() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
converter.setSupportDtd(true);
StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
XMLStreamReader streamReader = result.getXMLStreamReader();
assertTrue(streamReader.hasNext());
streamReader.next();
streamReader.next();
String s = streamReader.getLocalName();
assertEquals("root", s);
try {
s = streamReader.getElementText();
assertNotEquals("Foo Bar", s);
}
catch (XMLStreamException ex) {
// Some parsers raise a parse exception
}
streamReader.close();
}
/**
* Unmarshal an object from the given stream, DOM or other sources.
* Together with the {@linkplain #unmarshal(Source, Map) Unmarshal Global Root Element} variant,
* this is the most flexible unmarshalling method provided in this {@code XML} class.
* The source is specified by the {@code input} argument implementation, for example
* {@link javax.xml.transform.stream.StreamSource} for reading from a file or input stream.
* The optional {@code properties} map can contain any key documented in this {@code XML} class,
* together with the keys documented in the <cite>supported properties</cite> section of the the
* {@link Unmarshaller} class.
*
* @param <T> compile-time value of the {@code declaredType} argument.
* @param input the file from which to read a XML representation.
* @param declaredType the JAXB mapped class of the object to unmarshal.
* @param properties an optional map of properties to give to the unmarshaller, or {@code null} if none.
* @return the object unmarshalled from the given input, wrapped in a JAXB element.
* @throws JAXBException if a property has an illegal value, or if an error occurred during the unmarshalling.
*
* @since 0.8
*/
public static <T> JAXBElement<T> unmarshal(final Source input, final Class<T> declaredType, final Map<String,?> properties)
throws JAXBException
{
ensureNonNull("input", input);
ensureNonNull("declaredType", declaredType);
final MarshallerPool pool = getPool();
final Unmarshaller unmarshaller = pool.acquireUnmarshaller(properties);
final JAXBElement<T> element;
if (input instanceof StAXSource) { // Same workaround than the one documented in above method.
@Workaround(library = "JDK", version = "1.8")
final XMLStreamReader reader = ((StAXSource) input).getXMLStreamReader();
if (reader != null) {
element = unmarshaller.unmarshal(reader, declaredType);
} else {
element = unmarshaller.unmarshal(((StAXSource) input).getXMLEventReader(), declaredType);
}
} else {
element = unmarshaller.unmarshal(input, declaredType);
}
pool.recycle(unmarshaller);
return element;
}
@Test
void testTransactionElementWithXmlns() throws Exception {
EDIInputFactory ediFactory = EDIInputFactory.newFactory();
ediFactory.setProperty(EDIInputFactory.XML_DECLARE_TRANSACTION_XMLNS, Boolean.TRUE);
InputStream stream = getClass().getResourceAsStream("/x12/extraDelimiter997.edi");
ediReader = ediFactory.createEDIStreamReader(stream);
XMLStreamReader xmlReader = ediFactory.createXMLStreamReader(ediReader);
xmlReader.next(); // Per StAXSource JavaDoc, put in START_DOCUMENT state
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StringWriter result = new StringWriter();
transformer.transform(new StAXSource(xmlReader), new StreamResult(result));
String resultString = result.toString();
Diff d = DiffBuilder.compare(Input.fromFile("src/test/resources/x12/extraDelimiter997-transaction-xmlns.xml"))
.withTest(resultString).build();
assertTrue(!d.hasDifferences(), () -> "XML unexpectedly different:\n" + d.toString(new DefaultComparisonFormatter()));
}
@Test
void testXmlIOEquivalence() throws Exception {
XMLStreamReader xmlReader = getXmlReader("/x12/extraDelimiter997.edi");
xmlReader.next(); // Per StAXSource JavaDoc, put in START_DOCUMENT state
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StringWriter result = new StringWriter();
transformer.transform(new StAXSource(xmlReader), new StreamResult(result));
String resultString = result.toString();
EDIOutputFactory ediOutFactory = EDIOutputFactory.newFactory();
ediOutFactory.setProperty(EDIOutputFactory.PRETTY_PRINT, "true");
ByteArrayOutputStream resultEDI = new ByteArrayOutputStream();
EDIStreamWriter ediWriter = ediOutFactory.createEDIStreamWriter(resultEDI);
XMLStreamWriter xmlWriter = new StaEDIXMLStreamWriter(ediWriter);
transformer.transform(new StreamSource(new StringReader(resultString)), new StAXResult(xmlWriter));
String expectedEDI = String.join("\n", Files.readAllLines(Paths.get("src/test/resources/x12/extraDelimiter997.edi")));
assertEquals(expectedEDI, new String(resultEDI.toByteArray()).trim());
}
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
InputStream body = inputMessage.getBody();
if (DOMSource.class == clazz) {
return (T) readDOMSource(body);
}
else if (SAXSource.class == clazz) {
return (T) readSAXSource(body);
}
else if (StAXSource.class == clazz) {
return (T) readStAXSource(body);
}
else if (StreamSource.class == clazz || Source.class == clazz) {
return (T) readStreamSource(body);
}
else {
throw new HttpMessageConversionException("Could not read class [" + clazz +
"]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.");
}
}
/**
* Returns a Source for reading the XML value designated by this SQLXML
* instance. <p>
*
* @param sourceClass The class of the source, or null. If null, then a
* DOMSource is returned.
* @return a Source for reading the XML value.
* @throws SQLException if there is an error processing the XML value
* or if the given <tt>sourceClass</tt> is not supported.
*/
protected <T extends Source>T getSourceImpl(
Class<T> sourceClass) throws SQLException {
if (JAXBSource.class.isAssignableFrom(sourceClass)) {
// Must go first presently, since JAXBSource extends SAXSource
// (purely as an implementation detail) and it's not possible
// to instantiate a valid JAXBSource with a Zero-Args
// constructor(or any subclass thereof, due to the finality of
// its private marshaller and context object attributes)
// FALL THROUGH... will throw an exception
} else if (StreamSource.class.isAssignableFrom(sourceClass)) {
return createStreamSource(sourceClass);
} else if ((sourceClass == null)
|| DOMSource.class.isAssignableFrom(sourceClass)) {
return createDOMSource(sourceClass);
} else if (SAXSource.class.isAssignableFrom(sourceClass)) {
return createSAXSource(sourceClass);
} else if (StAXSource.class.isAssignableFrom(sourceClass)) {
return createStAXSource(sourceClass);
}
throw JDBCUtil.invalidArgument("sourceClass: " + sourceClass);
}
@DataProvider(name = "data_ValidatorA")
public Object[][] getDataValidator() {
DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
SAXSource ss = new SAXSource(new InputSource(xml_val_test));
ss.setSystemId(xml_val_test_id);
StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
StreamSource source = new StreamSource(new File(xml_val_test));
return new Object[][]{
// use catalog
{true, false, true, ds, null, null, xml_catalog, null},
{false, true, true, ds, null, null, null, xml_catalog},
{true, false, true, ss, null, null, xml_catalog, null},
{false, true, true, ss, null, null, null, xml_catalog},
{true, false, true, stax, null, null, xml_catalog, xml_catalog},
{false, true, true, stax1, null, null, xml_catalog, xml_catalog},
{true, false, true, source, null, null, xml_catalog, null},
{false, true, true, source, null, null, null, xml_catalog},
};
}
@DataProvider(name = "data_ValidatorC")
public Object[][] getDataValidator() {
DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
SAXSource ss = new SAXSource(new InputSource(xml_val_test));
ss.setSystemId(xml_val_test_id);
StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
StreamSource source = new StreamSource(new File(xml_val_test));
return new Object[][]{
// use catalog
{false, false, true, ds, null, null, xml_bogus_catalog, null},
{false, false, true, ds, null, null, null, xml_bogus_catalog},
{false, false, true, ss, null, null, xml_bogus_catalog, null},
{false, false, true, ss, null, null, null, xml_bogus_catalog},
{false, false, true, stax, null, null, xml_bogus_catalog, null},
{false, false, true, stax1, null, null, null, xml_bogus_catalog},
{false, false, true, source, null, null, xml_bogus_catalog, null},
{false, false, true, source, null, null, null, xml_bogus_catalog},
};
}
@DataProvider(name = "data_ValidatorC")
public Object[][] getDataValidator() {
DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
SAXSource ss = new SAXSource(new InputSource(xml_val_test));
ss.setSystemId(xml_val_test_id);
StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
StreamSource source = new StreamSource(new File(xml_val_test));
return new Object[][]{
// use catalog
{false, false, true, ds, null, null, xml_catalog, null},
{false, false, true, ds, null, null, null, xml_catalog},
{false, false, true, ss, null, null, xml_catalog, null},
{false, false, true, ss, null, null, null, xml_catalog},
{false, false, true, stax, null, null, xml_catalog, null},
{false, false, true, stax1, null, null, null, xml_catalog},
{false, false, true, source, null, null, xml_catalog, null},
{false, false, true, source, null, null, null, xml_catalog},
};
}
/**
* Return the {@link XMLEventReader} for the given StAX Source.
* @param source a JAXP 1.4 {@link StAXSource}
* @return the {@link XMLEventReader}
* @throws IllegalArgumentException if {@code source} isn't a JAXP 1.4 {@link StAXSource}
* or custom StAX Source
*/
@Nullable
public static XMLEventReader getXMLEventReader(Source source) {
if (source instanceof StAXSource) {
return ((StAXSource) source).getXMLEventReader();
}
else if (source instanceof StaxSource) {
return ((StaxSource) source).getXMLEventReader();
}
else {
throw new IllegalArgumentException("Source '" + source + "' is neither StaxSource nor StAXSource");
}
}
@Test
public void readStAXSourceWithXmlBomb() throws Exception {
// https://en.wikipedia.org/wiki/Billion_laughs
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
String content = "<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE lolz [\n" +
" <!ENTITY lol \"lol\">\n" +
" <!ELEMENT lolz (#PCDATA)>\n" +
" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
"]>\n" +
"<root>&lol9;</root>";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
StAXSource result = (StAXSource) this.converter.read(StAXSource.class, inputMessage);
XMLStreamReader streamReader = result.getXMLStreamReader();
assertTrue(streamReader.hasNext());
streamReader.next();
streamReader.next();
String s = streamReader.getLocalName();
assertEquals("root", s);
this.thrown.expectMessage("\"lol9\"");
s = streamReader.getElementText();
}
@Test
public void isStaxSourceJaxp14() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
String expected = "<element/>";
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
StAXSource source = new StAXSource(streamReader);
assertTrue("Not a StAX Source", StaxUtils.isStaxSource(source));
}
private List<String> splitResponseByMMO(String response)
throws XMLStreamException, TransformerException {
XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(new StringReader(response));
while (!reader.hasName() || !"MMOs".equals(reader.getLocalName())) {
reader.next();
}
List<String> mmoStrings = new ArrayList<>();
while (reader.nextTag() == XMLStreamConstants.START_ELEMENT) {
StringWriter buffer = new StringWriter();
transformer.transform(new StAXSource(reader), new StreamResult(buffer));
mmoStrings.add(buffer.toString());
}
return mmoStrings;
}
@Test
public void readStAXSource() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
XMLStreamReader streamReader = result.getXMLStreamReader();
assertTrue(streamReader.hasNext());
streamReader.nextTag();
String s = streamReader.getLocalName();
assertEquals("root", s);
s = streamReader.getElementText();
assertEquals("Hello World", s);
streamReader.close();
}
@Test
public void readStAXSourceWithXmlBomb() throws Exception {
// https://en.wikipedia.org/wiki/Billion_laughs
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
String content = "<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE lolz [\n" +
" <!ENTITY lol \"lol\">\n" +
" <!ELEMENT lolz (#PCDATA)>\n" +
" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
"]>\n" +
"<root>&lol9;</root>";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
StAXSource result = (StAXSource) this.converter.read(StAXSource.class, inputMessage);
XMLStreamReader streamReader = result.getXMLStreamReader();
assertTrue(streamReader.hasNext());
streamReader.next();
streamReader.next();
String s = streamReader.getLocalName();
assertEquals("root", s);
assertThatExceptionOfType(XMLStreamException.class).isThrownBy(() ->
streamReader.getElementText())
.withMessageContaining("\"lol9\"");
}
private StAXSource getStAXEventSource() {
try {
return new StAXSource(ifac.createXMLEventReader(xmlInputStream()));
} catch (XMLStreamException e) {
throw new WrapperException(e);
}
}
@Test
public void unmarshalJaxp14StaxSourceXmlEventReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
StAXSource source = new StAXSource(eventReader);
Object flights = unmarshaller.unmarshal(source);
testFlights(flights);
}
private void validate(XMLStreamReader reader) throws XMLStreamException {
try {
Source source = new StreamSource(getClass().getClassLoader().getResourceAsStream(SCHEMA_LOCATION));
Schema schema = schemaFactory.newSchema(source);
Validator validator = schema.newValidator();
validator.validate(new StAXSource(reader));
} catch(Exception e) {
throw new XMLStreamException(e);
}
}
@Test
public void unmarshalJaxp14StaxSourceXmlStreamReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
StAXSource source = new StAXSource(streamReader);
Object flights = unmarshaller.unmarshal(source);
testFlights(flights);
}
public void validate(InputStream inputStream) throws Exception {
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(XmlDataImporter.findResource("schema/artemis-import-export.xsd"));
Validator validator = schema.newValidator();
validator.validate(new StAXSource(reader));
reader.close();
}
@Test
public void readCorrect() throws Exception {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StAXSource source = new StAXSource(streamReader);
StringWriter writer = new StringWriter();
transformer.transform(source, new StreamResult(writer));
Predicate<Node> nodeFilter = n ->
n.getNodeType() != Node.DOCUMENT_TYPE_NODE && n.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE;
assertThat(writer.toString(), isSimilarTo(XML).withNodeFilter(nodeFilter));
}
@Test
public void isStaxSourceJaxp14() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
String expected = "<element/>";
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
StAXSource source = new StAXSource(streamReader);
assertTrue("Not a StAX Source", StaxUtils.isStaxSource(source));
}
@Test
public void testStAXSourcePAYLOAD() throws Exception {
URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
assertNotNull(wsdl);
Service service = Service.create(wsdl, SERVICE_NAME);
assertNotNull(service);
Dispatch<StAXSource> disp = service.createDispatch(PORT_NAME, StAXSource.class, Service.Mode.PAYLOAD);
disp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://localhost:"
+ greeterPort
+ "/SOAPDispatchService/SoapDispatchPort");
QName opQName = new QName("http://apache.org/hello_world_soap_http", "greetMe");
disp.getRequestContext().put(MessageContext.WSDL_OPERATION, opQName);
// Test request-response
InputStream is = getClass().getResourceAsStream("resources/GreetMeDocLiteralSOAPBodyReq.xml");
StAXSource staxSourceReq = new StAXSource(StaxUtils.createXMLStreamReader(is));
assertNotNull(staxSourceReq);
Source resp = disp.invoke(staxSourceReq);
assertNotNull(resp);
assertTrue(resp instanceof StAXSource);
String expected = "Hello TestSOAPInputMessage";
String actual = StaxUtils.toString(StaxUtils.read(resp));
assertTrue("Expected: " + expected, actual.contains(expected));
}
@Test
public void unmarshalJaxp14StaxSourceXmlStreamReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
StAXSource source = new StAXSource(streamReader);
Object flights = unmarshaller.unmarshal(source);
testFlights(flights);
}
@Test
public void unmarshalJaxp14StaxSourceXmlEventReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
StAXSource source = new StAXSource(eventReader);
Object flights = unmarshaller.unmarshal(source);
testFlights(flights);
}
public static CtEsitoAvvisoDigitale leggiProssimoEsitoAvvisoDigitale(XMLStreamReader xsr, Transformer t) throws JAXBException, IOException, XMLStreamException, TransformerException {
if(trovaProssimoEsitoAvvisoDigitale(xsr)) {
DOMResult result = new DOMResult();
t.transform(new StAXSource(xsr), result);
Node domNode = result.getNode();
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<CtEsitoAvvisoDigitale> root = jaxbUnmarshaller.unmarshal(domNode, CtEsitoAvvisoDigitale.class);
return root.getValue();
} else {
return null;
}
}
@Override
void convertAsXml(final Message in) {
final String body = bodyAsString(in);
if (body == null) {
return;
}
try {
final XMLStreamReader bodyReader = XML_INPUT_FACTORY.createXMLStreamReader(new StringReader(body));
final XMLStreamReader eventReader = XML_INPUT_FACTORY.createFilteredReader(bodyReader, new XmlPayloadProcessor(in.getHeaders()));
final Source source = new StAXSource(eventReader);
final ByteArrayOutputStream out = new ByteArrayOutputStream(body.length());
final Result result = new StreamResult(out);
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
final Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
in.setBody(new String(out.toByteArray(), StandardCharsets.UTF_8));
} catch (XMLStreamException | TransformerFactoryConfigurationError | TransformerException e) {
LOG.warn("Unable to parse payload, continuing without conversion", e);
return;
}
}