Debian Codegraph @

net/sf/saxon/serialize/codenorm/Normalizer.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * Implements Unicode Normalization Forms C, D, KC, KD.
17 * Copyright (c) 1991-2005 Unicode, Inc.
18 * For terms of use, see http://www.unicode.org/terms_of_use.html
19 * For documentation, see UAX#15.<br>
20 * The Unicode Consortium makes no expressed or implied warranty of any
21 * kind, and assumes no liability for errors or omissions.
22 * No liability is assumed for incidental and consequential damages
23 * in connection with or arising out of the use of the information here.
24 *
25 * @author Mark Davis
26 * Updates for supplementary code points: Vladimir Weinstein &amp; Markus Scherer
27 * Modified to remove dependency on ICU code: Michael Kay
28 */
29
30 public class Normalizer {
31
32 /**
33 * The requested normalization form.
34 */
35 private int form;
36
37
38 /**
39 * Create a normalizer for a given form.
40 *
41 * @param form the normalization form required: for example {@link Normalizer#C}, {@link Normalizer#D}
42 * @param config the Saxon configuration
43 * @throws XPathException if normalization fails
44 */
45 public Normalizer(int form, Configuration config) throws XPathException {
46 this.form = form;
47 if (data == null) {
48 //data = UnicodeDataParser.build(); // load 1st time
49 data = UnicodeDataParserFromXML.build(config);
50 }
51 }
52
53 /**
54 * Masks for the form selector
55 */
56 static final int
57 COMPATIBILITY_MASK = 1,
58 COMPOSITION_MASK = 2;
59
60 /**
61 * Normalization Form Selector
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 * Normalizes text according to the chosen form
72 *
73 * @param source the original text, unnormalized
74 * @return target the resulting normalized text
75 */
76 public CharSequence normalize(CharSequence source) {
77
78 if (form == NO_ACTION || source.length() == 0) {
79 return source;
80 }
81
82 // First decompose the source into target,
83 // then compose if the form requires.
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 * Decomposes text, either canonical or compatibility,
96 * replacing contents of the target buffer.
97 * // * @param form the normalization form. If COMPATIBILITY_MASK
98 * // * bit is on in this byte, then selects the recursive
99 * // * compatibility decomposition, otherwise selects
100 * // * the recursive canonical decomposition.
101 *
102 * @param source the original text, unnormalized
103 * @param target the resulting normalized text
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 // fast path - for ASCII characters, decomposition is a no-op
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 // add all of the characters in the decomposition.
124 // (may be just the original character, if there was
125 // no decomposition mapping)
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(); // insertion point
136 if (chClass != 0) {
137
138 // bubble-sort combining marks as necessary
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 * Composes text in place. Target must already
164 * have been decomposed.
165 *
166 * @param target input: decomposed text.
167 * output: the resulting normalized text.
168 */
169 private void internalCompose(FastStringBuffer target) {
170
171 int starterPos = 0;
172 //int starterCh = UTF16.charAt(target,0);
173 //int compPos = (starterCh<65536 ? 1 : 2); // length of last composition
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; // fix for strings staring with a combining mark
182 int oldLen = target.length();
183
184 // Loop on the decomposed characters, combining where possible
185
186 int ch;
187 //for (int decompPos = compPos; decompPos < target.length(); decompPos += (ch<65536 ? 1 : 2)) {
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 //ch = UTF16.charAt(target, decompPos);
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 // we know that we will only be replacing non-supplementaries by non-supplementaries
199 // so we don't have to adjust the decompPos
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) { // MAY HAVE TO ADJUST!
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 * Set the 32-bit character at a particular 16-bit offset in a string buffer,
220 * replacing the previous character at that position, and taking account of the
221 * fact that either, both, or neither of the characters might be a surrogate pair.
222 *
223 * @param target the StringBuffer in which the data is to be inserted
224 * @param offset the position at which the data is to be inserted
225 * @param ch32 the character to be inserted, as a 32-bit Unicode codepoint
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 * Contains normalization data from the Unicode Character Database.
249 */
250 /*@Nullable*/ private static NormalizerData data = null;
251
252 /**
253 * Just accessible for testing.
254 * @param ch a character
255 * @return true if the character is an excluded character
256 */
257 // boolean getExcluded (char ch) {
258 // return data.getExcluded(ch);
259 // }
260
261 /**
262 * Just accessible for testing.
263 * @param ch a character
264 * @return the raw decomposition mapping of the character
265 */
266 // String getRawDecompositionMapping (char ch) {
267 // return data.getRawDecompositionMapping(ch);
268 // }
269 }
270
271 // * The class is derived from the sample program Normalizer.java published by the
272 // * Unicode consortium.
273
274 // * Updates for supplementary code points: Vladimir Weinstein & Markus Scherer
275 // * Modified to remove dependency on ICU code: Michael Kay, Saxonica Limited