Please take a look at the code of getAssociatedStylesheet(Source source, String media, String title, String charset) throws TransformerConfigurationException in TransformerFactoryImpl class:
The definition of above function is ignoring the XMLReader I've passed in as a argument through source parameter (which is essentially a SAXSource object). The above function is creating a new XMLReader after creating a new SAXParserFactory object. Because of this, the problem that we are facing is, that while parsing the input XML file, it goes on parsing the DOCTYPE section and the external DTD specified through Public ID and System ID. Now since, our XMLReader is not used to parse the XML file, the EntityResolver registered with the reader isn't used, and hence external DTD is not resolved, and therefore getAssociatedStylesheet function fails with an exception.
However, if you take a look at the other function public Templates newTemplates(Source source) throws TransformerConfigurationException in TransformerFactoryImpl class. This function specifically checks, if the source argument passed in is a SAXSource, and if it is, then if XMLReader is present in the source, then it doesn't create the new XMLReader object.
Is this a bug? Any thoughts?
XalanJ Version: 2.7.1 Source file path: src\org\apache\xalan\processor\TransformerFactoryImpl.java
Thanks! Harsh Gupta Adobe Systems
Since Saxon is perfectly able to find the associated stylesheet, with the registered custom entity resolver, therefore whenever transformation is happening with XalanJ, we are using Saxon for getting the associated Stylesheet.
And once we get the associated stylesheet, we pass the input file, and the stylesheet to Xalan for transformation.