| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize.codenorm; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.om.AxisInfo; |
| 12 |
import net.sf.saxon.z.IntHashMap; |
| 13 |
import net.sf.saxon.z.IntToIntHashMap; |
| 14 |
import net.sf.saxon.z.IntToIntMap; |
| 15 |
import net.sf.saxon.lib.ParseOptions; |
| 16 |
import net.sf.saxon.lib.Validation; |
| 17 |
import net.sf.saxon.om.TreeInfo; |
| 18 |
import net.sf.saxon.om.NodeInfo; |
| 19 |
import net.sf.saxon.pattern.NodeKindTest; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 22 |
|
| 23 |
import javax.xml.transform.stream.StreamSource; |
| 24 |
import java.io.InputStream; |
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.BitSet; |
| 27 |
import java.util.List; |
| 28 |
import java.util.StringTokenizer; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class UnicodeDataParserFromXML { |
| 37 |
|
| 38 |
|
| 39 |
private UnicodeDataParserFromXML() { |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
static NormalizerData build(Configuration config) throws XPathException { |
| 47 |
|
| 48 |
InputStream in = Configuration.locateResource( |
| 49 |
"normalizationData.xml", new ArrayList<>(), new ArrayList<>()); |
| 50 |
if (in == null) { |
| 51 |
throw new XPathException("Unable to read normalizationData.xml file"); |
| 52 |
} |
| 53 |
|
| 54 |
BitSet isExcluded = new BitSet(128000); |
| 55 |
BitSet isCompatibility = new BitSet(128000); |
| 56 |
|
| 57 |
ParseOptions options = new ParseOptions(); |
| 58 |
options.setSchemaValidationMode(Validation.SKIP); |
| 59 |
TreeInfo doc = config.buildDocumentTree(new StreamSource(in, "normalizationData.xml"), options); |
| 60 |
NodeInfo canonicalClassKeys = null; |
| 61 |
NodeInfo canonicalClassValues = null; |
| 62 |
NodeInfo decompositionKeys = null; |
| 63 |
NodeInfo decompositionValues = null; |
| 64 |
|
| 65 |
AxisIterator iter = doc.getRootNode().iterateAxis(AxisInfo.DESCENDANT, NodeKindTest.ELEMENT); |
| 66 |
NodeInfo item; |
| 67 |
while ((item = iter.next()) != null) { |
| 68 |
switch (item.getLocalPart()) { |
| 69 |
case "CanonicalClassKeys": |
| 70 |
canonicalClassKeys = item; |
| 71 |
break; |
| 72 |
case "CanonicalClassValues": |
| 73 |
canonicalClassValues = item; |
| 74 |
break; |
| 75 |
case "DecompositionKeys": |
| 76 |
decompositionKeys = item; |
| 77 |
break; |
| 78 |
case "DecompositionValues": |
| 79 |
decompositionValues = item; |
| 80 |
break; |
| 81 |
case "ExclusionList": |
| 82 |
readExclusionList(item.getStringValue(), isExcluded); |
| 83 |
break; |
| 84 |
case "CompatibilityList": |
| 85 |
readCompatibilityList(item.getStringValue(), isCompatibility); |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
IntToIntMap canonicalClass = new IntToIntHashMap(400); |
| 91 |
canonicalClass.setDefaultValue(0); |
| 92 |
readCanonicalClassTable(canonicalClassKeys.getStringValue(), canonicalClassValues.getStringValue(), canonicalClass); |
| 93 |
|
| 94 |
|
| 95 |
IntHashMap<String> decompose = new IntHashMap<>(18000); |
| 96 |
IntToIntMap compose = new IntToIntHashMap(15000); |
| 97 |
compose.setDefaultValue(NormalizerData.NOT_COMPOSITE); |
| 98 |
|
| 99 |
readDecompositionTable(decompositionKeys.getStringValue(), decompositionValues.getStringValue(), |
| 100 |
decompose, compose, isExcluded, isCompatibility); |
| 101 |
|
| 102 |
return new NormalizerData(canonicalClass, decompose, compose, |
| 103 |
isCompatibility, isExcluded); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
private static void readExclusionList(String s, BitSet isExcluded) { |
| 111 |
StringTokenizer st = new StringTokenizer(s); |
| 112 |
while (st.hasMoreTokens()) { |
| 113 |
String tok = st.nextToken(); |
| 114 |
int value = Integer.parseInt(tok, 32); |
| 115 |
isExcluded.set(value); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private static void readCompatibilityList(String s, BitSet isCompatible) { |
| 124 |
StringTokenizer st = new StringTokenizer(s); |
| 125 |
while (st.hasMoreTokens()) { |
| 126 |
String tok = st.nextToken(); |
| 127 |
int value = Integer.parseInt(tok, 32); |
| 128 |
isCompatible.set(value); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private static void readCanonicalClassTable(String keyString, String valueString, IntToIntMap canonicalClasses) { |
| 137 |
List<Integer> keys = new ArrayList<>(5000); |
| 138 |
|
| 139 |
StringTokenizer st = new StringTokenizer(keyString); |
| 140 |
while (st.hasMoreTokens()) { |
| 141 |
String tok = st.nextToken(); |
| 142 |
int value = Integer.parseInt(tok, 32); |
| 143 |
keys.add(value); |
| 144 |
} |
| 145 |
|
| 146 |
int k = 0; |
| 147 |
st = new StringTokenizer(valueString); |
| 148 |
while (st.hasMoreTokens()) { |
| 149 |
String tok = st.nextToken(); |
| 150 |
int clss; |
| 151 |
int repeat = 1; |
| 152 |
int star = tok.indexOf('*'); |
| 153 |
if (star < 0) { |
| 154 |
clss = Integer.parseInt(tok, 32); |
| 155 |
} else { |
| 156 |
repeat = Integer.parseInt(tok.substring(0, star)); |
| 157 |
clss = Integer.parseInt(tok.substring(star + 1), 32); |
| 158 |
} |
| 159 |
for (int i = 0; i < repeat; i++) { |
| 160 |
canonicalClasses.put(keys.get(k++), clss); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private static void readDecompositionTable( |
| 170 |
String decompositionKeyString, String decompositionValuesString, |
| 171 |
IntHashMap<String> decompose, IntToIntMap compose, |
| 172 |
BitSet isExcluded, BitSet isCompatibility) { |
| 173 |
int k = 0; |
| 174 |
|
| 175 |
List<String> values = new ArrayList<>(1000); |
| 176 |
StringTokenizer st = new StringTokenizer(decompositionValuesString); |
| 177 |
while (st.hasMoreTokens()) { |
| 178 |
String tok = st.nextToken(); |
| 179 |
StringBuilder value = new StringBuilder(); |
| 180 |
for (int c = 0; c < tok.length(); ) { |
| 181 |
char h0 = tok.charAt(c++); |
| 182 |
char h1 = tok.charAt(c++); |
| 183 |
char h2 = tok.charAt(c++); |
| 184 |
char h3 = tok.charAt(c++); |
| 185 |
int code = ("0123456789abcdef".indexOf(h0) << 12) + |
| 186 |
("0123456789abcdef".indexOf(h1) << 8) + |
| 187 |
("0123456789abcdef".indexOf(h2) << 4) + |
| 188 |
"0123456789abcdef".indexOf(h3); |
| 189 |
value.append((char) code); |
| 190 |
} |
| 191 |
values.add(value.toString()); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
st = new StringTokenizer(decompositionKeyString); |
| 196 |
while (st.hasMoreTokens()) { |
| 197 |
String tok = st.nextToken(); |
| 198 |
int key = Integer.parseInt(tok, 32); |
| 199 |
String value = values.get(k++); |
| 200 |
decompose.put(key, value); |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
if (!isCompatibility.get(key) && !isExcluded.get(key)) { |
| 205 |
char first = '\u0000'; |
| 206 |
char second = value.charAt(0); |
| 207 |
if (value.length() > 1) { |
| 208 |
first = second; |
| 209 |
second = value.charAt(1); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
int pair = (first << 16) | second; |
| 215 |
compose.put(pair, key); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
for (int SIndex = 0; SIndex < SCount; ++SIndex) { |
| 224 |
int TIndex = SIndex % TCount; |
| 225 |
char first, second; |
| 226 |
if (TIndex != 0) { |
| 227 |
first = (char) (SBase + SIndex - TIndex); |
| 228 |
second = (char) (TBase + TIndex); |
| 229 |
} else { |
| 230 |
first = (char) (LBase + SIndex / NCount); |
| 231 |
second = (char) (VBase + (SIndex % NCount) / TCount); |
| 232 |
} |
| 233 |
int pair = (first << 16) | second; |
| 234 |
int key = SIndex + SBase; |
| 235 |
decompose.put(key, String.valueOf(first) + second); |
| 236 |
compose.put(pair, key); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
private static final int |
| 244 |
SBase = 0xAC00, LBase = 0x1100, VBase = 0x1161, TBase = 0x11A7, |
| 245 |
LCount = 19, VCount = 21, TCount = 28, |
| 246 |
NCount = VCount * TCount, |
| 247 |
SCount = LCount * NCount; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|