1 16 17 package org.apache.xml.serialize; 18 19 import java.io.OutputStream ; 20 import java.io.OutputStreamWriter ; 21 import java.io.UnsupportedEncodingException ; 22 import java.io.Writer ; 23 import org.apache.xerces.util.EncodingMap; 24 25 30 public class EncodingInfo { 31 32 private Object [] fArgsForMethod = null; 34 35 String ianaName; 38 String javaName; 39 int lastPrintable; 40 41 Object fCharsetEncoder = null; 43 44 Object fCharToByteConverter = null; 46 47 boolean fHaveTriedCToB = false; 51 52 boolean fHaveTriedCharsetEncoder = false; 54 55 58 public EncodingInfo(String ianaName, String javaName, int lastPrintable) { 59 this.ianaName = ianaName; 60 this.javaName = EncodingMap.getIANA2JavaMapping(ianaName); 61 this.lastPrintable = lastPrintable; 62 } 63 64 67 public String getIANAName() { 68 return this.ianaName; 69 } 70 71 79 public Writer getWriter(OutputStream output) 80 throws UnsupportedEncodingException { 81 if (javaName != null) 83 return new OutputStreamWriter (output, javaName); 84 javaName = EncodingMap.getIANA2JavaMapping(ianaName); 85 if(javaName == null) 86 return new OutputStreamWriter (output, "UTF8"); 88 return new OutputStreamWriter (output, javaName); 89 } 90 91 96 public boolean isPrintable(char ch) { 97 if (ch <= this.lastPrintable) { 98 return true; 99 } 100 return isPrintable0(ch); 101 } 102 103 110 private boolean isPrintable0(char ch) { 111 112 if (fCharsetEncoder == null && CharsetMethods.fgNIOCharsetAvailable && !fHaveTriedCharsetEncoder) { 114 if (fArgsForMethod == null) { 115 fArgsForMethod = new Object [1]; 116 } 117 try { 119 fArgsForMethod[0] = javaName; 120 Object charset = CharsetMethods.fgCharsetForNameMethod.invoke(null, fArgsForMethod); 121 if (((Boolean ) CharsetMethods.fgCharsetCanEncodeMethod.invoke(charset, (Object []) null)).booleanValue()) { 122 fCharsetEncoder = CharsetMethods.fgCharsetNewEncoderMethod.invoke(charset, (Object []) null); 123 } 124 else { 126 fHaveTriedCharsetEncoder = true; 127 } 128 } 129 catch (Exception e) { 130 fHaveTriedCharsetEncoder = true; 132 } 133 } 134 if (fCharsetEncoder != null) { 136 try { 137 fArgsForMethod[0] = new Character (ch); 138 return ((Boolean ) CharsetMethods.fgCharsetEncoderCanEncodeMethod.invoke(fCharsetEncoder, fArgsForMethod)).booleanValue(); 139 } 140 catch (Exception e) { 141 fCharsetEncoder = null; 143 fHaveTriedCharsetEncoder = false; 144 } 145 } 146 147 if (fCharToByteConverter == null) { 151 if (fHaveTriedCToB || !CharToByteConverterMethods.fgConvertersAvailable) { 152 return false; 154 } 155 if (fArgsForMethod == null) { 156 fArgsForMethod = new Object [1]; 157 } 158 try { 160 fArgsForMethod[0] = javaName; 161 fCharToByteConverter = CharToByteConverterMethods.fgGetConverterMethod.invoke(null, fArgsForMethod); 162 } 163 catch (Exception e) { 164 fHaveTriedCToB = true; 166 return false; 167 } 168 } 169 try { 170 fArgsForMethod[0] = new Character (ch); 171 return ((Boolean ) CharToByteConverterMethods.fgCanConvertMethod.invoke(fCharToByteConverter, fArgsForMethod)).booleanValue(); 172 } 173 catch (Exception e) { 174 fCharToByteConverter = null; 177 fHaveTriedCToB = false; 178 return false; 179 } 180 } 181 182 public static void testJavaEncodingName(String name) throws UnsupportedEncodingException { 185 final byte [] bTest = {(byte)'v', (byte)'a', (byte)'l', (byte)'i', (byte)'d'}; 186 String s = new String (bTest, name); 187 } 188 189 192 static class CharsetMethods { 193 194 private static java.lang.reflect.Method fgCharsetForNameMethod = null; 196 197 private static java.lang.reflect.Method fgCharsetCanEncodeMethod = null; 199 200 private static java.lang.reflect.Method fgCharsetNewEncoderMethod = null; 202 203 private static java.lang.reflect.Method fgCharsetEncoderCanEncodeMethod = null; 205 206 private static boolean fgNIOCharsetAvailable = false; 208 209 private CharsetMethods() {} 210 211 static { 213 try { 214 Class charsetClass = Class.forName("java.nio.charset.Charset"); 215 Class charsetEncoderClass = Class.forName("java.nio.charset.CharsetEncoder"); 216 fgCharsetForNameMethod = charsetClass.getMethod("forName", new Class [] {String .class}); 217 fgCharsetCanEncodeMethod = charsetClass.getMethod("canEncode", new Class [] {}); 218 fgCharsetNewEncoderMethod = charsetClass.getMethod("newEncoder", new Class [] {}); 219 fgCharsetEncoderCanEncodeMethod = charsetEncoderClass.getMethod("canEncode", new Class [] {Character.TYPE}); 220 fgNIOCharsetAvailable = true; 221 } 222 catch (Exception exc) { 225 fgCharsetForNameMethod = null; 226 fgCharsetCanEncodeMethod = null; 227 fgCharsetEncoderCanEncodeMethod = null; 228 fgCharsetNewEncoderMethod = null; 229 fgNIOCharsetAvailable = false; 230 } 231 } 232 } 233 234 237 static class CharToByteConverterMethods { 238 239 private static java.lang.reflect.Method fgGetConverterMethod = null; 241 242 private static java.lang.reflect.Method fgCanConvertMethod = null; 244 245 private static boolean fgConvertersAvailable = false; 247 248 private CharToByteConverterMethods() {} 249 250 static { 252 try { 253 Class clazz = Class.forName("sun.io.CharToByteConverter"); 254 fgGetConverterMethod = clazz.getMethod("getConverter", new Class [] {String .class}); 255 fgCanConvertMethod = clazz.getMethod("canConvert", new Class [] {Character.TYPE}); 256 fgConvertersAvailable = true; 257 } 258 catch (Exception exc) { 261 fgGetConverterMethod = null; 262 fgCanConvertMethod = null; 263 fgConvertersAvailable = false; 264 } 265 } 266 } 267 } 268 | Popular Tags |