Re: ContentHandler.startDocument callback not coming, while transforming the input XML File.

Michael Glavassevich <[email protected]> Tue, 12 Jun 2012 10:38:34 -0400
Newsgroups gmane.text.xml.xalan.java.user
Message-ID <OFA9AF7BEE.A21C9E6B-ON85257A1B.00501697-85257A1B.00506F23@ca.ibm.com>
This is a multipart message in MIME format.
--=_alternative 00506F2085257A1B_=
Content-Type: text/plain; charset="US-ASCII"

You could write an XMLFilter [1] which wraps the XMLReader and pass that 
to the Transformer instead.

[1] 
http://xerces.apache.org/xerces2-j/javadocs/api/org/xml/sax/XMLFilter.html

Michael Glavassevich
XML Technologies and WAS Development
IBM Toronto Lab
E-mail: [email protected]
E-mail: [email protected]

Harsh_Gupta <[email protected]> wrote on 12/06/2012 09:11:15 AM:

> We want to get startDocument callback in following scenarios:
> - When any input xml file is parsed before doing transformation.
> - Or when any file is opened using document function of XSLT.
> 
> Even after registering our own ContentHandler object in xmlReader 
object, we
> aren't getting any callbacks. While debugging the xalan code, I found 
that
> before parsing the input XML file, xalan is overriding the user supplied
> ContentHandler object, LexicalHandler object and DTDHandler object. 
> 
> Is there any way/workaround to get the callbacks? Any help would be
> appreciated.
> 
> The code that we are using is as follows:
> 
> String inputFilePath =
> "C:\\Users\\harshg.ADOBENET\\Desktop\\BugFiles\\document\\entity.xml";
> String xslFilePath =
> "C:\\Users\\harshg.ADOBENET\\Desktop\\BugFiles\\document\\vendor.xsl";
> String outputFilePath =
> "C:/Users/harshg.ADOBENET/Desktop/BugFiles/document/Data-out1.xml";
> 
> //1) Created the XMLReader and registered the ContentHandler as follows: 

> XMLReader m_xmlReader =
> SAXParserFactory.newInstance().newSAXParser().getXMLReader();
> m_xmlReader.setContentHandler(new FMSAXEventHandler());
> 
> //2) Created the TransformerFactory as follows:
> System.setProperty("javax.xml.transform.TransformerFactory",
> "org.apache.xalan.processor.TransformerFactoryImpl");
> TransformerFactory m_tFactory = TransformerFactory.newInstance();
> m_tFactory.setURIResolver(new FMURIResolver());
> 
> //3) Created the transformer as follows:
> Transformer transformer = m_tFactory.newTransformer(new StreamSource(new
> File(xslFilePath).toURI().toURL().toExternalForm()));
> 
> //4) Transforming the input file as follows:
> transformer.transform(new SAXSource(m_xmlReader, new InputSource(new
> File(inputFilePath).toURI().toURL().toExternalForm())), new 
StreamResult(new
> OutputStreamWriter(new FileOutputStream(outputFilePath), "UTF-8")));
> 
> 
> Thanks!
> Harsh Gupta
> Adobe Systems
--=_alternative 00506F2085257A1B_=
Content-Type: text/html; charset="US-ASCII"

<tt><font size=2>You could write an XMLFilter [1] which wraps the XMLReader
and pass that to the Transformer instead.</font></tt>
<br>
<br><tt><font size=2>[1] </font></tt><a href="http://xerces.apache.org/xerces2-j/javadocs/api/org/xml/sax/XMLFilter.html"><tt><font size=2>http://xerces.apache.org/xerces2-j/javadocs/api/org/xml/sax/XMLFilter.html</font></tt></a>
<br>
<br><tt><font size=2>Michael Glavassevich<br>
XML Technologies and WAS Development<br>
IBM Toronto Lab<br>
E-mail: [email protected]</font></tt>
<br><tt><font size=2>E-mail: [email protected]</font></tt>
<br>
<br><tt><font size=2>Harsh_Gupta &lt;[email protected]&gt; wrote on 12/06/2012
09:11:15 AM:<br>
<br>
&gt; We want to get startDocument callback in following scenarios:<br>
&gt; - When any input xml file is parsed before doing transformation.<br>
&gt; - Or when any file is opened using document function of XSLT.<br>
&gt; <br>
&gt; Even after registering our own ContentHandler object in xmlReader
object, we<br>
&gt; aren't getting any callbacks. While debugging the xalan code, I found
that<br>
&gt; before parsing the input XML file, xalan is overriding the user supplied<br>
&gt; ContentHandler object, LexicalHandler object and DTDHandler object.
<br>
&gt; <br>
&gt; Is there any way/workaround to get the callbacks? Any help would be<br>
&gt; appreciated.<br>
&gt; <br>
&gt; The code that we are using is as follows:<br>
&gt; <br>
&gt; String inputFilePath =<br>
&gt; &quot;C:\\Users\\harshg.ADOBENET\\Desktop\\BugFiles\\document\\entity.xml&quot;;<br>
&gt; String xslFilePath =<br>
&gt; &quot;C:\\Users\\harshg.ADOBENET\\Desktop\\BugFiles\\document\\vendor.xsl&quot;;<br>
&gt; String outputFilePath =<br>
&gt; &quot;C:/Users/harshg.ADOBENET/Desktop/BugFiles/document/Data-out1.xml&quot;;<br>
&gt; <br>
&gt; //1) Created the XMLReader and registered the ContentHandler as follows:
<br>
&gt; XMLReader m_xmlReader =<br>
&gt; SAXParserFactory.newInstance().newSAXParser().getXMLReader();<br>
&gt; m_xmlReader.setContentHandler(new FMSAXEventHandler());<br>
&gt; <br>
&gt; //2) Created the TransformerFactory as follows:<br>
&gt; System.setProperty(&quot;javax.xml.transform.TransformerFactory&quot;,<br>
&gt; &quot;org.apache.xalan.processor.TransformerFactoryImpl&quot;);<br>
&gt; TransformerFactory m_tFactory = TransformerFactory.newInstance();<br>
&gt; m_tFactory.setURIResolver(new FMURIResolver());<br>
&gt; <br>
&gt; //3) Created the transformer as follows:<br>
&gt; Transformer transformer = m_tFactory.newTransformer(new StreamSource(new<br>
&gt; File(xslFilePath).toURI().toURL().toExternalForm()));<br>
&gt; <br>
&gt; //4) Transforming the input file as follows:<br>
&gt; transformer.transform(new SAXSource(m_xmlReader, new InputSource(new<br>
&gt; File(inputFilePath).toURI().toURL().toExternalForm())), new StreamResult(new<br>
&gt; OutputStreamWriter(new FileOutputStream(outputFilePath), &quot;UTF-8&quot;)));<br>
&gt; <br>
&gt; <br>
&gt; Thanks!<br>
&gt; Harsh Gupta<br>
&gt; Adobe Systems</font></tt>
--=_alternative 00506F2085257A1B_=--