1 package net.sf.saxon.charcode; 2 3 import net.sf.saxon.om.XMLChar; 4 5 import java.nio.charset.Charset ; 6 import java.nio.charset.CharsetEncoder ; 7 import java.util.HashMap ; 8 9 13 14 public class UnknownCharacterSet implements CharacterSet { 15 16 public static HashMap map; 17 18 private CharsetEncoder encoder; 19 20 24 private byte[] charinfo = new byte[65536]; 25 private StringBuffer supplementary = new StringBuffer (2); 27 28 private static final byte GOOD = 1; 30 private static final byte BAD = 2; 31 32 private UnknownCharacterSet(Charset charset) { 33 encoder = charset.newEncoder(); 34 } 35 36 public static synchronized UnknownCharacterSet makeCharSet(Charset charset) { 37 if (map == null) { 38 map = new HashMap (10); 39 } 40 UnknownCharacterSet c = (UnknownCharacterSet)map.get(charset); 41 if (c == null) { 42 c = new UnknownCharacterSet(charset); 43 map.put(charset, c); 44 } 45 return c; 46 } 47 48 public final boolean inCharset(int c) { 49 if (c <= 127) { 51 return true; 52 } 53 if (c <= 65535) { 54 if (charinfo[c] == GOOD) { 55 return true; 56 } else if (charinfo[c] == BAD) { 57 return false; 58 } else { 59 if (encoder.canEncode((char)c)) { 60 charinfo[c] = GOOD; 61 return true; 62 } else { 63 charinfo[c] = BAD; 64 return false; 65 } 66 } 67 } else { 68 supplementary.setCharAt(0, XMLChar.highSurrogate(c)); 69 supplementary.setCharAt(1, XMLChar.lowSurrogate(c)); 70 return encoder.canEncode(supplementary); 71 } 72 } 73 74 } 75 76 | Popular Tags |