use of javax.xml.transform.stax.StAXSource in project activemq-artemis by apache.
the class XmlDataImporter method validate.
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();
}
use of javax.xml.transform.stax.StAXSource in project spring-framework by spring-projects.
the class SourceHttpMessageConverterTests method readStAXSourceWithXmlBomb.
@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(StandardCharsets.UTF_8));
StAXSource result = (StAXSource) this.converter.read(StAXSource.class, inputMessage);
XMLStreamReader streamReader = result.getXMLStreamReader();
assertThat(streamReader.hasNext()).isTrue();
streamReader.next();
streamReader.next();
String s = streamReader.getLocalName();
assertThat(s).isEqualTo("root");
assertThatExceptionOfType(XMLStreamException.class).isThrownBy(streamReader::getElementText).withMessageContaining("\"lol9\"");
}
use of javax.xml.transform.stax.StAXSource in project spring-framework by spring-projects.
the class SourceHttpMessageConverterTests method readStAXSource.
@Test
public void readStAXSource() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
XMLStreamReader streamReader = result.getXMLStreamReader();
assertThat(streamReader.hasNext()).isTrue();
streamReader.nextTag();
String s = streamReader.getLocalName();
assertThat(s).isEqualTo("root");
s = streamReader.getElementText();
assertThat(s).isEqualTo("Hello World");
streamReader.close();
}
use of javax.xml.transform.stax.StAXSource in project tomee by apache.
the class StaxUtils method copy.
public static void copy(Source source, XMLStreamWriter writer) throws XMLStreamException {
if (source instanceof StaxSource) {
StaxSource ss = (StaxSource) source;
if (ss.getXMLStreamReader() == null) {
return;
}
} else if (source instanceof StAXSource) {
StAXSource ss = (StAXSource) source;
if (ss.getXMLStreamReader() == null) {
return;
}
} else if (source instanceof SAXSource) {
SAXSource ss = (SAXSource) source;
InputSource src = ss.getInputSource();
if (src == null || (src.getSystemId() == null && src.getPublicId() == null)) {
if (ss.getXMLReader() != null) {
// OK - reader is OK. We'll use that out
StreamWriterContentHandler ch = new StreamWriterContentHandler(writer);
XMLReader reader = ((SAXSource) source).getXMLReader();
reader.setContentHandler(ch);
try {
try {
reader.setFeature("http://xml.org/sax/features/namespaces", true);
} catch (Throwable t) {
// ignore
}
try {
reader.setProperty("http://xml.org/sax/properties/lexical-handler", ch);
} catch (Throwable t) {
// ignore
}
reader.parse(((SAXSource) source).getInputSource());
return;
} catch (Exception e) {
throw new XMLStreamException(e.getMessage(), e);
}
} else if (ss.getInputSource() == null) {
// nothing to copy, just return
return;
}
}
} else if (source instanceof StreamSource) {
StreamSource ss = (StreamSource) source;
if (ss.getInputStream() == null && ss.getReader() == null && ss.getSystemId() == null) {
// nothing to copy, just return
return;
}
}
XMLStreamReader reader = createXMLStreamReader(source);
copy(reader, writer);
reader.close();
}
use of javax.xml.transform.stax.StAXSource in project spring-framework by spring-projects.
the class AbstractUnmarshallerTests method unmarshalJaxp14StaxSourceXmlStreamReader.
@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);
}
Aggregations