Search in sources :

Example 26 with SAXSource

use of javax.xml.transform.sax.SAXSource in project spring-framework by spring-projects.

the class SourceHttpMessageConverter method readSAXSource.

// on JDK 9
@SuppressWarnings("deprecation")
private SAXSource readSAXSource(InputStream body) throws IOException {
    try {
        XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
        xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
        xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
        if (!isProcessExternalEntities()) {
            xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
        }
        byte[] bytes = StreamUtils.copyToByteArray(body);
        return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream(bytes)));
    } catch (SAXException ex) {
        throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 27 with SAXSource

use of javax.xml.transform.sax.SAXSource in project spring-framework by spring-projects.

the class SourceHttpMessageConverterTests method writeSAXSource.

@Test
public void writeSAXSource() throws Exception {
    String xml = "<root>Hello World</root>";
    SAXSource saxSource = new SAXSource(new InputSource(new StringReader(xml)));
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(saxSource, null, outputMessage);
    assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<root>Hello World</root>"));
    assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) StringReader(java.io.StringReader) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 28 with SAXSource

use of javax.xml.transform.sax.SAXSource in project spring-framework by spring-projects.

the class SourceHttpMessageConverterTests method readSAXSourceWithXmlBomb.

@Test
public void readSAXSourceWithXmlBomb() 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"));
    SAXSource result = (SAXSource) this.converter.read(SAXSource.class, inputMessage);
    this.thrown.expect(SAXException.class);
    this.thrown.expectMessage("DOCTYPE");
    InputSource inputSource = result.getInputSource();
    XMLReader reader = result.getXMLReader();
    reader.parse(inputSource);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) Test(org.junit.Test)

Example 29 with SAXSource

use of javax.xml.transform.sax.SAXSource in project camel by apache.

the class XmlJsonDataFormatTest method testMarshalXMLSources.

@Test
public void testMarshalXMLSources() throws Exception {
    InputStream inStream = getClass().getResourceAsStream("testMessage1.xml");
    DOMSource inDOM = context.getTypeConverter().convertTo(DOMSource.class, inStream);
    inStream = getClass().getResourceAsStream("testMessage1.xml");
    SAXSource inSAX = context.getTypeConverter().convertTo(SAXSource.class, inStream);
    inStream = getClass().getResourceAsStream("testMessage1.xml");
    Document inDocument = context.getTypeConverter().convertTo(Document.class, inStream);
    // save the expected body of the message to set it later
    Object expectedBody = template.requestBody("direct:marshal", inDOM);
    MockEndpoint mockJSON = getMockEndpoint("mock:json");
    // reset the mock endpoint to get rid of the previous message
    mockJSON.reset();
    // all three messages should arrive, should be of type byte[] and
    // identical to one another
    mockJSON.expectedMessageCount(3);
    mockJSON.allMessages().body().isInstanceOf(byte[].class);
    mockJSON.expectedBodiesReceived(Arrays.asList(expectedBody, expectedBody, expectedBody));
    // start bombarding the route
    Object json = template.requestBody("direct:marshal", inDOM);
    String jsonString = context.getTypeConverter().convertTo(String.class, json);
    JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
    assertEquals("JSONObject doesn't contain 7 keys", 7, obj.entrySet().size());
    template.requestBody("direct:marshal", inSAX);
    template.requestBody("direct:marshal", inDocument);
    mockJSON.assertIsSatisfied();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SAXSource(javax.xml.transform.sax.SAXSource) JSONObject(net.sf.json.JSONObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 30 with SAXSource

use of javax.xml.transform.sax.SAXSource in project asciidoctor-fopub by asciidoctor.

the class InputHandler method transformTo.

/**
     * Transforms the input document to the input format expected by FOP using XSLT.
     * @param result the Result object where the result of the XSL transformation is sent to
     * @throws FOPException in case of an error during processing
     */
protected void transformTo(Result result) throws FOPException {
    try {
        // Setup XSLT
        TransformerFactory factory = TransformerFactory.newInstance();
        if (uriResolver != null) {
            factory.setURIResolver(uriResolver);
        }
        factory.setErrorListener(this);
        Transformer transformer;
        Source xsltSource = createXSLTSource();
        if (xsltSource == null) {
            // FO Input
            transformer = factory.newTransformer();
        } else {
            // XML/XSLT input
            transformer = factory.newTransformer(xsltSource);
            // Set the value of parameters, if any, defined for stylesheet
            if (xsltParams != null) {
                for (int i = 0; i < xsltParams.size(); i += 2) {
                    transformer.setParameter((String) xsltParams.elementAt(i), (String) xsltParams.elementAt(i + 1));
                }
            }
        }
        transformer.setErrorListener(this);
        // Create a SAXSource from the input Source file
        Source src = createMainSource();
        // Start XSLT transformation and FOP processing
        transformer.transform(src, result);
    } catch (Exception e) {
        throw new FOPException(e);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) TransformerException(javax.xml.transform.TransformerException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Aggregations

SAXSource (javax.xml.transform.sax.SAXSource)111 InputSource (org.xml.sax.InputSource)81 XMLReader (org.xml.sax.XMLReader)38 Source (javax.xml.transform.Source)28 StreamSource (javax.xml.transform.stream.StreamSource)28 DOMSource (javax.xml.transform.dom.DOMSource)27 SAXException (org.xml.sax.SAXException)26 TransformerException (javax.xml.transform.TransformerException)24 SAXParserFactory (javax.xml.parsers.SAXParserFactory)20 Unmarshaller (javax.xml.bind.Unmarshaller)17 SAXParser (javax.xml.parsers.SAXParser)17 Transformer (javax.xml.transform.Transformer)17 StreamResult (javax.xml.transform.stream.StreamResult)16 Test (org.junit.Test)16 StringReader (java.io.StringReader)15 IOException (java.io.IOException)14 JAXBContext (javax.xml.bind.JAXBContext)14 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 ValidationEvent (javax.xml.bind.ValidationEvent)10