1 21 22 package nu.xom; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.io.OutputStreamWriter ; 27 import java.io.UnsupportedEncodingException ; 28 import java.io.Writer ; 29 import java.util.Locale ; 30 31 46 class GenericWriter extends TextWriter { 47 48 49 private ByteArrayOutputStream bout; 50 private OutputStreamWriter wout; 51 private boolean isJapanese = false; 52 53 54 GenericWriter(Writer out, String encoding) 55 throws UnsupportedEncodingException { 56 57 super(out, encoding); 58 bout = new ByteArrayOutputStream (32); 59 wout = new OutputStreamWriter (bout, encoding); 60 encoding = encoding.toUpperCase(Locale.ENGLISH); 61 if (encoding.indexOf("EUC-JP") > -1 62 || encoding.startsWith("EUC_JP") 63 || encoding.equals("SHIFT_JIS") 64 || encoding.equals("SJIS") 65 || encoding.equals("ISO-2022-JP")) { 66 isJapanese = true; 67 } 68 69 } 70 71 72 boolean needsEscaping(char c) { 73 74 if (c <= 127) return false; 76 if (isJapanese) { 78 if (c == 0xA5) return true; if (c == 0x203E) return true; } 81 82 boolean result = false; 83 try { 84 wout.write(c); 85 wout.flush(); 86 byte[] data = bout.toByteArray(); 87 if (data.length == 0) result = true; else if (data[0] == '?') result = true; 89 else if (isJapanese && data[0] == 0x21) result = true; 92 } 93 catch (IOException ex) { 94 return true; 99 } 100 catch (Error err) { 101 return true; 105 } 106 finally { 107 bout.reset(); 108 } 109 return result; 110 111 } 112 113 114 } 115 | Popular Tags |