You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Maris Orbidans <sm...@apollo.lv> on 2001/03/15 18:13:42 UTC

problems with encoding in XSLT

hello

I use version 2.0.0.0  of  Xalan/Xerces,  I have a  problem that I can't use other character encodings in XSL 
document but UTF-8.  But I want to use cp-1257 and cp-1252 encoding.

If I change  "encoding" in XSL header then the parser throws "unsupported encoding" exception.  But 
I have seen  Xalan/Xerces ( some older version ) working with stylesheets in cp-1257 encoding.

Could you help me ?

thanx
Maris Orbidans


--------------------------------------------------------------------------------

// THESE METHODS THAT I WROTE THROW THE EXCEPTION

 public void writeDocumentTo(Writer out) throws IOException
 {
  xmlDoc.write(out);
 }

 public void writeDocumentTo(Writer out, String encoding) throws IOException
 {
  xmlDoc.write(out, encoding);
 }
 
 public String writeDoc2Str() throws IOException
 {
  StringWriter writer = new StringWriter();
  xmlDoc.write(writer);
  return writer.toString();
 }


 public String transformToHtmlString (String stXslUri) throws Exception
 {       
         TransformerFactory tFactory = TransformerFactory.newInstance();
 
         Transformer transformer = tFactory.newTransformer(new StreamSource(stXslUri));
         ByteArrayOutputStream theOutputStream = new ByteArrayOutputStream();
                StreamResult theResult= new StreamResult(theOutputStream);
                StringWriter swXmlDoc = new StringWriter();
                writeDocumentTo(swXmlDoc);
                StringReader srXmlDoc = new StringReader(swXmlDoc.toString());
                transformer.transform(new StreamSource(srXmlDoc),theResult);
                return (theOutputStream.toString());
 }
 
 public String transformToHtmlString (String stXslUri,String encoding) throws Exception 
 {       
         TransformerFactory tFactory = TransformerFactory.newInstance();
 
               Transformer transformer = tFactory.newTransformer(new StreamSource(stXslUri));
         ByteArrayOutputStream theOutputStream = new ByteArrayOutputStream();
                StreamResult theResult= new StreamResult(theOutputStream);
                StringWriter swXmlDoc = new StringWriter();
                writeDocumentTo(swXmlDoc,encoding);
                StringReader srXmlDoc = new StringReader(swXmlDoc.toString());
               transformer.transform(new StreamSource(srXmlDoc),theResult);
                return (theOutputStream.toString(encoding));
 }
 


-----------------------------------------
[14/03/2001 00:48:34:010 GMT+02:00] BooksDb: error in login : 
javax.xml.transform.TransformerConfigurationException: 
The encoding "windows-1257" is not supported.

---------------------------------------
<main_frame_2 username="abc">
  <search />
</main_frame_2>

-----------------------
<?xml version="1.0" encoding="cp-1257"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>

<xsl:template match="/">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=cp-1257"/>
<meta http-equiv="expires" content="0"/> 
<title>Testi ?Zeogr?üfij?ü (metodisks l?½dzeklis). I.?Cbols</title>
</head>
<body leftmargin="0" bgcolor="#e2e1c2" rightmargin="0" topmargin="0" bottommargin="0" link="#0000aa" alink="#0000aa" vlink="#0000aa">
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

-----------------------------------------

// THESE FUNCTIONS WORK WELL WITH NON UTF-8  XSL FILES !!!
// ( I DIDNT WRITE THEM )
// BUT THEY USE SOME OLD VERSION ( PERHAPS 1.0.0) OF XALAN
// I CANT FIGURE OUT WHY
// I HAVE LATEST VERSION OF XALAN/XERCES ONLY

/**
 Applies XSL to XML document and convert it to HTML in specified encoding
 @param url XSL file name
 @param encoding encoding of the result HTML file
*/
 public Writer applyXSL(String uri, String encoding) throws SAXException
 {
  StringWriter writer = new StringWriter();
  StringWriter writer2 = new StringWriter();
  this.writeDocumentTo(writer, encoding);
  StringReader reader = new StringReader(writer.toString());
  InputStream xsl = getClass().getResourceAsStream(uri);
 

  XSLTInputSource theXSLTInputSource1 = new XSLTInputSource(reader);
  file://XSLTInputSource theXSLTInputSource2 = new XSLTInputSource(uri);
  XSLTInputSource theXSLTInputSource2 = new XSLTInputSource(xsl);
  XSLTResultTarget theXSLTResultTarget = new XSLTResultTarget(writer2);

  XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
  processor.process(theXSLTInputSource1,
                      theXSLTInputSource2,
                      theXSLTResultTarget);
    return (Writer) writer2;
 }


/** Applies XSL to XML document ,convert it to HTML and returns string
  @param url XSL file name
*/

 public String applyXSL2String(String uri) throws SAXException
 {
  String html = "";
  try
  {
   html = stringFilter(this.applyXSL(uri).toString());
  }
  catch (NumberFormatException e)
  {
   throw new SAXException(e);
  }
  return html;
 }

/** Applies XSL to XML document ,convert it to HTML and returns string in specified encoding
  @param url XSL file name
  @param encoding - encoding of HTML
*/

 public String applyXSL2String(String uri, String encoding) throws SAXException
 {
  String html = "";
  try
  {
   html = stringFilter(this.applyXSL(uri, encoding).toString());
  }
  catch (NumberFormatException e)
  {
   throw new SAXException(e);
  }
  return html;
 }

/** Is used for filtering such expressions like &#x445; in HTML output.Converts such
  expressions back to the character representation.
  @param in input string to filter
*/

 protected static String stringFilter(String in) throws NumberFormatException
 {
  if(in != null && in.trim().length() > 0)
  {
   StringBuffer out = new StringBuffer();
   String tempStr;
   Character symbol;
   char tempChar1;
   char tempChar2;
   char tempChar3;
   int i = 0;
   int j;
   int inLength = in.length();
   do
   {
    tempChar1 = in.charAt(i);
    if (tempChar1 == '&')
    {
     i++;
     tempChar2 = in.charAt(i);
     if (tempChar2 == '#')
     {
      i++;
      tempChar3 = in.charAt(i);
      if (tempChar3 == 'x')
      {
       i++;
       j = 0;
       tempStr = "";
       while(in.charAt(i) != ';')
       {
        tempStr = tempStr + new Character(in.charAt(i)).toString();
        
        j++;
        if (j > 3)
        {
         out.append(tempChar1);
         out.append(tempChar2);
         out.append(tempChar3);
         out.append(tempStr);
         tempStr = "";
         break;
        }
        i++;
       }
       if (tempStr.length() > 0)
       {
        symbol = new Character((char)Integer.parseInt(tempStr,16));
        out.append(symbol.charValue());
       }
      }
      else
      {
       System.out.println("IN" + tempChar3);
       if (Character.isDigit(tempChar3))
       {
        i++;
        j = 0;
        tempStr = new Character(tempChar3).toString();
        while(in.charAt(i) != ';')
        {
         tempStr = tempStr + new Character(in.charAt(i)).toString();
         j++;
         if ((j > 4) || (!Character.isDigit(in.charAt(i))))
         {
          out.append(tempChar1);
          out.append(tempChar2);
          out.append(tempStr);
          tempStr = "";
          break;
         }
         i++;
        }
        if (tempStr.length() > 0)
        {
         System.out.println("OUT" + tempStr);
         symbol = new Character((char)Integer.parseInt(tempStr,10));
         out.append(symbol.charValue());
        }
       }
       else
       {
        out.append(tempChar1);
        out.append(tempChar2);
        out.append(tempChar3);
       }
      }
     }
     else
     {
      out.append(tempChar1);
      out.append(tempChar2);
     }
    }
    else
    {
     out.append(tempChar1);
    }
    i++;
   }
   while (i != inLength);
   return new String(out);
  }
  else return null;
 }
------------------------------------------


--------------------------------------------------------------------------------