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