author | alanb |
Thu, 18 Aug 2011 16:47:20 +0100 | |
changeset 10347 | 1c9efe1ec7d3 |
parent 7511 | 54a6d9105e27 |
child 19054 | a64012cb49d6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7511
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
2 |
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved |
|
28 |
* (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved |
|
29 |
* |
|
30 |
* The original version of this source code and documentation is copyrighted |
|
31 |
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These |
|
32 |
* materials are provided under terms of a License Agreement between Taligent |
|
33 |
* and Sun. This technology is protected by multiple US and International |
|
34 |
* patents. This notice and attribution to Taligent may not be removed. |
|
35 |
* Taligent is a registered trademark of Taligent, Inc. |
|
36 |
* |
|
37 |
*/ |
|
38 |
||
39 |
package java.text; |
|
40 |
||
41 |
import java.text.Normalizer; |
|
42 |
import java.util.Vector; |
|
43 |
import java.util.Locale; |
|
44 |
||
45 |
/** |
|
46 |
* The <code>RuleBasedCollator</code> class is a concrete subclass of |
|
47 |
* <code>Collator</code> that provides a simple, data-driven, table |
|
48 |
* collator. With this class you can create a customized table-based |
|
49 |
* <code>Collator</code>. <code>RuleBasedCollator</code> maps |
|
50 |
* characters to sort keys. |
|
51 |
* |
|
52 |
* <p> |
|
53 |
* <code>RuleBasedCollator</code> has the following restrictions |
|
54 |
* for efficiency (other subclasses may be used for more complex languages) : |
|
55 |
* <ol> |
|
56 |
* <li>If a special collation rule controlled by a <modifier> is |
|
57 |
specified it applies to the whole collator object. |
|
58 |
* <li>All non-mentioned characters are at the end of the |
|
59 |
* collation order. |
|
60 |
* </ol> |
|
61 |
* |
|
62 |
* <p> |
|
63 |
* The collation table is composed of a list of collation rules, where each |
|
64 |
* rule is of one of three forms: |
|
65 |
* <pre> |
|
66 |
* <modifier> |
|
67 |
* <relation> <text-argument> |
|
68 |
* <reset> <text-argument> |
|
69 |
* </pre> |
|
70 |
* The definitions of the rule elements is as follows: |
|
71 |
* <UL Type=disc> |
|
72 |
* <LI><strong>Text-Argument</strong>: A text-argument is any sequence of |
|
73 |
* characters, excluding special characters (that is, common |
|
74 |
* whitespace characters [0009-000D, 0020] and rule syntax characters |
|
75 |
* [0021-002F, 003A-0040, 005B-0060, 007B-007E]). If those |
|
76 |
* characters are desired, you can put them in single quotes |
|
77 |
* (e.g. ampersand => '&'). Note that unquoted white space characters |
|
78 |
* are ignored; e.g. <code>b c</code> is treated as <code>bc</code>. |
|
79 |
* <LI><strong>Modifier</strong>: There are currently two modifiers that |
|
80 |
* turn on special collation rules. |
|
81 |
* <UL Type=square> |
|
82 |
* <LI>'@' : Turns on backwards sorting of accents (secondary |
|
83 |
* differences), as in French. |
|
84 |
* <LI>'!' : Turns on Thai/Lao vowel-consonant swapping. If this |
|
85 |
* rule is in force when a Thai vowel of the range |
|
86 |
* \U0E40-\U0E44 precedes a Thai consonant of the range |
|
87 |
* \U0E01-\U0E2E OR a Lao vowel of the range \U0EC0-\U0EC4 |
|
88 |
* precedes a Lao consonant of the range \U0E81-\U0EAE then |
|
89 |
* the vowel is placed after the consonant for collation |
|
90 |
* purposes. |
|
91 |
* </UL> |
|
92 |
* <p>'@' : Indicates that accents are sorted backwards, as in French. |
|
93 |
* <LI><strong>Relation</strong>: The relations are the following: |
|
94 |
* <UL Type=square> |
|
95 |
* <LI>'<' : Greater, as a letter difference (primary) |
|
96 |
* <LI>';' : Greater, as an accent difference (secondary) |
|
97 |
* <LI>',' : Greater, as a case difference (tertiary) |
|
98 |
* <LI>'=' : Equal |
|
99 |
* </UL> |
|
100 |
* <LI><strong>Reset</strong>: There is a single reset |
|
101 |
* which is used primarily for contractions and expansions, but which |
|
102 |
* can also be used to add a modification at the end of a set of rules. |
|
103 |
* <p>'&' : Indicates that the next rule follows the position to where |
|
104 |
* the reset text-argument would be sorted. |
|
105 |
* </UL> |
|
106 |
* |
|
107 |
* <p> |
|
108 |
* This sounds more complicated than it is in practice. For example, the |
|
109 |
* following are equivalent ways of expressing the same thing: |
|
110 |
* <blockquote> |
|
111 |
* <pre> |
|
112 |
* a < b < c |
|
113 |
* a < b & b < c |
|
114 |
* a < c & a < b |
|
115 |
* </pre> |
|
116 |
* </blockquote> |
|
117 |
* Notice that the order is important, as the subsequent item goes immediately |
|
118 |
* after the text-argument. The following are not equivalent: |
|
119 |
* <blockquote> |
|
120 |
* <pre> |
|
121 |
* a < b & a < c |
|
122 |
* a < c & a < b |
|
123 |
* </pre> |
|
124 |
* </blockquote> |
|
125 |
* Either the text-argument must already be present in the sequence, or some |
|
126 |
* initial substring of the text-argument must be present. (e.g. "a < b & ae < |
|
127 |
* e" is valid since "a" is present in the sequence before "ae" is reset). In |
|
128 |
* this latter case, "ae" is not entered and treated as a single character; |
|
129 |
* instead, "e" is sorted as if it were expanded to two characters: "a" |
|
130 |
* followed by an "e". This difference appears in natural languages: in |
|
131 |
* traditional Spanish "ch" is treated as though it contracts to a single |
|
132 |
* character (expressed as "c < ch < d"), while in traditional German |
|
133 |
* a-umlaut is treated as though it expanded to two characters |
|
134 |
* (expressed as "a,A < b,B ... &ae;\u00e3&AE;\u00c3"). |
|
135 |
* [\u00e3 and \u00c3 are, of course, the escape sequences for a-umlaut.] |
|
136 |
* <p> |
|
137 |
* <strong>Ignorable Characters</strong> |
|
138 |
* <p> |
|
139 |
* For ignorable characters, the first rule must start with a relation (the |
|
140 |
* examples we have used above are really fragments; "a < b" really should be |
|
141 |
* "< a < b"). If, however, the first relation is not "<", then all the all |
|
142 |
* text-arguments up to the first "<" are ignorable. For example, ", - < a < b" |
|
143 |
* makes "-" an ignorable character, as we saw earlier in the word |
|
144 |
* "black-birds". In the samples for different languages, you see that most |
|
145 |
* accents are ignorable. |
|
146 |
* |
|
147 |
* <p><strong>Normalization and Accents</strong> |
|
148 |
* <p> |
|
149 |
* <code>RuleBasedCollator</code> automatically processes its rule table to |
|
150 |
* include both pre-composed and combining-character versions of |
|
151 |
* accented characters. Even if the provided rule string contains only |
|
152 |
* base characters and separate combining accent characters, the pre-composed |
|
153 |
* accented characters matching all canonical combinations of characters from |
|
154 |
* the rule string will be entered in the table. |
|
155 |
* <p> |
|
156 |
* This allows you to use a RuleBasedCollator to compare accented strings |
|
157 |
* even when the collator is set to NO_DECOMPOSITION. There are two caveats, |
|
158 |
* however. First, if the strings to be collated contain combining |
|
159 |
* sequences that may not be in canonical order, you should set the collator to |
|
160 |
* CANONICAL_DECOMPOSITION or FULL_DECOMPOSITION to enable sorting of |
|
161 |
* combining sequences. Second, if the strings contain characters with |
|
162 |
* compatibility decompositions (such as full-width and half-width forms), |
|
163 |
* you must use FULL_DECOMPOSITION, since the rule tables only include |
|
164 |
* canonical mappings. |
|
165 |
* |
|
166 |
* <p><strong>Errors</strong> |
|
167 |
* <p> |
|
168 |
* The following are errors: |
|
169 |
* <UL Type=disc> |
|
170 |
* <LI>A text-argument contains unquoted punctuation symbols |
|
171 |
* (e.g. "a < b-c < d"). |
|
172 |
* <LI>A relation or reset character not followed by a text-argument |
|
173 |
* (e.g. "a < ,b"). |
|
174 |
* <LI>A reset where the text-argument (or an initial substring of the |
|
175 |
* text-argument) is not already in the sequence. |
|
176 |
* (e.g. "a < b & e < f") |
|
177 |
* </UL> |
|
178 |
* If you produce one of these errors, a <code>RuleBasedCollator</code> throws |
|
179 |
* a <code>ParseException</code>. |
|
180 |
* |
|
181 |
* <p><strong>Examples</strong> |
|
182 |
* <p>Simple: "< a < b < c < d" |
|
7511
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
183 |
* <p>Norwegian: "< a, A < b, B < c, C < d, D < e, E < f, F |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
184 |
* < g, G < h, H < i, I < j, J < k, K < l, L |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
185 |
* < m, M < n, N < o, O < p, P < q, Q < r, R |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
186 |
* < s, S < t, T < u, U < v, V < w, W < x, X |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
187 |
* < y, Y < z, Z |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
188 |
* < \u00E6, \u00C6 |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
189 |
* < \u00F8, \u00D8 |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
190 |
* < \u00E5 = a\u030A, \u00C5 = A\u030A; |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
191 |
* aa, AA" |
2 | 192 |
* |
193 |
* <p> |
|
194 |
* To create a <code>RuleBasedCollator</code> object with specialized |
|
195 |
* rules tailored to your needs, you construct the <code>RuleBasedCollator</code> |
|
196 |
* with the rules contained in a <code>String</code> object. For example: |
|
197 |
* <blockquote> |
|
198 |
* <pre> |
|
199 |
* String simple = "< a< b< c< d"; |
|
200 |
* RuleBasedCollator mySimple = new RuleBasedCollator(simple); |
|
201 |
* </pre> |
|
202 |
* </blockquote> |
|
203 |
* Or: |
|
204 |
* <blockquote> |
|
205 |
* <pre> |
|
7511
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
206 |
* String Norwegian = "< a, A < b, B < c, C < d, D < e, E < f, F < g, G < h, H < i, I" + |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
207 |
* "< j, J < k, K < l, L < m, M < n, N < o, O < p, P < q, Q < r, R" + |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
208 |
* "< s, S < t, T < u, U < v, V < w, W < x, X < y, Y < z, Z" + |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
209 |
* "< \u00E6, \u00C6" + // Latin letter ae & AE |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
210 |
* "< \u00F8, \u00D8" + // Latin letter o & O with stroke |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
211 |
* "< \u00E5 = a\u030A," + // Latin letter a with ring above |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
212 |
* " \u00C5 = A\u030A;" + // Latin letter A with ring above |
54a6d9105e27
6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents:
6834
diff
changeset
|
213 |
* " aa, AA"; |
2 | 214 |
* RuleBasedCollator myNorwegian = new RuleBasedCollator(Norwegian); |
215 |
* </pre> |
|
216 |
* </blockquote> |
|
217 |
* |
|
218 |
* <p> |
|
219 |
* A new collation rules string can be created by concatenating rules |
|
220 |
* strings. For example, the rules returned by {@link #getRules()} could |
|
221 |
* be concatenated to combine multiple <code>RuleBasedCollator</code>s. |
|
222 |
* |
|
223 |
* <p> |
|
224 |
* The following example demonstrates how to change the order of |
|
225 |
* non-spacing accents, |
|
226 |
* <blockquote> |
|
227 |
* <pre> |
|
228 |
* // old rule |
|
229 |
* String oldRules = "=\u0301;\u0300;\u0302;\u0308" // main accents |
|
230 |
* + ";\u0327;\u0303;\u0304;\u0305" // main accents |
|
231 |
* + ";\u0306;\u0307;\u0309;\u030A" // main accents |
|
232 |
* + ";\u030B;\u030C;\u030D;\u030E" // main accents |
|
233 |
* + ";\u030F;\u0310;\u0311;\u0312" // main accents |
|
234 |
* + "< a , A ; ae, AE ; \u00e6 , \u00c6" |
|
235 |
* + "< b , B < c, C < e, E & C < d, D"; |
|
236 |
* // change the order of accent characters |
|
237 |
* String addOn = "& \u0300 ; \u0308 ; \u0302"; |
|
238 |
* RuleBasedCollator myCollator = new RuleBasedCollator(oldRules + addOn); |
|
239 |
* </pre> |
|
240 |
* </blockquote> |
|
241 |
* |
|
242 |
* @see Collator |
|
243 |
* @see CollationElementIterator |
|
244 |
* @author Helena Shih, Laura Werner, Richard Gillam |
|
245 |
*/ |
|
246 |
public class RuleBasedCollator extends Collator{ |
|
247 |
// IMPLEMENTATION NOTES: The implementation of the collation algorithm is |
|
248 |
// divided across three classes: RuleBasedCollator, RBCollationTables, and |
|
249 |
// CollationElementIterator. RuleBasedCollator contains the collator's |
|
250 |
// transient state and includes the code that uses the other classes to |
|
251 |
// implement comparison and sort-key building. RuleBasedCollator also |
|
252 |
// contains the logic to handle French secondary accent sorting. |
|
253 |
// A RuleBasedCollator has two CollationElementIterators. State doesn't |
|
254 |
// need to be preserved in these objects between calls to compare() or |
|
255 |
// getCollationKey(), but the objects persist anyway to avoid wasting extra |
|
256 |
// creation time. compare() and getCollationKey() are synchronized to ensure |
|
257 |
// thread safety with this scheme. The CollationElementIterator is responsible |
|
258 |
// for generating collation elements from strings and returning one element at |
|
259 |
// a time (sometimes there's a one-to-many or many-to-one mapping between |
|
260 |
// characters and collation elements-- this class handles that). |
|
261 |
// CollationElementIterator depends on RBCollationTables, which contains the |
|
262 |
// collator's static state. RBCollationTables contains the actual data |
|
263 |
// tables specifying the collation order of characters for a particular locale |
|
264 |
// or use. It also contains the base logic that CollationElementIterator |
|
265 |
// uses to map from characters to collation elements. A single RBCollationTables |
|
266 |
// object is shared among all RuleBasedCollators for the same locale, and |
|
267 |
// thus by all the CollationElementIterators they create. |
|
268 |
||
269 |
/** |
|
270 |
* RuleBasedCollator constructor. This takes the table rules and builds |
|
271 |
* a collation table out of them. Please see RuleBasedCollator class |
|
272 |
* description for more details on the collation rule syntax. |
|
273 |
* @see java.util.Locale |
|
274 |
* @param rules the collation rules to build the collation table from. |
|
275 |
* @exception ParseException A format exception |
|
276 |
* will be thrown if the build process of the rules fails. For |
|
277 |
* example, build rule "a < ? < d" will cause the constructor to |
|
278 |
* throw the ParseException because the '?' is not quoted. |
|
279 |
*/ |
|
280 |
public RuleBasedCollator(String rules) throws ParseException { |
|
281 |
this(rules, Collator.CANONICAL_DECOMPOSITION); |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* RuleBasedCollator constructor. This takes the table rules and builds |
|
286 |
* a collation table out of them. Please see RuleBasedCollator class |
|
287 |
* description for more details on the collation rule syntax. |
|
288 |
* @see java.util.Locale |
|
289 |
* @param rules the collation rules to build the collation table from. |
|
290 |
* @param decomp the decomposition strength used to build the |
|
291 |
* collation table and to perform comparisons. |
|
292 |
* @exception ParseException A format exception |
|
293 |
* will be thrown if the build process of the rules fails. For |
|
294 |
* example, build rule "a < ? < d" will cause the constructor to |
|
295 |
* throw the ParseException because the '?' is not quoted. |
|
296 |
*/ |
|
297 |
RuleBasedCollator(String rules, int decomp) throws ParseException { |
|
298 |
setStrength(Collator.TERTIARY); |
|
299 |
setDecomposition(decomp); |
|
300 |
tables = new RBCollationTables(rules, decomp); |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* "Copy constructor." Used in clone() for performance. |
|
305 |
*/ |
|
306 |
private RuleBasedCollator(RuleBasedCollator that) { |
|
307 |
setStrength(that.getStrength()); |
|
308 |
setDecomposition(that.getDecomposition()); |
|
309 |
tables = that.tables; |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* Gets the table-based rules for the collation object. |
|
314 |
* @return returns the collation rules that the table collation object |
|
315 |
* was created from. |
|
316 |
*/ |
|
317 |
public String getRules() |
|
318 |
{ |
|
319 |
return tables.getRules(); |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* Return a CollationElementIterator for the given String. |
|
324 |
* @see java.text.CollationElementIterator |
|
325 |
*/ |
|
326 |
public CollationElementIterator getCollationElementIterator(String source) { |
|
327 |
return new CollationElementIterator( source, this ); |
|
328 |
} |
|
329 |
||
330 |
/** |
|
331 |
* Return a CollationElementIterator for the given String. |
|
332 |
* @see java.text.CollationElementIterator |
|
333 |
* @since 1.2 |
|
334 |
*/ |
|
335 |
public CollationElementIterator getCollationElementIterator( |
|
336 |
CharacterIterator source) { |
|
337 |
return new CollationElementIterator( source, this ); |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* Compares the character data stored in two different strings based on the |
|
342 |
* collation rules. Returns information about whether a string is less |
|
343 |
* than, greater than or equal to another string in a language. |
|
344 |
* This can be overriden in a subclass. |
|
6834
303c8cca4a31
6970930: RuleBasedCollator.compare(String,null) throws IAE (should be NPE)
peytoia
parents:
5506
diff
changeset
|
345 |
* |
303c8cca4a31
6970930: RuleBasedCollator.compare(String,null) throws IAE (should be NPE)
peytoia
parents:
5506
diff
changeset
|
346 |
* @exception NullPointerException if <code>source</code> or <code>target</code> is null. |
2 | 347 |
*/ |
348 |
public synchronized int compare(String source, String target) |
|
349 |
{ |
|
6834
303c8cca4a31
6970930: RuleBasedCollator.compare(String,null) throws IAE (should be NPE)
peytoia
parents:
5506
diff
changeset
|
350 |
if (source == null || target == null) { |
303c8cca4a31
6970930: RuleBasedCollator.compare(String,null) throws IAE (should be NPE)
peytoia
parents:
5506
diff
changeset
|
351 |
throw new NullPointerException(); |
303c8cca4a31
6970930: RuleBasedCollator.compare(String,null) throws IAE (should be NPE)
peytoia
parents:
5506
diff
changeset
|
352 |
} |
303c8cca4a31
6970930: RuleBasedCollator.compare(String,null) throws IAE (should be NPE)
peytoia
parents:
5506
diff
changeset
|
353 |
|
2 | 354 |
// The basic algorithm here is that we use CollationElementIterators |
355 |
// to step through both the source and target strings. We compare each |
|
356 |
// collation element in the source string against the corresponding one |
|
357 |
// in the target, checking for differences. |
|
358 |
// |
|
359 |
// If a difference is found, we set <result> to LESS or GREATER to |
|
360 |
// indicate whether the source string is less or greater than the target. |
|
361 |
// |
|
362 |
// However, it's not that simple. If we find a tertiary difference |
|
363 |
// (e.g. 'A' vs. 'a') near the beginning of a string, it can be |
|
364 |
// overridden by a primary difference (e.g. "A" vs. "B") later in |
|
365 |
// the string. For example, "AA" < "aB", even though 'A' > 'a'. |
|
366 |
// |
|
367 |
// To keep track of this, we use strengthResult to keep track of the |
|
368 |
// strength of the most significant difference that has been found |
|
369 |
// so far. When we find a difference whose strength is greater than |
|
370 |
// strengthResult, it overrides the last difference (if any) that |
|
371 |
// was found. |
|
372 |
||
373 |
int result = Collator.EQUAL; |
|
374 |
||
375 |
if (sourceCursor == null) { |
|
376 |
sourceCursor = getCollationElementIterator(source); |
|
377 |
} else { |
|
378 |
sourceCursor.setText(source); |
|
379 |
} |
|
380 |
if (targetCursor == null) { |
|
381 |
targetCursor = getCollationElementIterator(target); |
|
382 |
} else { |
|
383 |
targetCursor.setText(target); |
|
384 |
} |
|
385 |
||
386 |
int sOrder = 0, tOrder = 0; |
|
387 |
||
388 |
boolean initialCheckSecTer = getStrength() >= Collator.SECONDARY; |
|
389 |
boolean checkSecTer = initialCheckSecTer; |
|
390 |
boolean checkTertiary = getStrength() >= Collator.TERTIARY; |
|
391 |
||
392 |
boolean gets = true, gett = true; |
|
393 |
||
394 |
while(true) { |
|
395 |
// Get the next collation element in each of the strings, unless |
|
396 |
// we've been requested to skip it. |
|
397 |
if (gets) sOrder = sourceCursor.next(); else gets = true; |
|
398 |
if (gett) tOrder = targetCursor.next(); else gett = true; |
|
399 |
||
400 |
// If we've hit the end of one of the strings, jump out of the loop |
|
401 |
if ((sOrder == CollationElementIterator.NULLORDER)|| |
|
402 |
(tOrder == CollationElementIterator.NULLORDER)) |
|
403 |
break; |
|
404 |
||
405 |
int pSOrder = CollationElementIterator.primaryOrder(sOrder); |
|
406 |
int pTOrder = CollationElementIterator.primaryOrder(tOrder); |
|
407 |
||
408 |
// If there's no difference at this position, we can skip it |
|
409 |
if (sOrder == tOrder) { |
|
410 |
if (tables.isFrenchSec() && pSOrder != 0) { |
|
411 |
if (!checkSecTer) { |
|
412 |
// in french, a secondary difference more to the right is stronger, |
|
413 |
// so accents have to be checked with each base element |
|
414 |
checkSecTer = initialCheckSecTer; |
|
415 |
// but tertiary differences are less important than the first |
|
416 |
// secondary difference, so checking tertiary remains disabled |
|
417 |
checkTertiary = false; |
|
418 |
} |
|
419 |
} |
|
420 |
continue; |
|
421 |
} |
|
422 |
||
423 |
// Compare primary differences first. |
|
424 |
if ( pSOrder != pTOrder ) |
|
425 |
{ |
|
426 |
if (sOrder == 0) { |
|
427 |
// The entire source element is ignorable. |
|
428 |
// Skip to the next source element, but don't fetch another target element. |
|
429 |
gett = false; |
|
430 |
continue; |
|
431 |
} |
|
432 |
if (tOrder == 0) { |
|
433 |
gets = false; |
|
434 |
continue; |
|
435 |
} |
|
436 |
||
437 |
// The source and target elements aren't ignorable, but it's still possible |
|
438 |
// for the primary component of one of the elements to be ignorable.... |
|
439 |
||
440 |
if (pSOrder == 0) // primary order in source is ignorable |
|
441 |
{ |
|
442 |
// The source's primary is ignorable, but the target's isn't. We treat ignorables |
|
443 |
// as a secondary difference, so remember that we found one. |
|
444 |
if (checkSecTer) { |
|
445 |
result = Collator.GREATER; // (strength is SECONDARY) |
|
446 |
checkSecTer = false; |
|
447 |
} |
|
448 |
// Skip to the next source element, but don't fetch another target element. |
|
449 |
gett = false; |
|
450 |
} |
|
451 |
else if (pTOrder == 0) |
|
452 |
{ |
|
453 |
// record differences - see the comment above. |
|
454 |
if (checkSecTer) { |
|
455 |
result = Collator.LESS; // (strength is SECONDARY) |
|
456 |
checkSecTer = false; |
|
457 |
} |
|
458 |
// Skip to the next source element, but don't fetch another target element. |
|
459 |
gets = false; |
|
460 |
} else { |
|
461 |
// Neither of the orders is ignorable, and we already know that the primary |
|
462 |
// orders are different because of the (pSOrder != pTOrder) test above. |
|
463 |
// Record the difference and stop the comparison. |
|
464 |
if (pSOrder < pTOrder) { |
|
465 |
return Collator.LESS; // (strength is PRIMARY) |
|
466 |
} else { |
|
467 |
return Collator.GREATER; // (strength is PRIMARY) |
|
468 |
} |
|
469 |
} |
|
470 |
} else { // else of if ( pSOrder != pTOrder ) |
|
471 |
// primary order is the same, but complete order is different. So there |
|
472 |
// are no base elements at this point, only ignorables (Since the strings are |
|
473 |
// normalized) |
|
474 |
||
475 |
if (checkSecTer) { |
|
476 |
// a secondary or tertiary difference may still matter |
|
477 |
short secSOrder = CollationElementIterator.secondaryOrder(sOrder); |
|
478 |
short secTOrder = CollationElementIterator.secondaryOrder(tOrder); |
|
479 |
if (secSOrder != secTOrder) { |
|
480 |
// there is a secondary difference |
|
481 |
result = (secSOrder < secTOrder) ? Collator.LESS : Collator.GREATER; |
|
482 |
// (strength is SECONDARY) |
|
483 |
checkSecTer = false; |
|
484 |
// (even in french, only the first secondary difference within |
|
485 |
// a base character matters) |
|
486 |
} else { |
|
487 |
if (checkTertiary) { |
|
488 |
// a tertiary difference may still matter |
|
489 |
short terSOrder = CollationElementIterator.tertiaryOrder(sOrder); |
|
490 |
short terTOrder = CollationElementIterator.tertiaryOrder(tOrder); |
|
491 |
if (terSOrder != terTOrder) { |
|
492 |
// there is a tertiary difference |
|
493 |
result = (terSOrder < terTOrder) ? Collator.LESS : Collator.GREATER; |
|
494 |
// (strength is TERTIARY) |
|
495 |
checkTertiary = false; |
|
496 |
} |
|
497 |
} |
|
498 |
} |
|
499 |
} // if (checkSecTer) |
|
500 |
||
501 |
} // if ( pSOrder != pTOrder ) |
|
502 |
} // while() |
|
503 |
||
504 |
if (sOrder != CollationElementIterator.NULLORDER) { |
|
505 |
// (tOrder must be CollationElementIterator::NULLORDER, |
|
506 |
// since this point is only reached when sOrder or tOrder is NULLORDER.) |
|
507 |
// The source string has more elements, but the target string hasn't. |
|
508 |
do { |
|
509 |
if (CollationElementIterator.primaryOrder(sOrder) != 0) { |
|
510 |
// We found an additional non-ignorable base character in the source string. |
|
511 |
// This is a primary difference, so the source is greater |
|
512 |
return Collator.GREATER; // (strength is PRIMARY) |
|
513 |
} |
|
514 |
else if (CollationElementIterator.secondaryOrder(sOrder) != 0) { |
|
515 |
// Additional secondary elements mean the source string is greater |
|
516 |
if (checkSecTer) { |
|
517 |
result = Collator.GREATER; // (strength is SECONDARY) |
|
518 |
checkSecTer = false; |
|
519 |
} |
|
520 |
} |
|
521 |
} while ((sOrder = sourceCursor.next()) != CollationElementIterator.NULLORDER); |
|
522 |
} |
|
523 |
else if (tOrder != CollationElementIterator.NULLORDER) { |
|
524 |
// The target string has more elements, but the source string hasn't. |
|
525 |
do { |
|
526 |
if (CollationElementIterator.primaryOrder(tOrder) != 0) |
|
527 |
// We found an additional non-ignorable base character in the target string. |
|
528 |
// This is a primary difference, so the source is less |
|
529 |
return Collator.LESS; // (strength is PRIMARY) |
|
530 |
else if (CollationElementIterator.secondaryOrder(tOrder) != 0) { |
|
531 |
// Additional secondary elements in the target mean the source string is less |
|
532 |
if (checkSecTer) { |
|
533 |
result = Collator.LESS; // (strength is SECONDARY) |
|
534 |
checkSecTer = false; |
|
535 |
} |
|
536 |
} |
|
537 |
} while ((tOrder = targetCursor.next()) != CollationElementIterator.NULLORDER); |
|
538 |
} |
|
539 |
||
540 |
// For IDENTICAL comparisons, we use a bitwise character comparison |
|
541 |
// as a tiebreaker if all else is equal |
|
542 |
if (result == 0 && getStrength() == IDENTICAL) { |
|
543 |
int mode = getDecomposition(); |
|
544 |
Normalizer.Form form; |
|
545 |
if (mode == CANONICAL_DECOMPOSITION) { |
|
546 |
form = Normalizer.Form.NFD; |
|
547 |
} else if (mode == FULL_DECOMPOSITION) { |
|
548 |
form = Normalizer.Form.NFKD; |
|
549 |
} else { |
|
550 |
return source.compareTo(target); |
|
551 |
} |
|
552 |
||
553 |
String sourceDecomposition = Normalizer.normalize(source, form); |
|
554 |
String targetDecomposition = Normalizer.normalize(target, form); |
|
555 |
return sourceDecomposition.compareTo(targetDecomposition); |
|
556 |
} |
|
557 |
return result; |
|
558 |
} |
|
559 |
||
560 |
/** |
|
561 |
* Transforms the string into a series of characters that can be compared |
|
562 |
* with CollationKey.compareTo. This overrides java.text.Collator.getCollationKey. |
|
563 |
* It can be overriden in a subclass. |
|
564 |
*/ |
|
565 |
public synchronized CollationKey getCollationKey(String source) |
|
566 |
{ |
|
567 |
// |
|
568 |
// The basic algorithm here is to find all of the collation elements for each |
|
569 |
// character in the source string, convert them to a char representation, |
|
570 |
// and put them into the collation key. But it's trickier than that. |
|
571 |
// Each collation element in a string has three components: primary (A vs B), |
|
572 |
// secondary (A vs A-acute), and tertiary (A' vs a); and a primary difference |
|
573 |
// at the end of a string takes precedence over a secondary or tertiary |
|
574 |
// difference earlier in the string. |
|
575 |
// |
|
576 |
// To account for this, we put all of the primary orders at the beginning of the |
|
577 |
// string, followed by the secondary and tertiary orders, separated by nulls. |
|
578 |
// |
|
579 |
// Here's a hypothetical example, with the collation element represented as |
|
580 |
// a three-digit number, one digit for primary, one for secondary, etc. |
|
581 |
// |
|
582 |
// String: A a B \u00e9 <--(e-acute) |
|
583 |
// Collation Elements: 101 100 201 510 |
|
584 |
// |
|
585 |
// Collation Key: 1125<null>0001<null>1010 |
|
586 |
// |
|
587 |
// To make things even trickier, secondary differences (accent marks) are compared |
|
588 |
// starting at the *end* of the string in languages with French secondary ordering. |
|
589 |
// But when comparing the accent marks on a single base character, they are compared |
|
590 |
// from the beginning. To handle this, we reverse all of the accents that belong |
|
591 |
// to each base character, then we reverse the entire string of secondary orderings |
|
592 |
// at the end. Taking the same example above, a French collator might return |
|
593 |
// this instead: |
|
594 |
// |
|
595 |
// Collation Key: 1125<null>1000<null>1010 |
|
596 |
// |
|
597 |
if (source == null) |
|
598 |
return null; |
|
599 |
||
600 |
if (primResult == null) { |
|
601 |
primResult = new StringBuffer(); |
|
602 |
secResult = new StringBuffer(); |
|
603 |
terResult = new StringBuffer(); |
|
604 |
} else { |
|
605 |
primResult.setLength(0); |
|
606 |
secResult.setLength(0); |
|
607 |
terResult.setLength(0); |
|
608 |
} |
|
609 |
int order = 0; |
|
610 |
boolean compareSec = (getStrength() >= Collator.SECONDARY); |
|
611 |
boolean compareTer = (getStrength() >= Collator.TERTIARY); |
|
612 |
int secOrder = CollationElementIterator.NULLORDER; |
|
613 |
int terOrder = CollationElementIterator.NULLORDER; |
|
614 |
int preSecIgnore = 0; |
|
615 |
||
616 |
if (sourceCursor == null) { |
|
617 |
sourceCursor = getCollationElementIterator(source); |
|
618 |
} else { |
|
619 |
sourceCursor.setText(source); |
|
620 |
} |
|
621 |
||
622 |
// walk through each character |
|
623 |
while ((order = sourceCursor.next()) != |
|
624 |
CollationElementIterator.NULLORDER) |
|
625 |
{ |
|
626 |
secOrder = CollationElementIterator.secondaryOrder(order); |
|
627 |
terOrder = CollationElementIterator.tertiaryOrder(order); |
|
628 |
if (!CollationElementIterator.isIgnorable(order)) |
|
629 |
{ |
|
630 |
primResult.append((char) (CollationElementIterator.primaryOrder(order) |
|
631 |
+ COLLATIONKEYOFFSET)); |
|
632 |
||
633 |
if (compareSec) { |
|
634 |
// |
|
635 |
// accumulate all of the ignorable/secondary characters attached |
|
636 |
// to a given base character |
|
637 |
// |
|
638 |
if (tables.isFrenchSec() && preSecIgnore < secResult.length()) { |
|
639 |
// |
|
640 |
// We're doing reversed secondary ordering and we've hit a base |
|
641 |
// (non-ignorable) character. Reverse any secondary orderings |
|
642 |
// that applied to the last base character. (see block comment above.) |
|
643 |
// |
|
644 |
RBCollationTables.reverse(secResult, preSecIgnore, secResult.length()); |
|
645 |
} |
|
646 |
// Remember where we are in the secondary orderings - this is how far |
|
647 |
// back to go if we need to reverse them later. |
|
648 |
secResult.append((char)(secOrder+ COLLATIONKEYOFFSET)); |
|
649 |
preSecIgnore = secResult.length(); |
|
650 |
} |
|
651 |
if (compareTer) { |
|
652 |
terResult.append((char)(terOrder+ COLLATIONKEYOFFSET)); |
|
653 |
} |
|
654 |
} |
|
655 |
else |
|
656 |
{ |
|
657 |
if (compareSec && secOrder != 0) |
|
658 |
secResult.append((char) |
|
659 |
(secOrder + tables.getMaxSecOrder() + COLLATIONKEYOFFSET)); |
|
660 |
if (compareTer && terOrder != 0) |
|
661 |
terResult.append((char) |
|
662 |
(terOrder + tables.getMaxTerOrder() + COLLATIONKEYOFFSET)); |
|
663 |
} |
|
664 |
} |
|
665 |
if (tables.isFrenchSec()) |
|
666 |
{ |
|
667 |
if (preSecIgnore < secResult.length()) { |
|
668 |
// If we've accumlated any secondary characters after the last base character, |
|
669 |
// reverse them. |
|
670 |
RBCollationTables.reverse(secResult, preSecIgnore, secResult.length()); |
|
671 |
} |
|
672 |
// And now reverse the entire secResult to get French secondary ordering. |
|
673 |
RBCollationTables.reverse(secResult, 0, secResult.length()); |
|
674 |
} |
|
675 |
primResult.append((char)0); |
|
676 |
secResult.append((char)0); |
|
677 |
secResult.append(terResult.toString()); |
|
678 |
primResult.append(secResult.toString()); |
|
679 |
||
680 |
if (getStrength() == IDENTICAL) { |
|
681 |
primResult.append((char)0); |
|
682 |
int mode = getDecomposition(); |
|
683 |
if (mode == CANONICAL_DECOMPOSITION) { |
|
684 |
primResult.append(Normalizer.normalize(source, Normalizer.Form.NFD)); |
|
685 |
} else if (mode == FULL_DECOMPOSITION) { |
|
686 |
primResult.append(Normalizer.normalize(source, Normalizer.Form.NFKD)); |
|
687 |
} else { |
|
688 |
primResult.append(source); |
|
689 |
} |
|
690 |
} |
|
691 |
return new RuleBasedCollationKey(source, primResult.toString()); |
|
692 |
} |
|
693 |
||
694 |
/** |
|
695 |
* Standard override; no change in semantics. |
|
696 |
*/ |
|
697 |
public Object clone() { |
|
698 |
// if we know we're not actually a subclass of RuleBasedCollator |
|
699 |
// (this class really should have been made final), bypass |
|
700 |
// Object.clone() and use our "copy constructor". This is faster. |
|
701 |
if (getClass() == RuleBasedCollator.class) { |
|
702 |
return new RuleBasedCollator(this); |
|
703 |
} |
|
704 |
else { |
|
705 |
RuleBasedCollator result = (RuleBasedCollator) super.clone(); |
|
706 |
result.primResult = null; |
|
707 |
result.secResult = null; |
|
708 |
result.terResult = null; |
|
709 |
result.sourceCursor = null; |
|
710 |
result.targetCursor = null; |
|
711 |
return result; |
|
712 |
} |
|
713 |
} |
|
714 |
||
715 |
/** |
|
716 |
* Compares the equality of two collation objects. |
|
717 |
* @param obj the table-based collation object to be compared with this. |
|
718 |
* @return true if the current table-based collation object is the same |
|
719 |
* as the table-based collation object obj; false otherwise. |
|
720 |
*/ |
|
721 |
public boolean equals(Object obj) { |
|
722 |
if (obj == null) return false; |
|
723 |
if (!super.equals(obj)) return false; // super does class check |
|
724 |
RuleBasedCollator other = (RuleBasedCollator) obj; |
|
725 |
// all other non-transient information is also contained in rules. |
|
726 |
return (getRules().equals(other.getRules())); |
|
727 |
} |
|
728 |
||
729 |
/** |
|
730 |
* Generates the hash code for the table-based collation object |
|
731 |
*/ |
|
732 |
public int hashCode() { |
|
733 |
return getRules().hashCode(); |
|
734 |
} |
|
735 |
||
736 |
/** |
|
737 |
* Allows CollationElementIterator access to the tables object |
|
738 |
*/ |
|
739 |
RBCollationTables getTables() { |
|
740 |
return tables; |
|
741 |
} |
|
742 |
||
743 |
// ============================================================== |
|
744 |
// private |
|
745 |
// ============================================================== |
|
746 |
||
747 |
final static int CHARINDEX = 0x70000000; // need look up in .commit() |
|
748 |
final static int EXPANDCHARINDEX = 0x7E000000; // Expand index follows |
|
749 |
final static int CONTRACTCHARINDEX = 0x7F000000; // contract indexes follow |
|
750 |
final static int UNMAPPED = 0xFFFFFFFF; |
|
751 |
||
752 |
private final static int COLLATIONKEYOFFSET = 1; |
|
753 |
||
754 |
private RBCollationTables tables = null; |
|
755 |
||
756 |
// Internal objects that are cached across calls so that they don't have to |
|
757 |
// be created/destroyed on every call to compare() and getCollationKey() |
|
758 |
private StringBuffer primResult = null; |
|
759 |
private StringBuffer secResult = null; |
|
760 |
private StringBuffer terResult = null; |
|
761 |
private CollationElementIterator sourceCursor = null; |
|
762 |
private CollationElementIterator targetCursor = null; |
|
763 |
} |