Search in sources :

Example 31 with StAXSource

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();
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) Schema(javax.xml.validation.Schema) StAXSource(javax.xml.transform.stax.StAXSource) Validator(javax.xml.validation.Validator)

Example 32 with StAXSource

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\"");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XMLStreamReader(javax.xml.stream.XMLStreamReader) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.jupiter.api.Test)

Example 33 with StAXSource

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();
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XMLStreamReader(javax.xml.stream.XMLStreamReader) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.jupiter.api.Test)

Example 34 with StAXSource

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();
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) XMLReader(org.xml.sax.XMLReader) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 35 with StAXSource

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);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.jupiter.api.Test)

Aggregations

StAXSource (javax.xml.transform.stax.StAXSource)66 XMLStreamReader (javax.xml.stream.XMLStreamReader)27 XMLStreamException (javax.xml.stream.XMLStreamException)24 StringReader (java.io.StringReader)19 XMLInputFactory (javax.xml.stream.XMLInputFactory)19 XMLEventReader (javax.xml.stream.XMLEventReader)16 StreamSource (javax.xml.transform.stream.StreamSource)15 DOMSource (javax.xml.transform.dom.DOMSource)14 Source (javax.xml.transform.Source)13 SAXSource (javax.xml.transform.sax.SAXSource)12 Test (org.junit.Test)12 StreamResult (javax.xml.transform.stream.StreamResult)10 InputStream (java.io.InputStream)9 TransformerException (javax.xml.transform.TransformerException)8 IOException (java.io.IOException)7 Transformer (javax.xml.transform.Transformer)7 Test (org.junit.jupiter.api.Test)7 Document (org.w3c.dom.Document)7 InputSource (org.xml.sax.InputSource)7 StringWriter (java.io.StringWriter)6