0N/A/*
3261N/A * Copyright (c) 1996, 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.lang.Character;
0N/Aimport java.util.Vector;
0N/Aimport sun.text.CollatorUtilities;
0N/Aimport sun.text.normalizer.NormalizerBase;
0N/A
0N/A/**
0N/A * The <code>CollationElementIterator</code> class is used as an iterator
0N/A * to walk through each character of an international string. Use the iterator
0N/A * to return the ordering priority of the positioned character. The ordering
0N/A * priority of a character, which we refer to as a key, defines how a character
0N/A * is collated in the given collation object.
0N/A *
0N/A * <p>
0N/A * For example, consider the following in Spanish:
0N/A * <blockquote>
0N/A * <pre>
0N/A * "ca" -> the first key is key('c') and second key is key('a').
0N/A * "cha" -> the first key is key('ch') and second key is key('a').
0N/A * </pre>
0N/A * </blockquote>
0N/A * And in German,
0N/A * <blockquote>
0N/A * <pre>
0N/A * "\u00e4b"-> the first key is key('a'), the second key is key('e'), and
0N/A * the third key is key('b').
0N/A * </pre>
0N/A * </blockquote>
0N/A * The key of a character is an integer composed of primary order(short),
0N/A * secondary order(byte), and tertiary order(byte). Java strictly defines
0N/A * the size and signedness of its primitive data types. Therefore, the static
0N/A * functions <code>primaryOrder</code>, <code>secondaryOrder</code>, and
0N/A * <code>tertiaryOrder</code> return <code>int</code>, <code>short</code>,
0N/A * and <code>short</code> respectively to ensure the correctness of the key
0N/A * value.
0N/A *
0N/A * <p>
0N/A * Example of the iterator usage,
0N/A * <blockquote>
0N/A * <pre>
0N/A *
0N/A * String testString = "This is a test";
3194N/A * Collator col = Collator.getInstance();
3194N/A * if (col instanceof RuleBasedCollator) {
3194N/A * RuleBasedCollator ruleBasedCollator = (RuleBasedCollator)col;
3194N/A * CollationElementIterator collationElementIterator = ruleBasedCollator.getCollationElementIterator(testString);
3194N/A * int primaryOrder = CollationElementIterator.primaryOrder(collationElementIterator.next());
3194N/A * :
3194N/A * }
0N/A * </pre>
0N/A * </blockquote>
0N/A *
0N/A * <p>
0N/A * <code>CollationElementIterator.next</code> returns the collation order
0N/A * of the next character. A collation order consists of primary order,
0N/A * secondary order and tertiary order. The data type of the collation
0N/A * order is <strong>int</strong>. The first 16 bits of a collation order
0N/A * is its primary order; the next 8 bits is the secondary order and the
0N/A * last 8 bits is the tertiary order.
0N/A *
3194N/A * <p><b>Note:</b> <code>CollationElementIterator</code> is a part of
3194N/A * <code>RuleBasedCollator</code> implementation. It is only usable
3194N/A * with <code>RuleBasedCollator</code> instances.
3194N/A *
0N/A * @see Collator
0N/A * @see RuleBasedCollator
0N/A * @author Helena Shih, Laura Werner, Richard Gillam
0N/A */
0N/Apublic final class CollationElementIterator
0N/A{
0N/A /**
0N/A * Null order which indicates the end of string is reached by the
0N/A * cursor.
0N/A */
0N/A public final static int NULLORDER = 0xffffffff;
0N/A
0N/A /**
0N/A * CollationElementIterator constructor. This takes the source string and
0N/A * the collation object. The cursor will walk thru the source string based
0N/A * on the predefined collation rules. If the source string is empty,
0N/A * NULLORDER will be returned on the calls to next().
0N/A * @param sourceText the source string.
0N/A * @param order the collation object.
0N/A */
0N/A CollationElementIterator(String sourceText, RuleBasedCollator owner) {
0N/A this.owner = owner;
0N/A ordering = owner.getTables();
0N/A if ( sourceText.length() != 0 ) {
0N/A NormalizerBase.Mode mode =
0N/A CollatorUtilities.toNormalizerMode(owner.getDecomposition());
0N/A text = new NormalizerBase(sourceText, mode);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * CollationElementIterator constructor. This takes the source string and
0N/A * the collation object. The cursor will walk thru the source string based
0N/A * on the predefined collation rules. If the source string is empty,
0N/A * NULLORDER will be returned on the calls to next().
0N/A * @param sourceText the source string.
0N/A * @param order the collation object.
0N/A */
0N/A CollationElementIterator(CharacterIterator sourceText, RuleBasedCollator owner) {
0N/A this.owner = owner;
0N/A ordering = owner.getTables();
0N/A NormalizerBase.Mode mode =
0N/A CollatorUtilities.toNormalizerMode(owner.getDecomposition());
0N/A text = new NormalizerBase(sourceText, mode);
0N/A }
0N/A
0N/A /**
0N/A * Resets the cursor to the beginning of the string. The next call
0N/A * to next() will return the first collation element in the string.
0N/A */
0N/A public void reset()
0N/A {
0N/A if (text != null) {
0N/A text.reset();
0N/A NormalizerBase.Mode mode =
0N/A CollatorUtilities.toNormalizerMode(owner.getDecomposition());
0N/A text.setMode(mode);
0N/A }
0N/A buffer = null;
0N/A expIndex = 0;
0N/A swapOrder = 0;
0N/A }
0N/A
0N/A /**
0N/A * Get the next collation element in the string. <p>This iterator iterates
0N/A * over a sequence of collation elements that were built from the string.
0N/A * Because there isn't necessarily a one-to-one mapping from characters to
0N/A * collation elements, this doesn't mean the same thing as "return the
0N/A * collation element [or ordering priority] of the next character in the
0N/A * string".</p>
0N/A * <p>This function returns the collation element that the iterator is currently
0N/A * pointing to and then updates the internal pointer to point to the next element.
0N/A * previous() updates the pointer first and then returns the element. This
0N/A * means that when you change direction while iterating (i.e., call next() and
0N/A * then call previous(), or call previous() and then call next()), you'll get
0N/A * back the same element twice.</p>
0N/A */
0N/A public int next()
0N/A {
0N/A if (text == null) {
0N/A return NULLORDER;
0N/A }
0N/A NormalizerBase.Mode textMode = text.getMode();
0N/A // convert the owner's mode to something the Normalizer understands
0N/A NormalizerBase.Mode ownerMode =
0N/A CollatorUtilities.toNormalizerMode(owner.getDecomposition());
0N/A if (textMode != ownerMode) {
0N/A text.setMode(ownerMode);
0N/A }
0N/A
0N/A // if buffer contains any decomposed char values
0N/A // return their strength orders before continuing in
0N/A // the Normalizer's CharacterIterator.
0N/A if (buffer != null) {
0N/A if (expIndex < buffer.length) {
0N/A return strengthOrder(buffer[expIndex++]);
0N/A } else {
0N/A buffer = null;
0N/A expIndex = 0;
0N/A }
0N/A } else if (swapOrder != 0) {
0N/A if (Character.isSupplementaryCodePoint(swapOrder)) {
0N/A char[] chars = Character.toChars(swapOrder);
0N/A swapOrder = chars[1];
0N/A return chars[0] << 16;
0N/A }
0N/A int order = swapOrder << 16;
0N/A swapOrder = 0;
0N/A return order;
0N/A }
0N/A int ch = text.next();
0N/A
0N/A // are we at the end of Normalizer's text?
0N/A if (ch == NormalizerBase.DONE) {
0N/A return NULLORDER;
0N/A }
0N/A
0N/A int value = ordering.getUnicodeOrder(ch);
0N/A if (value == RuleBasedCollator.UNMAPPED) {
0N/A swapOrder = ch;
0N/A return UNMAPPEDCHARVALUE;
0N/A }
0N/A else if (value >= RuleBasedCollator.CONTRACTCHARINDEX) {
0N/A value = nextContractChar(ch);
0N/A }
0N/A if (value >= RuleBasedCollator.EXPANDCHARINDEX) {
0N/A buffer = ordering.getExpandValueList(value);
0N/A expIndex = 0;
0N/A value = buffer[expIndex++];
0N/A }
0N/A
0N/A if (ordering.isSEAsianSwapping()) {
0N/A int consonant;
0N/A if (isThaiPreVowel(ch)) {
0N/A consonant = text.next();
0N/A if (isThaiBaseConsonant(consonant)) {
0N/A buffer = makeReorderedBuffer(consonant, value, buffer, true);
0N/A value = buffer[0];
0N/A expIndex = 1;
2137N/A } else if (consonant != NormalizerBase.DONE) {
0N/A text.previous();
0N/A }
0N/A }
0N/A if (isLaoPreVowel(ch)) {
0N/A consonant = text.next();
0N/A if (isLaoBaseConsonant(consonant)) {
0N/A buffer = makeReorderedBuffer(consonant, value, buffer, true);
0N/A value = buffer[0];
0N/A expIndex = 1;
2137N/A } else if (consonant != NormalizerBase.DONE) {
0N/A text.previous();
0N/A }
0N/A }
0N/A }
0N/A
0N/A return strengthOrder(value);
0N/A }
0N/A
0N/A /**
0N/A * Get the previous collation element in the string. <p>This iterator iterates
0N/A * over a sequence of collation elements that were built from the string.
0N/A * Because there isn't necessarily a one-to-one mapping from characters to
0N/A * collation elements, this doesn't mean the same thing as "return the
0N/A * collation element [or ordering priority] of the previous character in the
0N/A * string".</p>
0N/A * <p>This function updates the iterator's internal pointer to point to the
0N/A * collation element preceding the one it's currently pointing to and then
0N/A * returns that element, while next() returns the current element and then
0N/A * updates the pointer. This means that when you change direction while
0N/A * iterating (i.e., call next() and then call previous(), or call previous()
0N/A * and then call next()), you'll get back the same element twice.</p>
0N/A * @since 1.2
0N/A */
0N/A public int previous()
0N/A {
0N/A if (text == null) {
0N/A return NULLORDER;
0N/A }
0N/A NormalizerBase.Mode textMode = text.getMode();
0N/A // convert the owner's mode to something the Normalizer understands
0N/A NormalizerBase.Mode ownerMode =
0N/A CollatorUtilities.toNormalizerMode(owner.getDecomposition());
0N/A if (textMode != ownerMode) {
0N/A text.setMode(ownerMode);
0N/A }
0N/A if (buffer != null) {
0N/A if (expIndex > 0) {
0N/A return strengthOrder(buffer[--expIndex]);
0N/A } else {
0N/A buffer = null;
0N/A expIndex = 0;
0N/A }
0N/A } else if (swapOrder != 0) {
0N/A if (Character.isSupplementaryCodePoint(swapOrder)) {
0N/A char[] chars = Character.toChars(swapOrder);
0N/A swapOrder = chars[1];
0N/A return chars[0] << 16;
0N/A }
0N/A int order = swapOrder << 16;
0N/A swapOrder = 0;
0N/A return order;
0N/A }
0N/A int ch = text.previous();
0N/A if (ch == NormalizerBase.DONE) {
0N/A return NULLORDER;
0N/A }
0N/A
0N/A int value = ordering.getUnicodeOrder(ch);
0N/A
0N/A if (value == RuleBasedCollator.UNMAPPED) {
0N/A swapOrder = UNMAPPEDCHARVALUE;
0N/A return ch;
0N/A } else if (value >= RuleBasedCollator.CONTRACTCHARINDEX) {
0N/A value = prevContractChar(ch);
0N/A }
0N/A if (value >= RuleBasedCollator.EXPANDCHARINDEX) {
0N/A buffer = ordering.getExpandValueList(value);
0N/A expIndex = buffer.length;
0N/A value = buffer[--expIndex];
0N/A }
0N/A
0N/A if (ordering.isSEAsianSwapping()) {
0N/A int vowel;
0N/A if (isThaiBaseConsonant(ch)) {
0N/A vowel = text.previous();
0N/A if (isThaiPreVowel(vowel)) {
0N/A buffer = makeReorderedBuffer(vowel, value, buffer, false);
0N/A expIndex = buffer.length - 1;
0N/A value = buffer[expIndex];
0N/A } else {
0N/A text.next();
0N/A }
0N/A }
0N/A if (isLaoBaseConsonant(ch)) {
0N/A vowel = text.previous();
0N/A if (isLaoPreVowel(vowel)) {
0N/A buffer = makeReorderedBuffer(vowel, value, buffer, false);
0N/A expIndex = buffer.length - 1;
0N/A value = buffer[expIndex];
0N/A } else {
0N/A text.next();
0N/A }
0N/A }
0N/A }
0N/A
0N/A return strengthOrder(value);
0N/A }
0N/A
0N/A /**
0N/A * Return the primary component of a collation element.
0N/A * @param order the collation element
0N/A * @return the element's primary component
0N/A */
0N/A public final static int primaryOrder(int order)
0N/A {
0N/A order &= RBCollationTables.PRIMARYORDERMASK;
0N/A return (order >>> RBCollationTables.PRIMARYORDERSHIFT);
0N/A }
0N/A /**
0N/A * Return the secondary component of a collation element.
0N/A * @param order the collation element
0N/A * @return the element's secondary component
0N/A */
0N/A public final static short secondaryOrder(int order)
0N/A {
0N/A order = order & RBCollationTables.SECONDARYORDERMASK;
0N/A return ((short)(order >> RBCollationTables.SECONDARYORDERSHIFT));
0N/A }
0N/A /**
0N/A * Return the tertiary component of a collation element.
0N/A * @param order the collation element
0N/A * @return the element's tertiary component
0N/A */
0N/A public final static short tertiaryOrder(int order)
0N/A {
0N/A return ((short)(order &= RBCollationTables.TERTIARYORDERMASK));
0N/A }
0N/A
0N/A /**
0N/A * Get the comparison order in the desired strength. Ignore the other
0N/A * differences.
0N/A * @param order The order value
0N/A */
0N/A final int strengthOrder(int order)
0N/A {
0N/A int s = owner.getStrength();
0N/A if (s == Collator.PRIMARY)
0N/A {
0N/A order &= RBCollationTables.PRIMARYDIFFERENCEONLY;
0N/A } else if (s == Collator.SECONDARY)
0N/A {
0N/A order &= RBCollationTables.SECONDARYDIFFERENCEONLY;
0N/A }
0N/A return order;
0N/A }
0N/A
0N/A /**
0N/A * Sets the iterator to point to the collation element corresponding to
0N/A * the specified character (the parameter is a CHARACTER offset in the
0N/A * original string, not an offset into its corresponding sequence of
0N/A * collation elements). The value returned by the next call to next()
0N/A * will be the collation element corresponding to the specified position
0N/A * in the text. If that position is in the middle of a contracting
0N/A * character sequence, the result of the next call to next() is the
0N/A * collation element for that sequence. This means that getOffset()
0N/A * is not guaranteed to return the same value as was passed to a preceding
0N/A * call to setOffset().
0N/A *
0N/A * @param newOffset The new character offset into the original text.
0N/A * @since 1.2
0N/A */
0N/A public void setOffset(int newOffset)
0N/A {
0N/A if (text != null) {
0N/A if (newOffset < text.getBeginIndex()
0N/A || newOffset >= text.getEndIndex()) {
0N/A text.setIndexOnly(newOffset);
0N/A } else {
0N/A int c = text.setIndex(newOffset);
0N/A
0N/A // if the desired character isn't used in a contracting character
0N/A // sequence, bypass all the backing-up logic-- we're sitting on
0N/A // the right character already
0N/A if (ordering.usedInContractSeq(c)) {
0N/A // walk backwards through the string until we see a character
0N/A // that DOESN'T participate in a contracting character sequence
0N/A while (ordering.usedInContractSeq(c)) {
0N/A c = text.previous();
0N/A }
0N/A // now walk forward using this object's next() method until
0N/A // we pass the starting point and set our current position
0N/A // to the beginning of the last "character" before or at
0N/A // our starting position
0N/A int last = text.getIndex();
0N/A while (text.getIndex() <= newOffset) {
0N/A last = text.getIndex();
0N/A next();
0N/A }
0N/A text.setIndexOnly(last);
0N/A // we don't need this, since last is the last index
0N/A // that is the starting of the contraction which encompass
0N/A // newOffset
0N/A // text.previous();
0N/A }
0N/A }
0N/A }
0N/A buffer = null;
0N/A expIndex = 0;
0N/A swapOrder = 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns the character offset in the original text corresponding to the next
0N/A * collation element. (That is, getOffset() returns the position in the text
0N/A * corresponding to the collation element that will be returned by the next
0N/A * call to next().) This value will always be the index of the FIRST character
0N/A * corresponding to the collation element (a contracting character sequence is
0N/A * when two or more characters all correspond to the same collation element).
0N/A * This means if you do setOffset(x) followed immediately by getOffset(), getOffset()
0N/A * won't necessarily return x.
0N/A *
0N/A * @return The character offset in the original text corresponding to the collation
0N/A * element that will be returned by the next call to next().
0N/A * @since 1.2
0N/A */
0N/A public int getOffset()
0N/A {
0N/A return (text != null) ? text.getIndex() : 0;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Return the maximum length of any expansion sequences that end
0N/A * with the specified comparison order.
0N/A * @param order a collation order returned by previous or next.
0N/A * @return the maximum length of any expansion sequences ending
0N/A * with the specified order.
0N/A * @since 1.2
0N/A */
0N/A public int getMaxExpansion(int order)
0N/A {
0N/A return ordering.getMaxExpansion(order);
0N/A }
0N/A
0N/A /**
0N/A * Set a new string over which to iterate.
0N/A *
0N/A * @param source the new source text
0N/A * @since 1.2
0N/A */
0N/A public void setText(String source)
0N/A {
0N/A buffer = null;
0N/A swapOrder = 0;
0N/A expIndex = 0;
0N/A NormalizerBase.Mode mode =
0N/A CollatorUtilities.toNormalizerMode(owner.getDecomposition());
0N/A if (text == null) {
0N/A text = new NormalizerBase(source, mode);
0N/A } else {
0N/A text.setMode(mode);
0N/A text.setText(source);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Set a new string over which to iterate.
0N/A *
0N/A * @param source the new source text.
0N/A * @since 1.2
0N/A */
0N/A public void setText(CharacterIterator source)
0N/A {
0N/A buffer = null;
0N/A swapOrder = 0;
0N/A expIndex = 0;
0N/A NormalizerBase.Mode mode =
0N/A CollatorUtilities.toNormalizerMode(owner.getDecomposition());
0N/A if (text == null) {
0N/A text = new NormalizerBase(source, mode);
0N/A } else {
0N/A text.setMode(mode);
0N/A text.setText(source);
0N/A }
0N/A }
0N/A
0N/A //============================================================
0N/A // privates
0N/A //============================================================
0N/A
0N/A /**
0N/A * Determine if a character is a Thai vowel (which sorts after
0N/A * its base consonant).
0N/A */
0N/A private final static boolean isThaiPreVowel(int ch) {
0N/A return (ch >= 0x0e40) && (ch <= 0x0e44);
0N/A }
0N/A
0N/A /**
0N/A * Determine if a character is a Thai base consonant
0N/A */
0N/A private final static boolean isThaiBaseConsonant(int ch) {
0N/A return (ch >= 0x0e01) && (ch <= 0x0e2e);
0N/A }
0N/A
0N/A /**
0N/A * Determine if a character is a Lao vowel (which sorts after
0N/A * its base consonant).
0N/A */
0N/A private final static boolean isLaoPreVowel(int ch) {
0N/A return (ch >= 0x0ec0) && (ch <= 0x0ec4);
0N/A }
0N/A
0N/A /**
0N/A * Determine if a character is a Lao base consonant
0N/A */
0N/A private final static boolean isLaoBaseConsonant(int ch) {
0N/A return (ch >= 0x0e81) && (ch <= 0x0eae);
0N/A }
0N/A
0N/A /**
0N/A * This method produces a buffer which contains the collation
0N/A * elements for the two characters, with colFirst's values preceding
0N/A * another character's. Presumably, the other character precedes colFirst
0N/A * in logical order (otherwise you wouldn't need this method would you?).
0N/A * The assumption is that the other char's value(s) have already been
0N/A * computed. If this char has a single element it is passed to this
0N/A * method as lastValue, and lastExpansion is null. If it has an
0N/A * expansion it is passed in lastExpansion, and colLastValue is ignored.
0N/A */
0N/A private int[] makeReorderedBuffer(int colFirst,
0N/A int lastValue,
0N/A int[] lastExpansion,
0N/A boolean forward) {
0N/A
0N/A int[] result;
0N/A
0N/A int firstValue = ordering.getUnicodeOrder(colFirst);
0N/A if (firstValue >= RuleBasedCollator.CONTRACTCHARINDEX) {
0N/A firstValue = forward? nextContractChar(colFirst) : prevContractChar(colFirst);
0N/A }
0N/A
0N/A int[] firstExpansion = null;
0N/A if (firstValue >= RuleBasedCollator.EXPANDCHARINDEX) {
0N/A firstExpansion = ordering.getExpandValueList(firstValue);
0N/A }
0N/A
0N/A if (!forward) {
0N/A int temp1 = firstValue;
0N/A firstValue = lastValue;
0N/A lastValue = temp1;
0N/A int[] temp2 = firstExpansion;
0N/A firstExpansion = lastExpansion;
0N/A lastExpansion = temp2;
0N/A }
0N/A
0N/A if (firstExpansion == null && lastExpansion == null) {
0N/A result = new int [2];
0N/A result[0] = firstValue;
0N/A result[1] = lastValue;
0N/A }
0N/A else {
0N/A int firstLength = firstExpansion==null? 1 : firstExpansion.length;
0N/A int lastLength = lastExpansion==null? 1 : lastExpansion.length;
0N/A result = new int[firstLength + lastLength];
0N/A
0N/A if (firstExpansion == null) {
0N/A result[0] = firstValue;
0N/A }
0N/A else {
0N/A System.arraycopy(firstExpansion, 0, result, 0, firstLength);
0N/A }
0N/A
0N/A if (lastExpansion == null) {
0N/A result[firstLength] = lastValue;
0N/A }
0N/A else {
0N/A System.arraycopy(lastExpansion, 0, result, firstLength, lastLength);
0N/A }
0N/A }
0N/A
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Check if a comparison order is ignorable.
0N/A * @return true if a character is ignorable, false otherwise.
0N/A */
0N/A final static boolean isIgnorable(int order)
0N/A {
0N/A return ((primaryOrder(order) == 0) ? true : false);
0N/A }
0N/A
0N/A /**
0N/A * Get the ordering priority of the next contracting character in the
0N/A * string.
0N/A * @param ch the starting character of a contracting character token
0N/A * @return the next contracting character's ordering. Returns NULLORDER
0N/A * if the end of string is reached.
0N/A */
0N/A private int nextContractChar(int ch)
0N/A {
0N/A // First get the ordering of this single character,
0N/A // which is always the first element in the list
0N/A Vector list = ordering.getContractValues(ch);
0N/A EntryPair pair = (EntryPair)list.firstElement();
0N/A int order = pair.value;
0N/A
0N/A // find out the length of the longest contracting character sequence in the list.
0N/A // There's logic in the builder code to make sure the longest sequence is always
0N/A // the last.
0N/A pair = (EntryPair)list.lastElement();
0N/A int maxLength = pair.entryName.length();
0N/A
0N/A // (the Normalizer is cloned here so that the seeking we do in the next loop
0N/A // won't affect our real position in the text)
0N/A NormalizerBase tempText = (NormalizerBase)text.clone();
0N/A
0N/A // extract the next maxLength characters in the string (we have to do this using the
0N/A // Normalizer to ensure that our offsets correspond to those the rest of the
0N/A // iterator is using) and store it in "fragment".
0N/A tempText.previous();
0N/A key.setLength(0);
0N/A int c = tempText.next();
0N/A while (maxLength > 0 && c != NormalizerBase.DONE) {
0N/A if (Character.isSupplementaryCodePoint(c)) {
0N/A key.append(Character.toChars(c));
0N/A maxLength -= 2;
0N/A } else {
0N/A key.append((char)c);
0N/A --maxLength;
0N/A }
0N/A c = tempText.next();
0N/A }
0N/A String fragment = key.toString();
0N/A // now that we have that fragment, iterate through this list looking for the
0N/A // longest sequence that matches the characters in the actual text. (maxLength
0N/A // is used here to keep track of the length of the longest sequence)
0N/A // Upon exit from this loop, maxLength will contain the length of the matching
0N/A // sequence and order will contain the collation-element value corresponding
0N/A // to this sequence
0N/A maxLength = 1;
0N/A for (int i = list.size() - 1; i > 0; i--) {
0N/A pair = (EntryPair)list.elementAt(i);
0N/A if (!pair.fwd)
0N/A continue;
0N/A
0N/A if (fragment.startsWith(pair.entryName) && pair.entryName.length()
0N/A > maxLength) {
0N/A maxLength = pair.entryName.length();
0N/A order = pair.value;
0N/A }
0N/A }
0N/A
0N/A // seek our current iteration position to the end of the matching sequence
0N/A // and return the appropriate collation-element value (if there was no matching
0N/A // sequence, we're already seeked to the right position and order already contains
0N/A // the correct collation-element value for the single character)
0N/A while (maxLength > 1) {
0N/A c = text.next();
0N/A maxLength -= Character.charCount(c);
0N/A }
0N/A return order;
0N/A }
0N/A
0N/A /**
0N/A * Get the ordering priority of the previous contracting character in the
0N/A * string.
0N/A * @param ch the starting character of a contracting character token
0N/A * @return the next contracting character's ordering. Returns NULLORDER
0N/A * if the end of string is reached.
0N/A */
0N/A private int prevContractChar(int ch)
0N/A {
0N/A // This function is identical to nextContractChar(), except that we've
0N/A // switched things so that the next() and previous() calls on the Normalizer
0N/A // are switched and so that we skip entry pairs with the fwd flag turned on
0N/A // rather than off. Notice that we still use append() and startsWith() when
0N/A // working on the fragment. This is because the entry pairs that are used
0N/A // in reverse iteration have their names reversed already.
0N/A Vector list = ordering.getContractValues(ch);
0N/A EntryPair pair = (EntryPair)list.firstElement();
0N/A int order = pair.value;
0N/A
0N/A pair = (EntryPair)list.lastElement();
0N/A int maxLength = pair.entryName.length();
0N/A
0N/A NormalizerBase tempText = (NormalizerBase)text.clone();
0N/A
0N/A tempText.next();
0N/A key.setLength(0);
0N/A int c = tempText.previous();
0N/A while (maxLength > 0 && c != NormalizerBase.DONE) {
0N/A if (Character.isSupplementaryCodePoint(c)) {
0N/A key.append(Character.toChars(c));
0N/A maxLength -= 2;
0N/A } else {
0N/A key.append((char)c);
0N/A --maxLength;
0N/A }
0N/A c = tempText.previous();
0N/A }
0N/A String fragment = key.toString();
0N/A
0N/A maxLength = 1;
0N/A for (int i = list.size() - 1; i > 0; i--) {
0N/A pair = (EntryPair)list.elementAt(i);
0N/A if (pair.fwd)
0N/A continue;
0N/A
0N/A if (fragment.startsWith(pair.entryName) && pair.entryName.length()
0N/A > maxLength) {
0N/A maxLength = pair.entryName.length();
0N/A order = pair.value;
0N/A }
0N/A }
0N/A
0N/A while (maxLength > 1) {
0N/A c = text.previous();
0N/A maxLength -= Character.charCount(c);
0N/A }
0N/A return order;
0N/A }
0N/A
0N/A final static int UNMAPPEDCHARVALUE = 0x7FFF0000;
0N/A
0N/A private NormalizerBase text = null;
0N/A private int[] buffer = null;
0N/A private int expIndex = 0;
0N/A private StringBuffer key = new StringBuffer(5);
0N/A private int swapOrder = 0;
0N/A private RBCollationTables ordering;
0N/A private RuleBasedCollator owner;
0N/A}