0N/A/*
3196N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
0N/A * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
0N/A *
0N/A * The original version of this source code and documentation is copyrighted
0N/A * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
0N/A * materials are provided under terms of a License Agreement between Taligent
0N/A * and Sun. This technology is protected by multiple US and International
0N/A * patents. This notice and attribution to Taligent may not be removed.
0N/A * Taligent is a registered trademark of Taligent, Inc.
0N/A *
0N/A */
0N/A
0N/Apackage java.text;
0N/A
0N/Aimport java.text.Normalizer;
0N/Aimport java.util.Vector;
0N/Aimport java.util.Locale;
0N/A
0N/A/**
0N/A * The <code>RuleBasedCollator</code> class is a concrete subclass of
0N/A * <code>Collator</code> that provides a simple, data-driven, table
0N/A * collator. With this class you can create a customized table-based
0N/A * <code>Collator</code>. <code>RuleBasedCollator</code> maps
0N/A * characters to sort keys.
0N/A *
0N/A * <p>
0N/A * <code>RuleBasedCollator</code> has the following restrictions
0N/A * for efficiency (other subclasses may be used for more complex languages) :
0N/A * <ol>
0N/A * <li>If a special collation rule controlled by a &lt;modifier&gt; is
0N/A specified it applies to the whole collator object.
0N/A * <li>All non-mentioned characters are at the end of the
0N/A * collation order.
0N/A * </ol>
0N/A *
0N/A * <p>
0N/A * The collation table is composed of a list of collation rules, where each
0N/A * rule is of one of three forms:
0N/A * <pre>
0N/A * &lt;modifier&gt;
0N/A * &lt;relation&gt; &lt;text-argument&gt;
0N/A * &lt;reset&gt; &lt;text-argument&gt;
0N/A * </pre>
0N/A * The definitions of the rule elements is as follows:
0N/A * <UL Type=disc>
0N/A * <LI><strong>Text-Argument</strong>: A text-argument is any sequence of
0N/A * characters, excluding special characters (that is, common
0N/A * whitespace characters [0009-000D, 0020] and rule syntax characters
0N/A * [0021-002F, 003A-0040, 005B-0060, 007B-007E]). If those
0N/A * characters are desired, you can put them in single quotes
0N/A * (e.g. ampersand => '&'). Note that unquoted white space characters
0N/A * are ignored; e.g. <code>b c</code> is treated as <code>bc</code>.
0N/A * <LI><strong>Modifier</strong>: There are currently two modifiers that
0N/A * turn on special collation rules.
0N/A * <UL Type=square>
0N/A * <LI>'@' : Turns on backwards sorting of accents (secondary
0N/A * differences), as in French.
0N/A * <LI>'!' : Turns on Thai/Lao vowel-consonant swapping. If this
0N/A * rule is in force when a Thai vowel of the range
0N/A * &#92;U0E40-&#92;U0E44 precedes a Thai consonant of the range
0N/A * &#92;U0E01-&#92;U0E2E OR a Lao vowel of the range &#92;U0EC0-&#92;U0EC4
0N/A * precedes a Lao consonant of the range &#92;U0E81-&#92;U0EAE then
0N/A * the vowel is placed after the consonant for collation
0N/A * purposes.
0N/A * </UL>
0N/A * <p>'@' : Indicates that accents are sorted backwards, as in French.
0N/A * <LI><strong>Relation</strong>: The relations are the following:
0N/A * <UL Type=square>
0N/A * <LI>'&lt;' : Greater, as a letter difference (primary)
0N/A * <LI>';' : Greater, as an accent difference (secondary)
0N/A * <LI>',' : Greater, as a case difference (tertiary)
0N/A * <LI>'=' : Equal
0N/A * </UL>
0N/A * <LI><strong>Reset</strong>: There is a single reset
0N/A * which is used primarily for contractions and expansions, but which
0N/A * can also be used to add a modification at the end of a set of rules.
0N/A * <p>'&' : Indicates that the next rule follows the position to where
0N/A * the reset text-argument would be sorted.
0N/A * </UL>
0N/A *
0N/A * <p>
0N/A * This sounds more complicated than it is in practice. For example, the
0N/A * following are equivalent ways of expressing the same thing:
0N/A * <blockquote>
0N/A * <pre>
0N/A * a &lt; b &lt; c
0N/A * a &lt; b &amp; b &lt; c
0N/A * a &lt; c &amp; a &lt; b
0N/A * </pre>
0N/A * </blockquote>
0N/A * Notice that the order is important, as the subsequent item goes immediately
0N/A * after the text-argument. The following are not equivalent:
0N/A * <blockquote>
0N/A * <pre>
0N/A * a &lt; b &amp; a &lt; c
0N/A * a &lt; c &amp; a &lt; b
0N/A * </pre>
0N/A * </blockquote>
0N/A * Either the text-argument must already be present in the sequence, or some
0N/A * initial substring of the text-argument must be present. (e.g. "a &lt; b &amp; ae &lt;
0N/A * e" is valid since "a" is present in the sequence before "ae" is reset). In
0N/A * this latter case, "ae" is not entered and treated as a single character;
0N/A * instead, "e" is sorted as if it were expanded to two characters: "a"
0N/A * followed by an "e". This difference appears in natural languages: in
0N/A * traditional Spanish "ch" is treated as though it contracts to a single
0N/A * character (expressed as "c &lt; ch &lt; d"), while in traditional German
0N/A * a-umlaut is treated as though it expanded to two characters
0N/A * (expressed as "a,A &lt; b,B ... &amp;ae;&#92;u00e3&amp;AE;&#92;u00c3").
0N/A * [&#92;u00e3 and &#92;u00c3 are, of course, the escape sequences for a-umlaut.]
0N/A * <p>
0N/A * <strong>Ignorable Characters</strong>
0N/A * <p>
0N/A * For ignorable characters, the first rule must start with a relation (the
0N/A * examples we have used above are really fragments; "a &lt; b" really should be
0N/A * "&lt; a &lt; b"). If, however, the first relation is not "&lt;", then all the all
0N/A * text-arguments up to the first "&lt;" are ignorable. For example, ", - &lt; a &lt; b"
0N/A * makes "-" an ignorable character, as we saw earlier in the word
0N/A * "black-birds". In the samples for different languages, you see that most
0N/A * accents are ignorable.
0N/A *
0N/A * <p><strong>Normalization and Accents</strong>
0N/A * <p>
0N/A * <code>RuleBasedCollator</code> automatically processes its rule table to
0N/A * include both pre-composed and combining-character versions of
0N/A * accented characters. Even if the provided rule string contains only
0N/A * base characters and separate combining accent characters, the pre-composed
0N/A * accented characters matching all canonical combinations of characters from
0N/A * the rule string will be entered in the table.
0N/A * <p>
0N/A * This allows you to use a RuleBasedCollator to compare accented strings
0N/A * even when the collator is set to NO_DECOMPOSITION. There are two caveats,
0N/A * however. First, if the strings to be collated contain combining
0N/A * sequences that may not be in canonical order, you should set the collator to
0N/A * CANONICAL_DECOMPOSITION or FULL_DECOMPOSITION to enable sorting of
0N/A * combining sequences. Second, if the strings contain characters with
0N/A * compatibility decompositions (such as full-width and half-width forms),
0N/A * you must use FULL_DECOMPOSITION, since the rule tables only include
0N/A * canonical mappings.
0N/A *
0N/A * <p><strong>Errors</strong>
0N/A * <p>
0N/A * The following are errors:
0N/A * <UL Type=disc>
0N/A * <LI>A text-argument contains unquoted punctuation symbols
0N/A * (e.g. "a &lt; b-c &lt; d").
0N/A * <LI>A relation or reset character not followed by a text-argument
0N/A * (e.g. "a &lt; ,b").
0N/A * <LI>A reset where the text-argument (or an initial substring of the
0N/A * text-argument) is not already in the sequence.
0N/A * (e.g. "a &lt; b &amp; e &lt; f")
0N/A * </UL>
0N/A * If you produce one of these errors, a <code>RuleBasedCollator</code> throws
0N/A * a <code>ParseException</code>.
0N/A *
0N/A * <p><strong>Examples</strong>
0N/A * <p>Simple: "&lt; a &lt; b &lt; c &lt; d"
3196N/A * <p>Norwegian: "&lt; a, A &lt; b, B &lt; c, C &lt; d, D &lt; e, E &lt; f, F
3196N/A * &lt; g, G &lt; h, H &lt; i, I &lt; j, J &lt; k, K &lt; l, L
3196N/A * &lt; m, M &lt; n, N &lt; o, O &lt; p, P &lt; q, Q &lt; r, R
3196N/A * &lt; s, S &lt; t, T &lt; u, U &lt; v, V &lt; w, W &lt; x, X
3196N/A * &lt; y, Y &lt; z, Z
3196N/A * &lt; &#92;u00E6, &#92;u00C6
3196N/A * &lt; &#92;u00F8, &#92;u00D8
3196N/A * &lt; &#92;u00E5 = a&#92;u030A, &#92;u00C5 = A&#92;u030A;
3196N/A * aa, AA"
0N/A *
0N/A * <p>
0N/A * To create a <code>RuleBasedCollator</code> object with specialized
0N/A * rules tailored to your needs, you construct the <code>RuleBasedCollator</code>
0N/A * with the rules contained in a <code>String</code> object. For example:
0N/A * <blockquote>
0N/A * <pre>
0N/A * String simple = "&lt; a&lt; b&lt; c&lt; d";
0N/A * RuleBasedCollator mySimple = new RuleBasedCollator(simple);
0N/A * </pre>
0N/A * </blockquote>
0N/A * Or:
0N/A * <blockquote>
0N/A * <pre>
3196N/A * String Norwegian = "&lt; a, A &lt; b, B &lt; c, C &lt; d, D &lt; e, E &lt; f, F &lt; g, G &lt; h, H &lt; i, I" +
3196N/A * "&lt; j, J &lt; k, K &lt; l, L &lt; m, M &lt; n, N &lt; o, O &lt; p, P &lt; q, Q &lt; r, R" +
3196N/A * "&lt; s, S &lt; t, T &lt; u, U &lt; v, V &lt; w, W &lt; x, X &lt; y, Y &lt; z, Z" +
3196N/A * "&lt; &#92;u00E6, &#92;u00C6" + // Latin letter ae & AE
3196N/A * "&lt; &#92;u00F8, &#92;u00D8" + // Latin letter o & O with stroke
3196N/A * "&lt; &#92;u00E5 = a&#92;u030A," + // Latin letter a with ring above
3196N/A * " &#92;u00C5 = A&#92;u030A;" + // Latin letter A with ring above
3196N/A * " aa, AA";
0N/A * RuleBasedCollator myNorwegian = new RuleBasedCollator(Norwegian);
0N/A * </pre>
0N/A * </blockquote>
0N/A *
0N/A * <p>
0N/A * A new collation rules string can be created by concatenating rules
0N/A * strings. For example, the rules returned by {@link #getRules()} could
0N/A * be concatenated to combine multiple <code>RuleBasedCollator</code>s.
0N/A *
0N/A * <p>
0N/A * The following example demonstrates how to change the order of
0N/A * non-spacing accents,
0N/A * <blockquote>
0N/A * <pre>
0N/A * // old rule
0N/A * String oldRules = "=&#92;u0301;&#92;u0300;&#92;u0302;&#92;u0308" // main accents
0N/A * + ";&#92;u0327;&#92;u0303;&#92;u0304;&#92;u0305" // main accents
0N/A * + ";&#92;u0306;&#92;u0307;&#92;u0309;&#92;u030A" // main accents
0N/A * + ";&#92;u030B;&#92;u030C;&#92;u030D;&#92;u030E" // main accents
0N/A * + ";&#92;u030F;&#92;u0310;&#92;u0311;&#92;u0312" // main accents
0N/A * + "&lt; a , A ; ae, AE ; &#92;u00e6 , &#92;u00c6"
0N/A * + "&lt; b , B &lt; c, C &lt; e, E & C &lt; d, D";
0N/A * // change the order of accent characters
0N/A * String addOn = "& &#92;u0300 ; &#92;u0308 ; &#92;u0302";
0N/A * RuleBasedCollator myCollator = new RuleBasedCollator(oldRules + addOn);
0N/A * </pre>
0N/A * </blockquote>
0N/A *
0N/A * @see Collator
0N/A * @see CollationElementIterator
0N/A * @author Helena Shih, Laura Werner, Richard Gillam
0N/A */
0N/Apublic class RuleBasedCollator extends Collator{
0N/A // IMPLEMENTATION NOTES: The implementation of the collation algorithm is
0N/A // divided across three classes: RuleBasedCollator, RBCollationTables, and
0N/A // CollationElementIterator. RuleBasedCollator contains the collator's
0N/A // transient state and includes the code that uses the other classes to
0N/A // implement comparison and sort-key building. RuleBasedCollator also
0N/A // contains the logic to handle French secondary accent sorting.
0N/A // A RuleBasedCollator has two CollationElementIterators. State doesn't
0N/A // need to be preserved in these objects between calls to compare() or
0N/A // getCollationKey(), but the objects persist anyway to avoid wasting extra
0N/A // creation time. compare() and getCollationKey() are synchronized to ensure
0N/A // thread safety with this scheme. The CollationElementIterator is responsible
0N/A // for generating collation elements from strings and returning one element at
0N/A // a time (sometimes there's a one-to-many or many-to-one mapping between
0N/A // characters and collation elements-- this class handles that).
0N/A // CollationElementIterator depends on RBCollationTables, which contains the
0N/A // collator's static state. RBCollationTables contains the actual data
0N/A // tables specifying the collation order of characters for a particular locale
0N/A // or use. It also contains the base logic that CollationElementIterator
0N/A // uses to map from characters to collation elements. A single RBCollationTables
0N/A // object is shared among all RuleBasedCollators for the same locale, and
0N/A // thus by all the CollationElementIterators they create.
0N/A
0N/A /**
0N/A * RuleBasedCollator constructor. This takes the table rules and builds
0N/A * a collation table out of them. Please see RuleBasedCollator class
0N/A * description for more details on the collation rule syntax.
0N/A * @see java.util.Locale
0N/A * @param rules the collation rules to build the collation table from.
0N/A * @exception ParseException A format exception
0N/A * will be thrown if the build process of the rules fails. For
0N/A * example, build rule "a < ? < d" will cause the constructor to
0N/A * throw the ParseException because the '?' is not quoted.
0N/A */
0N/A public RuleBasedCollator(String rules) throws ParseException {
0N/A this(rules, Collator.CANONICAL_DECOMPOSITION);
0N/A }
0N/A
0N/A /**
0N/A * RuleBasedCollator constructor. This takes the table rules and builds
0N/A * a collation table out of them. Please see RuleBasedCollator class
0N/A * description for more details on the collation rule syntax.
0N/A * @see java.util.Locale
0N/A * @param rules the collation rules to build the collation table from.
0N/A * @param decomp the decomposition strength used to build the
0N/A * collation table and to perform comparisons.
0N/A * @exception ParseException A format exception
0N/A * will be thrown if the build process of the rules fails. For
0N/A * example, build rule "a < ? < d" will cause the constructor to
0N/A * throw the ParseException because the '?' is not quoted.
0N/A */
0N/A RuleBasedCollator(String rules, int decomp) throws ParseException {
0N/A setStrength(Collator.TERTIARY);
0N/A setDecomposition(decomp);
0N/A tables = new RBCollationTables(rules, decomp);
0N/A }
0N/A
0N/A /**
0N/A * "Copy constructor." Used in clone() for performance.
0N/A */
0N/A private RuleBasedCollator(RuleBasedCollator that) {
0N/A setStrength(that.getStrength());
0N/A setDecomposition(that.getDecomposition());
0N/A tables = that.tables;
0N/A }
0N/A
0N/A /**
0N/A * Gets the table-based rules for the collation object.
0N/A * @return returns the collation rules that the table collation object
0N/A * was created from.
0N/A */
0N/A public String getRules()
0N/A {
0N/A return tables.getRules();
0N/A }
0N/A
0N/A /**
0N/A * Return a CollationElementIterator for the given String.
0N/A * @see java.text.CollationElementIterator
0N/A */
0N/A public CollationElementIterator getCollationElementIterator(String source) {
0N/A return new CollationElementIterator( source, this );
0N/A }
0N/A
0N/A /**
0N/A * Return a CollationElementIterator for the given String.
0N/A * @see java.text.CollationElementIterator
0N/A * @since 1.2
0N/A */
0N/A public CollationElementIterator getCollationElementIterator(
0N/A CharacterIterator source) {
0N/A return new CollationElementIterator( source, this );
0N/A }
0N/A
0N/A /**
0N/A * Compares the character data stored in two different strings based on the
0N/A * collation rules. Returns information about whether a string is less
0N/A * than, greater than or equal to another string in a language.
0N/A * This can be overriden in a subclass.
2868N/A *
2868N/A * @exception NullPointerException if <code>source</code> or <code>target</code> is null.
0N/A */
0N/A public synchronized int compare(String source, String target)
0N/A {
2868N/A if (source == null || target == null) {
2868N/A throw new NullPointerException();
2868N/A }
2868N/A
0N/A // The basic algorithm here is that we use CollationElementIterators
0N/A // to step through both the source and target strings. We compare each
0N/A // collation element in the source string against the corresponding one
0N/A // in the target, checking for differences.
0N/A //
0N/A // If a difference is found, we set <result> to LESS or GREATER to
0N/A // indicate whether the source string is less or greater than the target.
0N/A //
0N/A // However, it's not that simple. If we find a tertiary difference
0N/A // (e.g. 'A' vs. 'a') near the beginning of a string, it can be
0N/A // overridden by a primary difference (e.g. "A" vs. "B") later in
0N/A // the string. For example, "AA" < "aB", even though 'A' > 'a'.
0N/A //
0N/A // To keep track of this, we use strengthResult to keep track of the
0N/A // strength of the most significant difference that has been found
0N/A // so far. When we find a difference whose strength is greater than
0N/A // strengthResult, it overrides the last difference (if any) that
0N/A // was found.
0N/A
0N/A int result = Collator.EQUAL;
0N/A
0N/A if (sourceCursor == null) {
0N/A sourceCursor = getCollationElementIterator(source);
0N/A } else {
0N/A sourceCursor.setText(source);
0N/A }
0N/A if (targetCursor == null) {
0N/A targetCursor = getCollationElementIterator(target);
0N/A } else {
0N/A targetCursor.setText(target);
0N/A }
0N/A
0N/A int sOrder = 0, tOrder = 0;
0N/A
0N/A boolean initialCheckSecTer = getStrength() >= Collator.SECONDARY;
0N/A boolean checkSecTer = initialCheckSecTer;
0N/A boolean checkTertiary = getStrength() >= Collator.TERTIARY;
0N/A
0N/A boolean gets = true, gett = true;
0N/A
0N/A while(true) {
0N/A // Get the next collation element in each of the strings, unless
0N/A // we've been requested to skip it.
0N/A if (gets) sOrder = sourceCursor.next(); else gets = true;
0N/A if (gett) tOrder = targetCursor.next(); else gett = true;
0N/A
0N/A // If we've hit the end of one of the strings, jump out of the loop
0N/A if ((sOrder == CollationElementIterator.NULLORDER)||
0N/A (tOrder == CollationElementIterator.NULLORDER))
0N/A break;
0N/A
0N/A int pSOrder = CollationElementIterator.primaryOrder(sOrder);
0N/A int pTOrder = CollationElementIterator.primaryOrder(tOrder);
0N/A
0N/A // If there's no difference at this position, we can skip it
0N/A if (sOrder == tOrder) {
0N/A if (tables.isFrenchSec() && pSOrder != 0) {
0N/A if (!checkSecTer) {
0N/A // in french, a secondary difference more to the right is stronger,
0N/A // so accents have to be checked with each base element
0N/A checkSecTer = initialCheckSecTer;
0N/A // but tertiary differences are less important than the first
0N/A // secondary difference, so checking tertiary remains disabled
0N/A checkTertiary = false;
0N/A }
0N/A }
0N/A continue;
0N/A }
0N/A
0N/A // Compare primary differences first.
0N/A if ( pSOrder != pTOrder )
0N/A {
0N/A if (sOrder == 0) {
0N/A // The entire source element is ignorable.
0N/A // Skip to the next source element, but don't fetch another target element.
0N/A gett = false;
0N/A continue;
0N/A }
0N/A if (tOrder == 0) {
0N/A gets = false;
0N/A continue;
0N/A }
0N/A
0N/A // The source and target elements aren't ignorable, but it's still possible
0N/A // for the primary component of one of the elements to be ignorable....
0N/A
0N/A if (pSOrder == 0) // primary order in source is ignorable
0N/A {
0N/A // The source's primary is ignorable, but the target's isn't. We treat ignorables
0N/A // as a secondary difference, so remember that we found one.
0N/A if (checkSecTer) {
0N/A result = Collator.GREATER; // (strength is SECONDARY)
0N/A checkSecTer = false;
0N/A }
0N/A // Skip to the next source element, but don't fetch another target element.
0N/A gett = false;
0N/A }
0N/A else if (pTOrder == 0)
0N/A {
0N/A // record differences - see the comment above.
0N/A if (checkSecTer) {
0N/A result = Collator.LESS; // (strength is SECONDARY)
0N/A checkSecTer = false;
0N/A }
0N/A // Skip to the next source element, but don't fetch another target element.
0N/A gets = false;
0N/A } else {
0N/A // Neither of the orders is ignorable, and we already know that the primary
0N/A // orders are different because of the (pSOrder != pTOrder) test above.
0N/A // Record the difference and stop the comparison.
0N/A if (pSOrder < pTOrder) {
0N/A return Collator.LESS; // (strength is PRIMARY)
0N/A } else {
0N/A return Collator.GREATER; // (strength is PRIMARY)
0N/A }
0N/A }
0N/A } else { // else of if ( pSOrder != pTOrder )
0N/A // primary order is the same, but complete order is different. So there
0N/A // are no base elements at this point, only ignorables (Since the strings are
0N/A // normalized)
0N/A
0N/A if (checkSecTer) {
0N/A // a secondary or tertiary difference may still matter
0N/A short secSOrder = CollationElementIterator.secondaryOrder(sOrder);
0N/A short secTOrder = CollationElementIterator.secondaryOrder(tOrder);
0N/A if (secSOrder != secTOrder) {
0N/A // there is a secondary difference
0N/A result = (secSOrder < secTOrder) ? Collator.LESS : Collator.GREATER;
0N/A // (strength is SECONDARY)
0N/A checkSecTer = false;
0N/A // (even in french, only the first secondary difference within
0N/A // a base character matters)
0N/A } else {
0N/A if (checkTertiary) {
0N/A // a tertiary difference may still matter
0N/A short terSOrder = CollationElementIterator.tertiaryOrder(sOrder);
0N/A short terTOrder = CollationElementIterator.tertiaryOrder(tOrder);
0N/A if (terSOrder != terTOrder) {
0N/A // there is a tertiary difference
0N/A result = (terSOrder < terTOrder) ? Collator.LESS : Collator.GREATER;
0N/A // (strength is TERTIARY)
0N/A checkTertiary = false;
0N/A }
0N/A }
0N/A }
0N/A } // if (checkSecTer)
0N/A
0N/A } // if ( pSOrder != pTOrder )
0N/A } // while()
0N/A
0N/A if (sOrder != CollationElementIterator.NULLORDER) {
0N/A // (tOrder must be CollationElementIterator::NULLORDER,
0N/A // since this point is only reached when sOrder or tOrder is NULLORDER.)
0N/A // The source string has more elements, but the target string hasn't.
0N/A do {
0N/A if (CollationElementIterator.primaryOrder(sOrder) != 0) {
0N/A // We found an additional non-ignorable base character in the source string.
0N/A // This is a primary difference, so the source is greater
0N/A return Collator.GREATER; // (strength is PRIMARY)
0N/A }
0N/A else if (CollationElementIterator.secondaryOrder(sOrder) != 0) {
0N/A // Additional secondary elements mean the source string is greater
0N/A if (checkSecTer) {
0N/A result = Collator.GREATER; // (strength is SECONDARY)
0N/A checkSecTer = false;
0N/A }
0N/A }
0N/A } while ((sOrder = sourceCursor.next()) != CollationElementIterator.NULLORDER);
0N/A }
0N/A else if (tOrder != CollationElementIterator.NULLORDER) {
0N/A // The target string has more elements, but the source string hasn't.
0N/A do {
0N/A if (CollationElementIterator.primaryOrder(tOrder) != 0)
0N/A // We found an additional non-ignorable base character in the target string.
0N/A // This is a primary difference, so the source is less
0N/A return Collator.LESS; // (strength is PRIMARY)
0N/A else if (CollationElementIterator.secondaryOrder(tOrder) != 0) {
0N/A // Additional secondary elements in the target mean the source string is less
0N/A if (checkSecTer) {
0N/A result = Collator.LESS; // (strength is SECONDARY)
0N/A checkSecTer = false;
0N/A }
0N/A }
0N/A } while ((tOrder = targetCursor.next()) != CollationElementIterator.NULLORDER);
0N/A }
0N/A
0N/A // For IDENTICAL comparisons, we use a bitwise character comparison
0N/A // as a tiebreaker if all else is equal
0N/A if (result == 0 && getStrength() == IDENTICAL) {
0N/A int mode = getDecomposition();
0N/A Normalizer.Form form;
0N/A if (mode == CANONICAL_DECOMPOSITION) {
0N/A form = Normalizer.Form.NFD;
0N/A } else if (mode == FULL_DECOMPOSITION) {
0N/A form = Normalizer.Form.NFKD;
0N/A } else {
0N/A return source.compareTo(target);
0N/A }
0N/A
0N/A String sourceDecomposition = Normalizer.normalize(source, form);
0N/A String targetDecomposition = Normalizer.normalize(target, form);
0N/A return sourceDecomposition.compareTo(targetDecomposition);
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Transforms the string into a series of characters that can be compared
0N/A * with CollationKey.compareTo. This overrides java.text.Collator.getCollationKey.
0N/A * It can be overriden in a subclass.
0N/A */
0N/A public synchronized CollationKey getCollationKey(String source)
0N/A {
0N/A //
0N/A // The basic algorithm here is to find all of the collation elements for each
0N/A // character in the source string, convert them to a char representation,
0N/A // and put them into the collation key. But it's trickier than that.
0N/A // Each collation element in a string has three components: primary (A vs B),
0N/A // secondary (A vs A-acute), and tertiary (A' vs a); and a primary difference
0N/A // at the end of a string takes precedence over a secondary or tertiary
0N/A // difference earlier in the string.
0N/A //
0N/A // To account for this, we put all of the primary orders at the beginning of the
0N/A // string, followed by the secondary and tertiary orders, separated by nulls.
0N/A //
0N/A // Here's a hypothetical example, with the collation element represented as
0N/A // a three-digit number, one digit for primary, one for secondary, etc.
0N/A //
0N/A // String: A a B \u00e9 <--(e-acute)
0N/A // Collation Elements: 101 100 201 510
0N/A //
0N/A // Collation Key: 1125<null>0001<null>1010
0N/A //
0N/A // To make things even trickier, secondary differences (accent marks) are compared
0N/A // starting at the *end* of the string in languages with French secondary ordering.
0N/A // But when comparing the accent marks on a single base character, they are compared
0N/A // from the beginning. To handle this, we reverse all of the accents that belong
0N/A // to each base character, then we reverse the entire string of secondary orderings
0N/A // at the end. Taking the same example above, a French collator might return
0N/A // this instead:
0N/A //
0N/A // Collation Key: 1125<null>1000<null>1010
0N/A //
0N/A if (source == null)
0N/A return null;
0N/A
0N/A if (primResult == null) {
0N/A primResult = new StringBuffer();
0N/A secResult = new StringBuffer();
0N/A terResult = new StringBuffer();
0N/A } else {
0N/A primResult.setLength(0);
0N/A secResult.setLength(0);
0N/A terResult.setLength(0);
0N/A }
0N/A int order = 0;
0N/A boolean compareSec = (getStrength() >= Collator.SECONDARY);
0N/A boolean compareTer = (getStrength() >= Collator.TERTIARY);
0N/A int secOrder = CollationElementIterator.NULLORDER;
0N/A int terOrder = CollationElementIterator.NULLORDER;
0N/A int preSecIgnore = 0;
0N/A
0N/A if (sourceCursor == null) {
0N/A sourceCursor = getCollationElementIterator(source);
0N/A } else {
0N/A sourceCursor.setText(source);
0N/A }
0N/A
0N/A // walk through each character
0N/A while ((order = sourceCursor.next()) !=
0N/A CollationElementIterator.NULLORDER)
0N/A {
0N/A secOrder = CollationElementIterator.secondaryOrder(order);
0N/A terOrder = CollationElementIterator.tertiaryOrder(order);
0N/A if (!CollationElementIterator.isIgnorable(order))
0N/A {
0N/A primResult.append((char) (CollationElementIterator.primaryOrder(order)
0N/A + COLLATIONKEYOFFSET));
0N/A
0N/A if (compareSec) {
0N/A //
0N/A // accumulate all of the ignorable/secondary characters attached
0N/A // to a given base character
0N/A //
0N/A if (tables.isFrenchSec() && preSecIgnore < secResult.length()) {
0N/A //
0N/A // We're doing reversed secondary ordering and we've hit a base
0N/A // (non-ignorable) character. Reverse any secondary orderings
0N/A // that applied to the last base character. (see block comment above.)
0N/A //
0N/A RBCollationTables.reverse(secResult, preSecIgnore, secResult.length());
0N/A }
0N/A // Remember where we are in the secondary orderings - this is how far
0N/A // back to go if we need to reverse them later.
0N/A secResult.append((char)(secOrder+ COLLATIONKEYOFFSET));
0N/A preSecIgnore = secResult.length();
0N/A }
0N/A if (compareTer) {
0N/A terResult.append((char)(terOrder+ COLLATIONKEYOFFSET));
0N/A }
0N/A }
0N/A else
0N/A {
0N/A if (compareSec && secOrder != 0)
0N/A secResult.append((char)
0N/A (secOrder + tables.getMaxSecOrder() + COLLATIONKEYOFFSET));
0N/A if (compareTer && terOrder != 0)
0N/A terResult.append((char)
0N/A (terOrder + tables.getMaxTerOrder() + COLLATIONKEYOFFSET));
0N/A }
0N/A }
0N/A if (tables.isFrenchSec())
0N/A {
0N/A if (preSecIgnore < secResult.length()) {
0N/A // If we've accumlated any secondary characters after the last base character,
0N/A // reverse them.
0N/A RBCollationTables.reverse(secResult, preSecIgnore, secResult.length());
0N/A }
0N/A // And now reverse the entire secResult to get French secondary ordering.
0N/A RBCollationTables.reverse(secResult, 0, secResult.length());
0N/A }
0N/A primResult.append((char)0);
0N/A secResult.append((char)0);
0N/A secResult.append(terResult.toString());
0N/A primResult.append(secResult.toString());
0N/A
0N/A if (getStrength() == IDENTICAL) {
0N/A primResult.append((char)0);
0N/A int mode = getDecomposition();
0N/A if (mode == CANONICAL_DECOMPOSITION) {
0N/A primResult.append(Normalizer.normalize(source, Normalizer.Form.NFD));
0N/A } else if (mode == FULL_DECOMPOSITION) {
0N/A primResult.append(Normalizer.normalize(source, Normalizer.Form.NFKD));
0N/A } else {
0N/A primResult.append(source);
0N/A }
0N/A }
0N/A return new RuleBasedCollationKey(source, primResult.toString());
0N/A }
0N/A
0N/A /**
0N/A * Standard override; no change in semantics.
0N/A */
0N/A public Object clone() {
0N/A // if we know we're not actually a subclass of RuleBasedCollator
0N/A // (this class really should have been made final), bypass
0N/A // Object.clone() and use our "copy constructor". This is faster.
0N/A if (getClass() == RuleBasedCollator.class) {
0N/A return new RuleBasedCollator(this);
0N/A }
0N/A else {
0N/A RuleBasedCollator result = (RuleBasedCollator) super.clone();
0N/A result.primResult = null;
0N/A result.secResult = null;
0N/A result.terResult = null;
0N/A result.sourceCursor = null;
0N/A result.targetCursor = null;
0N/A return result;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Compares the equality of two collation objects.
0N/A * @param obj the table-based collation object to be compared with this.
0N/A * @return true if the current table-based collation object is the same
0N/A * as the table-based collation object obj; false otherwise.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj == null) return false;
0N/A if (!super.equals(obj)) return false; // super does class check
0N/A RuleBasedCollator other = (RuleBasedCollator) obj;
0N/A // all other non-transient information is also contained in rules.
0N/A return (getRules().equals(other.getRules()));
0N/A }
0N/A
0N/A /**
0N/A * Generates the hash code for the table-based collation object
0N/A */
0N/A public int hashCode() {
0N/A return getRules().hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Allows CollationElementIterator access to the tables object
0N/A */
0N/A RBCollationTables getTables() {
0N/A return tables;
0N/A }
0N/A
0N/A // ==============================================================
0N/A // private
0N/A // ==============================================================
0N/A
0N/A final static int CHARINDEX = 0x70000000; // need look up in .commit()
0N/A final static int EXPANDCHARINDEX = 0x7E000000; // Expand index follows
0N/A final static int CONTRACTCHARINDEX = 0x7F000000; // contract indexes follow
0N/A final static int UNMAPPED = 0xFFFFFFFF;
0N/A
0N/A private final static int COLLATIONKEYOFFSET = 1;
0N/A
0N/A private RBCollationTables tables = null;
0N/A
0N/A // Internal objects that are cached across calls so that they don't have to
0N/A // be created/destroyed on every call to compare() and getCollationKey()
0N/A private StringBuffer primResult = null;
0N/A private StringBuffer secResult = null;
0N/A private StringBuffer terResult = null;
0N/A private CollationElementIterator sourceCursor = null;
0N/A private CollationElementIterator targetCursor = null;
0N/A}