01: package net.sf.saxon.charcode;
02:
03: import net.sf.saxon.om.XMLChar;
04:
05: import java.nio.charset.Charset;
06: import java.nio.charset.CharsetEncoder;
07: import java.util.HashMap;
08:
09: /**
10: * This class establishes properties of a character set that is
11: * known to the Java VM but not specifically known to Saxon
12: */
13:
14: public class UnknownCharacterSet implements CharacterSet {
15:
16: public static HashMap map;
17:
18: private CharsetEncoder encoder;
19:
20: // This class is written on the assumption that the CharsetEncoder.canEncode()
21: // method may be expensive. For BMP characters, it therefore remembers the results
22: // so each character is only looked up the first time it is encountered.
23:
24: private byte[] charinfo = new byte[65536];
25: // rely on initialization to zeroes
26: private StringBuffer supplementary = new StringBuffer(2);
27:
28: //private final static byte UNKNOWN = 0;
29: 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(
37: Charset charset) {
38: if (map == null) {
39: map = new HashMap(10);
40: }
41: UnknownCharacterSet c = (UnknownCharacterSet) map.get(charset);
42: if (c == null) {
43: c = new UnknownCharacterSet(charset);
44: map.put(charset, c);
45: }
46: return c;
47: }
48:
49: public final boolean inCharset(int c) {
50: // Assume ASCII chars are always OK
51: if (c <= 127) {
52: return true;
53: }
54: if (c <= 65535) {
55: if (charinfo[c] == GOOD) {
56: return true;
57: } else if (charinfo[c] == BAD) {
58: return false;
59: } else {
60: if (encoder.canEncode((char) c)) {
61: charinfo[c] = GOOD;
62: return true;
63: } else {
64: charinfo[c] = BAD;
65: return false;
66: }
67: }
68: } else {
69: supplementary.setCharAt(0, XMLChar.highSurrogate(c));
70: supplementary.setCharAt(1, XMLChar.lowSurrogate(c));
71: return encoder.canEncode(supplementary);
72: }
73: }
74:
75: }
76:
77: //
78: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
79: // you may not use this file except in compliance with the License. You may obtain a copy of the
80: // License at http://www.mozilla.org/MPL/
81: //
82: // Software distributed under the License is distributed on an "AS IS" basis,
83: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
84: // See the License for the specific language governing rights and limitations under the License.
85: //
86: // The Original Code is: all this file.
87: //
88: // The Initial Developer of the Original Code is
89: // Aleksei Makarov [makarov@iitam.omsk.net.ru]
90: //
91: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
92: //
93: // Contributor(s): none.
94: //
|