| 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.serialize.charcode.UTF16CharacterSet; |
| 12 |
import net.sf.saxon.trans.XPathException; |
| 13 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class Normalizer { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
private int form; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public Normalizer(int form, Configuration config) throws XPathException { |
| 46 |
this.form = form; |
| 47 |
if (data == null) { |
| 48 |
|
| 49 |
data = UnicodeDataParserFromXML.build(config); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
static final int |
| 57 |
COMPATIBILITY_MASK = 1, |
| 58 |
COMPOSITION_MASK = 2; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static final int |
| 64 |
D = 0, |
| 65 |
C = COMPOSITION_MASK, |
| 66 |
KD = COMPATIBILITY_MASK, |
| 67 |
KC = (byte) (COMPATIBILITY_MASK + COMPOSITION_MASK), |
| 68 |
NO_ACTION = 8; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public CharSequence normalize(CharSequence source) { |
| 77 |
|
| 78 |
if (form == NO_ACTION || source.length() == 0) { |
| 79 |
return source; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
FastStringBuffer target = new FastStringBuffer(source.length() + 8); |
| 86 |
internalDecompose(source, target); |
| 87 |
if ((form & COMPOSITION_MASK) != 0) { |
| 88 |
internalCompose(target); |
| 89 |
} |
| 90 |
return target; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private void internalDecompose(CharSequence source, FastStringBuffer target) { |
| 106 |
FastStringBuffer buffer = new FastStringBuffer(8); |
| 107 |
boolean canonical = (form & COMPATIBILITY_MASK) == 0; |
| 108 |
int ch32; |
| 109 |
for (int i = 0; i < source.length(); ) { |
| 110 |
buffer.setLength(0); |
| 111 |
ch32 = source.charAt(i++); |
| 112 |
if (ch32 < 128) { |
| 113 |
|
| 114 |
target.append((char) ch32); |
| 115 |
continue; |
| 116 |
} |
| 117 |
if (UTF16CharacterSet.isHighSurrogate(ch32)) { |
| 118 |
char low = source.charAt(i++); |
| 119 |
ch32 = UTF16CharacterSet.combinePair((char) ch32, low); |
| 120 |
} |
| 121 |
data.getRecursiveDecomposition(canonical, ch32, buffer); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
int ch; |
| 128 |
for (int j = 0; j < buffer.length(); ) { |
| 129 |
ch = buffer.charAt(j++); |
| 130 |
if (UTF16CharacterSet.isHighSurrogate(ch)) { |
| 131 |
char low = buffer.charAt(j++); |
| 132 |
ch = UTF16CharacterSet.combinePair((char) ch, low); |
| 133 |
} |
| 134 |
int chClass = data.getCanonicalClass(ch); |
| 135 |
int k = target.length(); |
| 136 |
if (chClass != 0) { |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
int ch2; |
| 141 |
while (k > 0) { |
| 142 |
int step = 1; |
| 143 |
ch2 = target.charAt(k - 1); |
| 144 |
if (UTF16CharacterSet.isSurrogate(ch2)) { |
| 145 |
step = 2; |
| 146 |
char high = target.charAt(k - 2); |
| 147 |
ch2 = UTF16CharacterSet.combinePair(high, (char) ch2); |
| 148 |
} |
| 149 |
if (data.getCanonicalClass(ch2) <= chClass) break; |
| 150 |
k -= step; |
| 151 |
} |
| 152 |
} |
| 153 |
if (ch < 65536) { |
| 154 |
target.insert(k, (char) ch); |
| 155 |
} else { |
| 156 |
target.insertWideChar(k, ch); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private void internalCompose(FastStringBuffer target) { |
| 170 |
|
| 171 |
int starterPos = 0; |
| 172 |
|
| 173 |
|
| 174 |
int starterCh = target.charAt(0); |
| 175 |
int compPos = 1; |
| 176 |
if (UTF16CharacterSet.isHighSurrogate(starterCh)) { |
| 177 |
starterCh = UTF16CharacterSet.combinePair((char) starterCh, target.charAt(1)); |
| 178 |
compPos++; |
| 179 |
} |
| 180 |
int lastClass = data.getCanonicalClass(starterCh); |
| 181 |
if (lastClass != 0) lastClass = 256; |
| 182 |
int oldLen = target.length(); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
int ch; |
| 187 |
|
| 188 |
for (int decompPos = compPos; decompPos < target.length(); ) { |
| 189 |
ch = target.charAt(decompPos++); |
| 190 |
if (UTF16CharacterSet.isHighSurrogate(ch)) { |
| 191 |
ch = UTF16CharacterSet.combinePair((char) ch, target.charAt(decompPos++)); |
| 192 |
} |
| 193 |
|
| 194 |
int chClass = data.getCanonicalClass(ch); |
| 195 |
int composite = data.getPairwiseComposition(starterCh, ch); |
| 196 |
if (composite != NormalizerData.NOT_COMPOSITE && (lastClass < chClass || lastClass == 0)) { |
| 197 |
setCharAt(target, starterPos, composite); |
| 198 |
|
| 199 |
|
| 200 |
starterCh = composite; |
| 201 |
} else { |
| 202 |
if (chClass == 0) { |
| 203 |
starterPos = compPos; |
| 204 |
starterCh = ch; |
| 205 |
} |
| 206 |
lastClass = chClass; |
| 207 |
setCharAt(target, compPos, ch); |
| 208 |
if (target.length() != oldLen) { |
| 209 |
decompPos += target.length() - oldLen; |
| 210 |
oldLen = target.length(); |
| 211 |
} |
| 212 |
compPos += (ch < 65536 ? 1 : 2); |
| 213 |
} |
| 214 |
} |
| 215 |
target.setLength(compPos); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
private static void setCharAt(FastStringBuffer target, int offset, int ch32) { |
| 229 |
if (ch32 < 65536) { |
| 230 |
if (UTF16CharacterSet.isHighSurrogate(target.charAt(offset))) { |
| 231 |
target.setCharAt(offset, (char) ch32); |
| 232 |
target.removeCharAt(offset + 1); |
| 233 |
} else { |
| 234 |
target.setCharAt(offset, (char) ch32); |
| 235 |
} |
| 236 |
} else { |
| 237 |
if (UTF16CharacterSet.isHighSurrogate(target.charAt(offset))) { |
| 238 |
target.setCharAt(offset, UTF16CharacterSet.highSurrogate(ch32)); |
| 239 |
target.setCharAt(offset + 1, UTF16CharacterSet.lowSurrogate(ch32)); |
| 240 |
} else { |
| 241 |
target.setCharAt(offset, UTF16CharacterSet.highSurrogate(ch32)); |
| 242 |
target.insert(offset + 1, UTF16CharacterSet.lowSurrogate(ch32)); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
private static NormalizerData data = null; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|