DOMSerializerImpl.writeToString() generates bugy XML due to implementation error in 
sun.io.CharToByteConverter
--------------------------------------------------------------------------------------------------------------

         Key: XERCESJ-1024
         URL: http://issues.apache.org/jira/browse/XERCESJ-1024
     Project: Xerces2-J
        Type: Bug
  Components: DOM, Serialization  
    Versions: 2.6.2    
 Environment: Linux, Xerces 2.6.2 from CVS, JDK1.3.1 and JDK1.4.2
    Reporter: Juergen Birkle
    Priority: Critical


There is a major bug in the class sun.io.CharToByteConverter !!!

The following code can be used to reproduce this error:
-----
public class Test {
   public static void main(String[] args)
   {
      try {
         sun.io.CharToByteConverter ctbc = 
sun.io.CharToByteConverter.getConverter("UTF-16");
         boolean result = ctbc.canConvert('a');
         System.out.println("Result1 = " + result);
         result = ctbc.canConvert('a');
         System.out.println("Result2 = " + result);
         result = ctbc.canConvert('a');
         System.out.println("Result3 = " + result);
      }
      catch(Exception e) {
         System.err.println("Error\n" + e);
      }
   }
}
-----

The output of this code is:
-----
Result1 = false
Result2 = true
Result3 = true
-----
As you can see, the first call to the method failed!


Because of this Bug the method writeToString(Node) from class DOMSerializerImpl 
generates bugy XML code!

The class "sun.io.CharToByteConverter" is used in the class "EncodingInfo". I fixed 
the bug by rewriting the method "isPrintable(char ch)":
-----
    public boolean isPrintable(char ch) {
        if(ch <= this.lastPrintable) 
            return true;

        if(fCharToByteConverter == null) {
            if(fHaveTriedCToB || !fgConvertersAvailable) {
                // forget it; nothing we can do...
                return false;
            }
            if (fArgsForMethod == null) {
                fArgsForMethod = new Object [1];
            }
            // try and create it:
            try {
                fArgsForMethod[0] = javaName;
                fCharToByteConverter = fgGetConverterMethod.invoke(null, 
fArgsForMethod);
            }
            catch(Exception e) {   
                // don't try it again...
                fHaveTriedCToB = true;
                return false;
            }

            // 
---------------------------------------------------------------------------------
            // NO FUNCTION - ONLY BUG WORKAROUND
            //
            // There is a bug in the implementation of sun.io.CharToByteConverter!!!
            // Because of this it is necessary to call the method once before 
            // it can be used correct!
            try {
               fArgsForMethod[0] = new Character('a');
               fgCanConvertMethod.invoke(fCharToByteConverter, fArgsForMethod);
            }
            catch(Exception e) {
               // obviously can't use this converter; probably some kind of
               // security restriction
               fCharToByteConverter = null;
               fHaveTriedCToB = false;
               return false;
            }
            // 
---------------------------------------------------------------------------------
        }

        try {
            fArgsForMethod[0] = new Character(ch);
            Boolean tmp = (Boolean)fgCanConvertMethod.invoke(fCharToByteConverter, 
fArgsForMethod);
            boolean result = tmp.booleanValue();
            System.out.println("Result = " + result);
            return result;
        }
        catch (Exception e) {
            // obviously can't use this converter; probably some kind of
            // security restriction
            fCharToByteConverter = null;
            fHaveTriedCToB = false;
            return false;
        }
    }
-----

I don't know where else in Xerces the class sun.io.CharToByteConverter is used, but 
all occurrences must be checked and fixed with a workaround!

Best regards,
Juergen Birkle




-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
If you want more information on JIRA, or have a bug to report see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to