You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by "Kumar, Ritesh (GEP, Contractor)" <Ri...@gepex.ge.com> on 2000/11/23 01:52:40 UTC

A bug in xalan...

Hi Xalan Developers,
		       I guess I have found a bug in Xalan 1.2.1
 I had posted the problem several times on the forum, got a few replies but
they were not of  much use. I also had a discussion with Scott, but the
problem was unsolved.I think I have found the solution for  thr problem.

Problem: You have just one xsl and many xml files in different languages
(e.g. Japanese) . All the xml files are to be displayed using this xsl file
and the servlet(DefaultApplyXSL). The problem is with the existing version
of  xalan unless you explicitly give the encoding as "SHIFT_JIS" in your xsl
file, the japanese file cannot be displayed. But for an english file, this
xsl won't work.Even if you give the encoding as "UTF-8" or "UTF-16" in xsl
files, both the enghish as well as japanese files won't work for the same
xsl.

Reason: I think the reason for this discrepeny is a section of code in
ApplyXSL. The default encoding for xalan is 
"UTF-8". The parser is  parsing the xml and xsl files properly and giving
the output in UTF-8 encoding. But the DefaultApplyXsl is not able to throw
it to browser in proper way.

In ApplyXSL class the codes at lines 331 and 332 are

  331          if ((contentType =
getContentType(xslprocessor.processStylesheet(xslSource))) != null)
  332            response.setContentType(contentType);

Here the content type of response object is set dynamically from a parameter
from the xsl file.

Next in the lines from 335 to 349 the files are parsed and set to the
resonse object of the servlet.

335    if (debug)
            {
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              XSLTResultTarget outBuffer = new XSLTResultTarget(baos);
              setStylesheetParams(xslprocessor, request);
              xslprocessor.process(xmlSource, xslSource, outBuffer);
              baos.writeTo(response.getOutputStream());
              writeLog(listener.getMessage(), response.SC_OK);
            }
            else
            {
              setStylesheetParams(xslprocessor, request);
              xslprocessor.process(xmlSource, xslSource,
                                   new
XSLTResultTarget(response.getWriter()));
349      }	

Here if the debug is false, PrintWriter object is retrieved from the
response object and that is used to throw the output to the browser.Even if
you are setting the content type of response object in line number 332, but
the encoding of output/response returned to this response object from the
parser (which is in UTF-8 ) is lost! That is the reason the code fails.
Even if the debug is true, the code fails proabably beacase the content type
of the response object is not set properly in line 332.Anyway you cannoot
set the encoding type of the ServletOutputStream/OutputStream, so setting
the encoding type in line 332 is not going to effect it.

Solution: I replaced the lines of code in 331 and 332 as 
331		 response.setContentType("text/html; charset=UTF-8");

This makes sure that the encoding type of response object is always UTF-8
which can take care of all the languages.

And then I replaced the lines of code from 335 to 349 by these lines.

 335	  ByteArrayOutputStream baos = new ByteArrayOutputStream();
              XSLTResultTarget outBuffer = new XSLTResultTarget(baos);
              setStylesheetParams(xslprocessor, request);
              xslprocessor.process(xmlSource, xslSource, outBuffer);
              baos.writeTo(response.getOutputStream());
              writeLog(listener.getMessage(), response.SC_OK);  

This lines of code ensures that the encoding type of the parsed output(i.e.
UTF-8)  is never lost by the servlet.The output in
 UTF-8 from the parser is returned as it is to the browser by the
servlet.And this modification cares care of everything. Now I can display an
xml file in any langauge with the same xsl file. 
I havenot mentioned anything as encoding explicitly in my xsl file. And this
WORKS!!!

Doubt: Although I could get this thing working, I still have a doubt in my
mind. I cannot set encoding type of ServletOutputStream beacuse the data is
this stream is at the lowest level (I guess so - I think it is in binary
form ). So by this rule even if donot set the contentType in the response
object of the ApplyXSL in MY code  , the code should work.But it doesnot!

I am very confused ( but relieved since the code is working) as to how all
this is working.........

Any help in this regard will be higjly appreciated.

Thanks a lot..........(for reading such a looooooooooooong mail)

Bye,
Ritesh Kumar