You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2003/11/04 20:24:54 UTC

cvs commit: xml-xerces/java/src/org/apache/xml/serialize EncodingInfo.java

mrglavas    2003/11/04 11:24:54

  Modified:    java/src/org/apache/xml/serialize EncodingInfo.java
  Log:
  Fixing Bug #20304:
  http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20304
  
  Static compile time and run time dependencies on
  sun.io.CharToByteConverter made it impossible to compile
  Xerces with JDKs which do not contain this class, and made
  the serializer unusable (assuming you already have a binary)
  for encodings which tried to access it. Users were probably
  getting a NoClassDefFoundError in this situation.
  
  For now, access the CharToByteConverter through reflection.
  This should hopefully resolve both of those problems, however
  on platforms where the converter is available we fall back to the
  old behaviour of printing character references which are
  expressable in the output encoding. This is something to revisit
  in the future.
  
  Revision  Changes    Path
  1.5       +46 -11    xml-xerces/java/src/org/apache/xml/serialize/EncodingInfo.java
  
  Index: EncodingInfo.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/EncodingInfo.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- EncodingInfo.java	20 Jan 2003 22:43:14 -0000	1.4
  +++ EncodingInfo.java	4 Nov 2003 19:24:54 -0000	1.5
  @@ -62,7 +62,6 @@
   import java.io.OutputStreamWriter;
   import java.io.UnsupportedEncodingException;
   import java.io.Writer;
  -import sun.io.CharToByteConverter;
   import org.apache.xerces.util.EncodingMap;
   
   /**
  @@ -72,15 +71,28 @@
    */
   public class EncodingInfo {
   
  +    // Method: sun.io.CharToByteConverter.getConverter(java.lang.String)
  +    private static java.lang.reflect.Method fgGetConverterMethod = null; 
  +    
  +    // Method: sun.io.CharToByteConverter.canConvert(char)
  +    private static java.lang.reflect.Method fgCanConvertMethod = null;
  +    
  +    // Flag indicating whether or not sun.io.CharToByteConverter is available.
  +    private static boolean fgConvertersAvailable = false;
  +    
  +    // An array to hold the argument for a method of CharToByteConverter.
  +    private Object [] fArgsForMethod = null;
  +    
       // name of encoding as registered with IANA;
       // preferably a MIME name, but aliases are fine too.
       String ianaName;
       String javaName;
       int lastPrintable;
  -    // the charToByteConverter we test unusual characters
  -    // with
  -    CharToByteConverter fCToB = null;
  -    // is the converter null because it can't be instantiated
  +    
  +    // The charToByteConverter with which we test unusual characters.
  +    Object fCharToByteConverter = null;
  +    
  +    // Is the converter null because it can't be instantiated
       // for some reason (perhaps we're running with insufficient authority as 
       // an applet?
       boolean fHaveTriedCToB = false;
  @@ -130,14 +142,18 @@
           if(ch <= this.lastPrintable) 
               return true;
           
  -        if(fCToB == null) {
  -            if(fHaveTriedCToB) {
  +        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 {
  -                fCToB = CharToByteConverter.getConverter(javaName);
  +                fArgsForMethod[0] = javaName;
  +                fCharToByteConverter = fgGetConverterMethod.invoke(null, fArgsForMethod);
               } catch(Exception e) {   
                   // don't try it again...
                   fHaveTriedCToB = true;
  @@ -145,11 +161,12 @@
               }
           }
           try {
  -            return fCToB.canConvert(ch); 
  +            fArgsForMethod[0] = new Character(ch);
  +            return ((Boolean) fgCanConvertMethod.invoke(fCharToByteConverter, fArgsForMethod)).booleanValue();
           } catch (Exception e) {
               // obviously can't use this converter; probably some kind of
               // security restriction
  -            fCToB = null;
  +            fCharToByteConverter = null;
               fHaveTriedCToB = false;
               return false;
           }
  @@ -160,5 +177,23 @@
       public static void testJavaEncodingName(String name)  throws UnsupportedEncodingException {
           final byte [] bTest = {(byte)'v', (byte)'a', (byte)'l', (byte)'i', (byte)'d'};
           String s = new String(bTest, name);
  +    }
  +    
  +    // Attempt to get methods for char to byte 
  +    // converter on class initialization.
  +    static {
  +        try {
  +            Class clazz = Class.forName("sun.io.CharToByteConverter");
  +            fgGetConverterMethod = clazz.getMethod("getConverter", new Class [] {String.class});
  +            fgCanConvertMethod = clazz.getMethod("canConvert", new Class [] {Character.TYPE});
  +            fgConvertersAvailable = true;
  +        }
  +        // ClassNotFoundException, NoSuchMethodException or SecurityException
  +        // Whatever the case, we cannot use sun.io.CharToByteConverter.
  +        catch (Exception exc) {
  +            fgGetConverterMethod = null;
  +            fgCanConvertMethod = null;
  +            fgConvertersAvailable = false;
  +        }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org