1 package net.sf.saxon.charcode; 2 3 import net.sf.saxon.om.XMLChar; 4 5 import java.nio.CharBuffer ; 6 import java.nio.charset.CharacterCodingException ; 7 import java.nio.charset.Charset ; 8 import java.nio.charset.CharsetEncoder ; 9 import java.util.HashMap ; 10 11 19 20 public class BuggyCharacterSet implements CharacterSet { 21 22 private static HashMap map; 23 24 private CharsetEncoder encoder; 25 26 30 private byte[] charinfo = new byte[65536]; 31 33 private static final byte GOOD = 1; 35 private static final byte BAD = 2; 36 37 private BuggyCharacterSet(Charset charset) { 38 encoder = charset.newEncoder(); 39 } 40 41 public static synchronized BuggyCharacterSet makeCharSet(Charset charset) { 42 if (map == null) { 43 map = new HashMap (10); 44 } 45 BuggyCharacterSet c = (BuggyCharacterSet)map.get(charset); 46 if (c == null) { 47 c = new BuggyCharacterSet(charset); 48 map.put(charset, c); 49 } 50 return c; 51 } 52 53 public final boolean inCharset(int c) { 54 if (c <= 127) { 56 return true; 57 } 58 try { 59 if (c <= 65535) { 60 if (charinfo[c] == GOOD) { 61 return true; 62 } else if (charinfo[c] == BAD) { 63 return false; 64 } else { 65 charinfo[c] = BAD; char[] cc = {(char)c}; 67 encoder.encode(CharBuffer.wrap(cc)); 68 charinfo[c] = GOOD; 69 return true; 70 } 71 } else { 72 char[] ss = { XMLChar.highSurrogate(c), 73 XMLChar.lowSurrogate(c) }; 74 encoder.encode(CharBuffer.wrap(ss)); 75 return true; 76 } 77 } catch (CharacterCodingException ex) { 78 return false; 79 } 80 } 81 82 } 83 84 | Popular Tags |