mrglavas 2005/05/22 12:11:07 Modified: java/src/org/apache/xml/serialize EncodingInfo.java Log: Fixing JIRA Bug #735: http://issues.apache.org/jira/browse/XERCESJ-735 sun.io.CharToByteConverter doesn't exist in some JDKs. There's a strong possibility sun.io.CharToByteConverter won't exist at all in J2SE 6.0. Use java.nio.charset.CharsetEncoder by reflection if it's available. This should work on JDK 1.4 an above and be portable. Revision Changes Path 1.7 +148 -39 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.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- EncodingInfo.java 24 Feb 2004 23:34:03 -0000 1.6 +++ EncodingInfo.java 22 May 2005 19:11:07 -0000 1.7 @@ -1,5 +1,5 @@ /* - * Copyright 2000-2002,2004 The Apache Software Foundation. + * Copyright 2000-2002,2004,2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,6 @@ * limitations under the License. */ - package org.apache.xml.serialize; import java.io.OutputStream; @@ -29,17 +28,8 @@ * @version $Id$ */ 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. + // An array to hold the argument for a method of Charset, CharsetEncoder or CharToByteConverter. private Object [] fArgsForMethod = null; // name of encoding as registered with IANA; @@ -48,13 +38,19 @@ String javaName; int lastPrintable; - // The charToByteConverter with which we test unusual characters. + // The CharsetEncoder with which we test unusual characters. + Object fCharsetEncoder = null; + + // 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; + + // Is the charset encoder usable or available. + boolean fHaveTriedCharsetEncoder = false; /** * Creates new <code>EncodingInfo</code> instance. @@ -91,29 +87,80 @@ return new OutputStreamWriter(output, "UTF8"); return new OutputStreamWriter(output, javaName); } + /** - * Checks whether the specified character is printable or not - * in this encoding. + * Checks whether the specified character is printable or not in this encoding. * * @param ch a code point (0-0x10ffff) */ public boolean isPrintable(char ch) { - if(ch <= this.lastPrintable) + if (ch <= this.lastPrintable) { return true; + } + return isPrintable0(ch); + } + + /** + * Checks whether the specified character is printable or not in this encoding. + * This method accomplishes this using a java.nio.CharsetEncoder. If NIO isn't + * available it will attempt use a sun.io.CharToByteConverter. + * + * @param ch a code point (0-0x10ffff) + */ + private boolean isPrintable0(char ch) { + + // Attempt to get a CharsetEncoder for this encoding. + if (fCharsetEncoder == null && CharsetMethods.fgNIOCharsetAvailable && !fHaveTriedCharsetEncoder) { + if (fArgsForMethod == null) { + fArgsForMethod = new Object [1]; + } + // try and create the CharsetEncoder + try { + fArgsForMethod[0] = javaName; + Object charset = CharsetMethods.fgCharsetForNameMethod.invoke(null, fArgsForMethod); + if (((Boolean) CharsetMethods.fgCharsetCanEncodeMethod.invoke(charset, (Object[]) null)).booleanValue()) { + fCharsetEncoder = CharsetMethods.fgCharsetNewEncoderMethod.invoke(charset, (Object[]) null); + } + // This charset cannot be used for encoding, don't try it again... + else { + fHaveTriedCharsetEncoder = true; + } + } + catch (Exception e) { + // don't try it again... + fHaveTriedCharsetEncoder = true; + } + } + // Attempt to use the CharsetEncoder to determine whether the character is printable. + if (fCharsetEncoder != null) { + try { + fArgsForMethod[0] = new Character(ch); + return ((Boolean) CharsetMethods.fgCharsetEncoderCanEncodeMethod.invoke(fCharsetEncoder, fArgsForMethod)).booleanValue(); + } + catch (Exception e) { + // obviously can't use this charset encoder; possibly a JDK bug + fCharsetEncoder = null; + fHaveTriedCharsetEncoder = false; + } + } - if(fCharToByteConverter == null) { - if(fHaveTriedCToB || !fgConvertersAvailable) { + // As a last resort try to use a sun.io.CharToByteConverter to + // determine whether this character is printable. We will always + // reach here on JDK 1.3 or below. + if (fCharToByteConverter == null) { + if (fHaveTriedCToB || !CharToByteConverterMethods.fgConvertersAvailable) { // forget it; nothing we can do... return false; } if (fArgsForMethod == null) { fArgsForMethod = new Object [1]; } - // try and create it: + // try and create the CharToByteConverter try { fArgsForMethod[0] = javaName; - fCharToByteConverter = fgGetConverterMethod.invoke(null, fArgsForMethod); - } catch(Exception e) { + fCharToByteConverter = CharToByteConverterMethods.fgGetConverterMethod.invoke(null, fArgsForMethod); + } + catch (Exception e) { // don't try it again... fHaveTriedCToB = true; return false; @@ -121,8 +168,9 @@ } try { fArgsForMethod[0] = new Character(ch); - return ((Boolean) fgCanConvertMethod.invoke(fCharToByteConverter, fArgsForMethod)).booleanValue(); - } catch (Exception e) { + return ((Boolean) CharToByteConverterMethods.fgCanConvertMethod.invoke(fCharToByteConverter, fArgsForMethod)).booleanValue(); + } + catch (Exception e) { // obviously can't use this converter; probably some kind of // security restriction fCharToByteConverter = null; @@ -138,21 +186,82 @@ 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; + /** + * Holder of methods from java.nio.charset.Charset and java.nio.charset.CharsetEncoder. + */ + static class CharsetMethods { + + // Method: java.nio.charset.Charset.forName(java.lang.String) + private static java.lang.reflect.Method fgCharsetForNameMethod = null; + + // Method: java.nio.charset.Charset.canEncode() + private static java.lang.reflect.Method fgCharsetCanEncodeMethod = null; + + // Method: java.nio.charset.Charset.newEncoder() + private static java.lang.reflect.Method fgCharsetNewEncoderMethod = null; + + // Method: java.nio.charset.CharsetEncoder.canEncode(char) + private static java.lang.reflect.Method fgCharsetEncoderCanEncodeMethod = null; + + // Flag indicating whether or not java.nio.charset.* is available. + private static boolean fgNIOCharsetAvailable = false; + + private CharsetMethods() {} + + // Attempt to get methods for Charset and CharsetEncoder on class initialization. + static { + try { + Class charsetClass = Class.forName("java.nio.charset.Charset"); + Class charsetEncoderClass = Class.forName("java.nio.charset.CharsetEncoder"); + fgCharsetForNameMethod = charsetClass.getMethod("forName", new Class [] {String.class}); + fgCharsetCanEncodeMethod = charsetClass.getMethod("canEncode", new Class [] {}); + fgCharsetNewEncoderMethod = charsetClass.getMethod("newEncoder", new Class [] {}); + fgCharsetEncoderCanEncodeMethod = charsetEncoderClass.getMethod("canEncode", new Class [] {Character.TYPE}); + fgNIOCharsetAvailable = true; + } + // ClassNotFoundException, NoSuchMethodException or SecurityException + // Whatever the case, we cannot use java.nio.charset.*. + catch (Exception exc) { + fgCharsetForNameMethod = null; + fgCharsetCanEncodeMethod = null; + fgCharsetEncoderCanEncodeMethod = null; + fgCharsetNewEncoderMethod = null; + fgNIOCharsetAvailable = false; + } + } + } + + /** + * Holder of methods from sun.io.CharToByteConverter. + */ + static class CharToByteConverterMethods { + + // 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; + + private CharToByteConverterMethods() {} + + // 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
