1 16 19 package org.apache.xml.serializer; 20 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.io.OutputStreamWriter ; 24 import java.io.UnsupportedEncodingException ; 25 import java.io.Writer ; 26 import java.lang.reflect.Method ; 27 import java.net.URL ; 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import java.util.Properties ; 31 import java.util.StringTokenizer ; 32 import java.security.PrivilegedAction ; 33 import java.security.AccessController ; 34 35 36 45 46 public class Encodings extends Object 47 { 48 49 52 static final int m_defaultLastPrintable = 0x7F; 53 54 57 static final String ENCODINGS_FILE = "org/apache/xml/serializer/Encodings.properties"; 58 59 62 static final String ENCODINGS_PROP = "org.apache.xalan.serialize.encodings"; 63 64 65 private static final Method 66 SUN_CHAR2BYTE_CONVERTER_METHOD = findCharToByteConverterMethod(); 67 68 private static Method findCharToByteConverterMethod() { 69 try 70 { 71 AccessController.doPrivileged(new PrivilegedAction () { 72 public Object run() { 73 try { 74 Class charToByteConverterClass = (Class ) 75 Class.forName("sun.io.CharToByteConverter"); 76 Class argTypes[] = {String .class}; 77 return charToByteConverterClass.getMethod("getConverter", argTypes); 78 } 79 catch (Exception e) { 80 throw new RuntimeException (e.toString()); 81 } 82 }}); 83 } 84 catch (Exception e) 85 { 86 System.err.println( 87 "Warning: Could not get charToByteConverterClass!"); 88 } 89 90 return null; 91 } 92 93 103 public static Writer getWriter(OutputStream output, String encoding) 104 throws UnsupportedEncodingException 105 { 106 107 for (int i = 0; i < _encodings.length; ++i) 108 { 109 if (_encodings[i].name.equalsIgnoreCase(encoding)) 110 { 111 try 112 { 113 return new OutputStreamWriter ( 114 output, 115 _encodings[i].javaName); 116 } 117 catch (java.lang.IllegalArgumentException iae) { 119 } 121 catch (UnsupportedEncodingException usee) 122 { 123 124 } 126 } 127 } 128 129 try 130 { 131 return new OutputStreamWriter (output, encoding); 132 } 133 catch (java.lang.IllegalArgumentException iae) { 135 throw new UnsupportedEncodingException (encoding); 136 } 137 } 138 139 145 public static Object getCharToByteConverter(String encoding) 146 { 147 if (SUN_CHAR2BYTE_CONVERTER_METHOD == null) { 148 return null; 149 } 150 151 Object args[] = new Object [1]; 152 for (int i = 0; i < _encodings.length; ++i) 153 { 154 if (_encodings[i].name.equalsIgnoreCase(encoding)) 155 { 156 try 157 { 158 args[0] = _encodings[i].javaName; 159 Object converter = 160 SUN_CHAR2BYTE_CONVERTER_METHOD.invoke(null, args); 161 if (null != converter) 162 return converter; 163 } 164 catch (Exception iae) 165 { 166 } 168 } 169 } 170 171 return null; 172 } 173 174 181 public static int getLastPrintable(String encoding) 182 { 183 EncodingInfo ei; 184 185 String normalizedEncoding = encoding.toUpperCase(); 186 ei = (EncodingInfo) _encodingTableKeyJava.get(normalizedEncoding); 187 if (ei == null) 188 ei = (EncodingInfo) _encodingTableKeyMime.get(normalizedEncoding); 189 if (ei != null) 190 return ei.lastPrintable; 191 return m_defaultLastPrintable; 192 } 193 194 200 public static int getLastPrintable() 201 { 202 return m_defaultLastPrintable; 203 } 204 205 206 public static final String DEFAULT_MIME_ENCODING = "UTF-8"; 207 208 224 public static String getMimeEncoding(String encoding) 225 { 226 227 if (null == encoding) 228 { 229 try 230 { 231 232 encoding = System.getProperty("file.encoding", "UTF8"); 236 237 if (null != encoding) 238 { 239 240 247 String jencoding = 248 (encoding.equalsIgnoreCase("Cp1252") 249 || encoding.equalsIgnoreCase("ISO8859_1") 250 || encoding.equalsIgnoreCase("8859_1") 251 || encoding.equalsIgnoreCase("UTF8")) 252 ? DEFAULT_MIME_ENCODING 253 : convertJava2MimeEncoding(encoding); 254 255 encoding = 256 (null != jencoding) ? jencoding : DEFAULT_MIME_ENCODING; 257 } 258 else 259 { 260 encoding = DEFAULT_MIME_ENCODING; 261 } 262 } 263 catch (SecurityException se) 264 { 265 encoding = DEFAULT_MIME_ENCODING; 266 } 267 } 268 else 269 { 270 encoding = convertJava2MimeEncoding(encoding); 271 } 272 273 return encoding; 274 } 275 276 283 public static String convertJava2MimeEncoding(String encoding) 284 { 285 EncodingInfo enc = 286 (EncodingInfo) _encodingTableKeyJava.get(encoding.toUpperCase()); 287 if (null != enc) 288 return enc.name; 289 return encoding; 290 } 291 292 299 public static String convertMime2JavaEncoding(String encoding) 300 { 301 302 for (int i = 0; i < _encodings.length; ++i) 303 { 304 if (_encodings[i].name.equalsIgnoreCase(encoding)) 305 { 306 return _encodings[i].javaName; 307 } 308 } 309 310 return encoding; 311 } 312 313 320 private static EncodingInfo[] loadEncodingInfo() 321 { 322 URL url = null; 323 try 324 { 325 String urlString = null; 326 InputStream is = null; 327 328 try 329 { 330 urlString = System.getProperty(ENCODINGS_PROP, ""); 331 } 332 catch (SecurityException e) 333 { 334 } 335 336 if (urlString != null && urlString.length() > 0) { 337 url = new URL (urlString); 338 is = url.openStream(); 339 } 340 341 if (is == null) { 342 SecuritySupport ss = SecuritySupport.getInstance(); 343 is = ss.getResourceAsStream(ObjectFactory.findClassLoader(), 344 ENCODINGS_FILE); 345 } 346 347 Properties props = new Properties (); 348 if (is != null) { 349 props.load(is); 350 is.close(); 351 } else { 352 } 359 360 int totalEntries = props.size(); 361 int totalMimeNames = 0; 362 Enumeration keys = props.keys(); 363 for (int i = 0; i < totalEntries; ++i) 364 { 365 String javaName = (String ) keys.nextElement(); 366 String val = props.getProperty(javaName); 367 totalMimeNames++; 368 int pos = val.indexOf(' '); 369 for (int j = 0; j < pos; ++j) 370 if (val.charAt(j) == ',') 371 totalMimeNames++; 372 } 373 EncodingInfo[] ret = new EncodingInfo[totalMimeNames]; 374 int j = 0; 375 keys = props.keys(); 376 for (int i = 0; i < totalEntries; ++i) 377 { 378 String javaName = (String ) keys.nextElement(); 379 String val = props.getProperty(javaName); 380 int pos = val.indexOf(' '); 381 String mimeName; 382 int lastPrintable; 383 if (pos < 0) 384 { 385 mimeName = val; 389 lastPrintable = 0x00FF; 390 } 391 else 392 { 393 lastPrintable = 394 Integer.decode(val.substring(pos).trim()).intValue(); 395 StringTokenizer st = 396 new StringTokenizer (val.substring(0, pos), ","); 397 for (boolean first = true; 398 st.hasMoreTokens(); 399 first = false) 400 { 401 mimeName = st.nextToken(); 402 ret[j] = 403 new EncodingInfo(mimeName, javaName, lastPrintable); 404 _encodingTableKeyMime.put( 405 mimeName.toUpperCase(), 406 ret[j]); 407 if (first) 408 _encodingTableKeyJava.put( 409 javaName.toUpperCase(), 410 ret[j]); 411 j++; 412 } 413 } 414 } 415 return ret; 416 } 417 catch (java.net.MalformedURLException mue) 418 { 419 throw new org.apache.xml.utils.WrappedRuntimeException(mue); 420 } 421 catch (java.io.IOException ioe) 422 { 423 throw new org.apache.xml.utils.WrappedRuntimeException(ioe); 424 } 425 } 426 427 private static final Hashtable _encodingTableKeyJava = new Hashtable (); 428 private static final Hashtable _encodingTableKeyMime = new Hashtable (); 429 private static final EncodingInfo[] _encodings = loadEncodingInfo(); 430 } 431 | Popular Tags |