KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > charcode > UnknownCharacterSet


1 package net.sf.saxon.charcode;
2
3 import net.sf.saxon.om.XMLChar;
4
5 import java.nio.charset.Charset JavaDoc;
6 import java.nio.charset.CharsetEncoder JavaDoc;
7 import java.util.HashMap JavaDoc;
8
9 /**
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 JavaDoc map;
17
18     private CharsetEncoder JavaDoc 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 JavaDoc supplementary = new StringBuffer JavaDoc(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 JavaDoc charset) {
33         encoder = charset.newEncoder();
34     }
35
36     public static synchronized UnknownCharacterSet makeCharSet(Charset JavaDoc charset) {
37         if (map == null) {
38             map = new HashMap JavaDoc(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         // Assume ASCII chars are always OK
50
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 //
77
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
78
// you may not use this file except in compliance with the License. You may obtain a copy of the
79
// License at http://www.mozilla.org/MPL/
80
//
81
// Software distributed under the License is distributed on an "AS IS" basis,
82
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
83
// See the License for the specific language governing rights and limitations under the License.
84
//
85
// The Original Code is: all this file.
86
//
87
// The Initial Developer of the Original Code is
88
// Aleksei Makarov [makarov@iitam.omsk.net.ru]
89
//
90
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
91
//
92
// Contributor(s): none.
93
//
94
Popular Tags