Disable DTD validations in XML transform using a StreamSource



Is there a way to disable DTD validations in an XML transformation using a StreamSource ?


The code here



String source = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- COMMENT --><!DOCTYPE config SYSTEM \"validation.dtd\" ><config><host>localhost</host><port>2000</port><buffer-size>32768</buffer-size><retry-count>0</retry-count><retry-timeout>5000</retry-timeout><connect-timeout>60000</connect-timeout><write-timeout>5000</write-timeout></config>";
ByteArrayInputStream sourceInputStream = new ByteArrayInputStream(source.getBytes());

TransformerFactory optimusPrimeFactory = TransformerFactory.newInstance(
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", Test.class.getClassLoader());

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(sourceInputStream));

Result result = new StreamResult(new StringWriter());
Transformer bumblebee = optimusPrimeFactory.newTransformer();

bumblebee.transform(new StreamSource(bufferedReader), result);


for transforming this XML piece



<?xml version="1.0" encoding="UTF-8"?>
<!-- COMMENT -->
<!DOCTYPE config SYSTEM "validation.dtd" >
<config>
<host>localhost</host>
<port>2000</port>
<buffer-size>32768</buffer-size>
<retry-count>0</retry-count>
<retry-timeout>5000</retry-timeout>
<connect-timeout>60000</connect-timeout>
<write-timeout>5000</write-timeout>
</config>


will always throw for a FileNotFoundException since the DTD file does not exist, but I want to ignore the DTD validations and references in the transformation process.


This page proposes a solution with a custom EntityResolver, but I don't want to use SAX because it does not work in a streaming way.


I've tried setting features on the factory, but I can't seem to find the right ones:



optimusPrimeFactory.setFeature("http://ift.tt/1gFjbcP", false);
optimusPrimeFactory.setFeature("http://ift.tt/Qb9Hvl", false);


Please note that the use of the XSLTC TrasformerFactory implementation cannot be changed.


No comments:

Post a Comment