jdk/src/share/classes/java/text/RBTableBuilder.java
author naoto
Tue, 31 Aug 2010 11:27:10 -0700
changeset 6489 9e7015635425
parent 5506 202f599c92aa
child 12848 da701d422d2c
permissions -rw-r--r--
4700857: RFE: separating user locale and user interface locale Reviewed-by: okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *   The original version of this source code and documentation is copyrighted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * materials are provided under terms of a License Agreement between Taligent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * and Sun. This technology is protected by multiple US and International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * patents. This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *   Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
package java.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.text.UCompactIntArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.text.IntHashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.text.ComposedCharIter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.text.CollatorUtilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.text.normalizer.NormalizerImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This class contains all the code to parse a RuleBasedCollator pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and build a RBCollationTables object from it.  A particular instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * of tis class exists only during the actual build process-- once an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * RBCollationTables object has been built, the RBTableBuilder object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * goes away.  This object carries all of the state which is only needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * during the build process, plus a "shadow" copy of all of the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * that will go into the tables object itself.  This object communicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * with RBCollationTables through a separate class, RBCollationTables.BuildAPI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * this is an inner class of RBCollationTables and provides a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * private API for communication with RBTableBuilder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * This class isn't just an inner class of RBCollationTables itself because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * of its large size.  For source-code readability, it seemed better for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * builder to have its own source file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
final class RBTableBuilder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public RBTableBuilder(RBCollationTables.BuildAPI tables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.tables = tables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Create a table-based collation object with the given rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * This is the main function that actually builds the tables and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * stores them back in the RBCollationTables object.  It is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * ONLY by the RBCollationTables constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @see java.util.RuleBasedCollator#RuleBasedCollator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @exception ParseException If the rules format is incorrect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public void build(String pattern, int decmp) throws ParseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        boolean isSource = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        String expChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        String groupChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (pattern.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new ParseException("Build rules empty.", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // This array maps Unicode characters to their collation ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        mapping = new UCompactIntArray((int)RBCollationTables.UNMAPPED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // Normalize the build rules.  Find occurances of all decomposed characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // and normalize the rules before feeding into the builder.  By "normalize",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // we mean that all precomposed Unicode characters must be converted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // a base character and one or more combining characters (such as accents).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // When there are multiple combining characters attached to a base character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // the combining characters must be in their canonical order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // sherman/Note:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        //(1)decmp will be NO_DECOMPOSITION only in ko locale to prevent decompose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        //hangual syllables to jamos, so we can actually just call decompose with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        //normalizer's IGNORE_HANGUL option turned on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        //(2)just call the "special version" in NormalizerImpl directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        //pattern = Normalizer.decompose(pattern, false, Normalizer.IGNORE_HANGUL, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        //Normalizer.Mode mode = CollatorUtilities.toNormalizerMode(decmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        //pattern = Normalizer.normalize(pattern, mode, 0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        pattern = NormalizerImpl.canonicalDecomposeWithSingleQuotation(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Build the merged collation entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // Since rules can be specified in any order in the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // (e.g. "c , C < d , D < e , E .... C < CH")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // this splits all of the rules in the string out into separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // objects and then sorts them.  In the above example, it merges the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // "C < CH" rule in just before the "C < D" rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        mPattern = new MergeCollation(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int order = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // Now walk though each entry and add it to my own tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (i = 0; i < mPattern.getCount(); ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            PatternEntry entry = mPattern.getItemAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                groupChars = entry.getChars();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (groupChars.length() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    switch(groupChars.charAt(groupChars.length()-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    case '@':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        frenchSec = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        groupChars = groupChars.substring(0, groupChars.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    case '!':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        seAsianSwapping = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        groupChars = groupChars.substring(0, groupChars.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                order = increment(entry.getStrength(), order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                expChars = entry.getExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (expChars.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    addExpandOrder(groupChars, expChars, order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                } else if (groupChars.length() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    char ch = groupChars.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    if (Character.isHighSurrogate(ch) && groupChars.length() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        addOrder(Character.toCodePoint(ch, groupChars.charAt(1)), order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        addContractOrder(groupChars, order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    char ch = groupChars.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    addOrder(ch, order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        addComposedChars();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        commit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        mapping.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        System.out.println("mappingSize=" + mapping.getKSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        for (int j = 0; j < 0xffff; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            int value = mapping.elementAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (value != RBCollationTables.UNMAPPED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                System.out.println("index=" + Integer.toString(j, 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                           + ", value=" + Integer.toString(value, 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        tables.fillInTables(frenchSec, seAsianSwapping, mapping, contractTable, expandTable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    contractFlags, maxSecOrder, maxTerOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /** Add expanding entries for pre-composed unicode characters so that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * collator can be used reasonably well with decomposition turned off.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private void addComposedChars() throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // Iterate through all of the pre-composed characters in Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        ComposedCharIter iter = new ComposedCharIter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        while ((c = iter.next()) != ComposedCharIter.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (getCharOrder(c) == RBCollationTables.UNMAPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                // We don't already have an ordering for this pre-composed character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                // First, see if the decomposed string is already in our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                // tables as a single contracting-string ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                // If so, just map the precomposed character to that order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                // TODO: What we should really be doing here is trying to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                // longest initial substring of the decomposition that is present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                // in the tables as a contracting character sequence, and find its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                // ordering.  Then do this recursively with the remaining chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // so that we build a list of orderings, and add that list to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                // the expansion table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                // That would be more correct but also significantly slower, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                // I'm not totally sure it's worth doing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                String s = iter.decomposition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                //sherman/Note: if this is 1 character decomposed string, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                //only thing need to do is to check if this decomposed character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                //has an entry in our order table, this order is not necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                //to be a contraction order, if it does have one, add an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                //for the precomposed character by using the same order, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                //previous impl unnecessarily adds a single character expansion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                //entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (s.length() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    int order = getCharOrder(s.charAt(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    if (order != RBCollationTables.UNMAPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        addOrder(c, order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                } else if (s.length() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    char ch0 = s.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    if (Character.isHighSurrogate(ch0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        int order = getCharOrder(s.codePointAt(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        if (order != RBCollationTables.UNMAPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            addOrder(c, order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                int contractOrder = getContractOrder(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                if (contractOrder != RBCollationTables.UNMAPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    addOrder(c, contractOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    // We don't have a contracting ordering for the entire string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    // that results from the decomposition, but if we have orders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    // for each individual character, we can add an expanding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    // table entry for the pre-composed character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    boolean allThere = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        if (getCharOrder(s.charAt(i)) == RBCollationTables.UNMAPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                            allThere = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    if (allThere) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        addExpandOrder(c, s, RBCollationTables.UNMAPPED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Look up for unmapped values in the expanded character table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * When the expanding character tables are built by addExpandOrder,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * it doesn't know what the final ordering of each character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * in the expansion will be.  Instead, it just puts the raw character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * code into the table, adding CHARINDEX as a flag.  Now that we've
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * finished building the mapping table, we can go back and look up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * that character to see what its real collation order is and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * stick that into the expansion table.  That lets us avoid doing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * a two-stage lookup later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private final void commit()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (expandTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            for (int i = 0; i < expandTable.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                int[] valueList = (int [])expandTable.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                for (int j = 0; j < valueList.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    int order = valueList[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    if (order < RBCollationTables.EXPANDCHARINDEX && order > CHARINDEX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        // found a expanding character that isn't filled in yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        int ch = order - CHARINDEX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        // Get the real values for the non-filled entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        int realValue = getCharOrder(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        if (realValue == RBCollationTables.UNMAPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                            // The real value is still unmapped, maybe it's ignorable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                            valueList[j] = IGNORABLEMASK & ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                            // just fill in the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                            valueList[j] = realValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *  Increment of the last order based on the comparison level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private final int increment(int aStrength, int lastValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        switch(aStrength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        case Collator.PRIMARY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            // increment priamry order  and mask off secondary and tertiary difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            lastValue += PRIMARYORDERINCREMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            lastValue &= RBCollationTables.PRIMARYORDERMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            isOverIgnore = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        case Collator.SECONDARY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // increment secondary order and mask off tertiary difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            lastValue += SECONDARYORDERINCREMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            lastValue &= RBCollationTables.SECONDARYDIFFERENCEONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            // record max # of ignorable chars with secondary difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (!isOverIgnore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                maxSecOrder++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        case Collator.TERTIARY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // increment tertiary order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            lastValue += TERTIARYORDERINCREMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            // record max # of ignorable chars with tertiary difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (!isOverIgnore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                maxTerOrder++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return lastValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *  Adds a character and its designated order into the collation table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private final void addOrder(int ch, int anOrder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // See if the char already has an order in the mapping table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int order = mapping.elementAt(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (order >= RBCollationTables.CONTRACTCHARINDEX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // There's already an entry for this character that points to a contracting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // character table.  Instead of adding the character directly to the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // table, we must add it to the contract table instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            int length = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (Character.isSupplementaryCodePoint(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                length = Character.toChars(ch, keyBuf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                keyBuf[0] = (char)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            addContractOrder(new String(keyBuf, 0, length), anOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            // add the entry to the mapping table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            // the same later entry replaces the previous one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            mapping.setElementAt(ch, anOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    private final void addContractOrder(String groupChars, int anOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        addContractOrder(groupChars, anOrder, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *  Adds the contracting string into the collation table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private final void addContractOrder(String groupChars, int anOrder,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                          boolean fwd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (contractTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            contractTable = new Vector(INITIALTABLESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        //initial character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        int ch = groupChars.codePointAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        char ch0 = groupChars.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        int ch = Character.isHighSurrogate(ch0)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
          Character.toCodePoint(ch0, groupChars.charAt(1)):ch0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
          */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // See if the initial character of the string already has a contract table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        int entry = mapping.elementAt(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        Vector entryTable = getContractValuesImpl(entry - RBCollationTables.CONTRACTCHARINDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (entryTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            // We need to create a new table of contract entries for this base char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            int tableIndex = RBCollationTables.CONTRACTCHARINDEX + contractTable.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            entryTable = new Vector(INITIALTABLESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            contractTable.addElement(entryTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            // Add the initial character's current ordering first. then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            // update its mapping to point to this contract table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            entryTable.addElement(new EntryPair(groupChars.substring(0,Character.charCount(ch)), entry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            mapping.setElementAt(ch, tableIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // Now add (or replace) this string in the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        int index = RBCollationTables.getEntry(entryTable, groupChars, fwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (index != RBCollationTables.UNMAPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            EntryPair pair = (EntryPair) entryTable.elementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            pair.value = anOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            EntryPair pair = (EntryPair)entryTable.lastElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            // NOTE:  This little bit of logic is here to speed CollationElementIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            // .nextContractChar().  This code ensures that the longest sequence in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            // this list is always the _last_ one in the list.  This keeps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            // nextContractChar() from having to search the entire list for the longest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            // sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (groupChars.length() > pair.entryName.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                entryTable.addElement(new EntryPair(groupChars, anOrder, fwd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                entryTable.insertElementAt(new EntryPair(groupChars, anOrder,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        fwd), entryTable.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // If this was a forward mapping for a contracting string, also add a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // reverse mapping for it, so that CollationElementIterator.previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        // can work right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (fwd && groupChars.length() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            addContractFlags(groupChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            addContractOrder(new StringBuffer(groupChars).reverse().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                             anOrder, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * If the given string has been specified as a contracting string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * in this collation table, return its ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Otherwise return UNMAPPED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    private int getContractOrder(String groupChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        int result = RBCollationTables.UNMAPPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (contractTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            int ch = groupChars.codePointAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            char ch0 = groupChars.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            int ch = Character.isHighSurrogate(ch0)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
              Character.toCodePoint(ch0, groupChars.charAt(1)):ch0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
              */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            Vector entryTable = getContractValues(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (entryTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                int index = RBCollationTables.getEntry(entryTable, groupChars, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                if (index != RBCollationTables.UNMAPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    EntryPair pair = (EntryPair) entryTable.elementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    result = pair.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    private final int getCharOrder(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        int order = mapping.elementAt(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (order >= RBCollationTables.CONTRACTCHARINDEX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            Vector groupList = getContractValuesImpl(order - RBCollationTables.CONTRACTCHARINDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            EntryPair pair = (EntryPair)groupList.firstElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            order = pair.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        return order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *  Get the entry of hash table of the contracting string in the collation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *  table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *  @param ch the starting character of the contracting string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private Vector getContractValues(int ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        int index = mapping.elementAt(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        return getContractValuesImpl(index - RBCollationTables.CONTRACTCHARINDEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    private Vector getContractValuesImpl(int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (index >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return (Vector)contractTable.elementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        else // not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *  Adds the expanding string into the collation table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    private final void addExpandOrder(String contractChars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                String expandChars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                int anOrder) throws ParseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        // Create an expansion table entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        int tableIndex = addExpansion(anOrder, expandChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        // And add its index into the main mapping table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (contractChars.length() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            char ch = contractChars.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            if (Character.isHighSurrogate(ch) && contractChars.length() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                char ch2 = contractChars.charAt(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                if (Character.isLowSurrogate(ch2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    //only add into table when it is a legal surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    addOrder(Character.toCodePoint(ch, ch2), tableIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                addContractOrder(contractChars, tableIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            addOrder(contractChars.charAt(0), tableIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    private final void addExpandOrder(int ch, String expandChars, int anOrder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
      throws ParseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        int tableIndex = addExpansion(anOrder, expandChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        addOrder(ch, tableIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Create a new entry in the expansion table that contains the orderings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * for the given characers.  If anOrder is valid, it is added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * beginning of the expanded list of orders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    private int addExpansion(int anOrder, String expandChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (expandTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            expandTable = new Vector(INITIALTABLESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        // If anOrder is valid, we want to add it at the beginning of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        int offset = (anOrder == RBCollationTables.UNMAPPED) ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int[] valueList = new int[expandChars.length() + offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (offset == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            valueList[0] = anOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        int j = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        for (int i = 0; i < expandChars.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            char ch0 = expandChars.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            char ch1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            int ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            if (Character.isHighSurrogate(ch0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                if (++i == expandChars.length() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    !Character.isLowSurrogate(ch1=expandChars.charAt(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    //ether we are missing the low surrogate or the next char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    //is not a legal low surrogate, so stop loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                ch = Character.toCodePoint(ch0, ch1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                ch = ch0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            int mapValue = getCharOrder(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if (mapValue != RBCollationTables.UNMAPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                valueList[j++] = mapValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                // can't find it in the table, will be filled in by commit().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                valueList[j++] = CHARINDEX + ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (j < valueList.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            //we had at least one supplementary character, the size of valueList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            //is bigger than it really needs...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            int[] tmpBuf = new int[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            while (--j >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                tmpBuf[j] = valueList[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            valueList = tmpBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        // Add the expanding char list into the expansion table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        int tableIndex = RBCollationTables.EXPANDCHARINDEX + expandTable.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        expandTable.addElement(valueList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return tableIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    private void addContractFlags(String chars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        char c0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        int len = chars.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            c0 = chars.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            c = Character.isHighSurrogate(c0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                          ?Character.toCodePoint(c0, chars.charAt(++i))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                          :c0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            contractFlags.put(c, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    // ==============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    // constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    // ==============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    final static int CHARINDEX = 0x70000000;  // need look up in .commit()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    private final static int IGNORABLEMASK = 0x0000ffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    private final static int PRIMARYORDERINCREMENT = 0x00010000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    private final static int SECONDARYORDERINCREMENT = 0x00000100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    private final static int TERTIARYORDERINCREMENT = 0x00000001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    private final static int INITIALTABLESIZE = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    private final static int MAXKEYSIZE = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    // ==============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    // instance variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    // ==============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    // variables used by the build process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    private RBCollationTables.BuildAPI tables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    private MergeCollation mPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private boolean isOverIgnore = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    private char[] keyBuf = new char[MAXKEYSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    private IntHashtable contractFlags = new IntHashtable(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    // "shadow" copies of the instance variables in RBCollationTables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    // (the values in these variables are copied back into RBCollationTables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    // at the end of the build process)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private boolean frenchSec = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    private boolean seAsianSwapping = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    private UCompactIntArray mapping = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    private Vector   contractTable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    private Vector   expandTable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    private short maxSecOrder = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    private short maxTerOrder = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
}