jdk/src/share/classes/sun/util/locale/provider/BreakDictionary.java
author sherman
Tue, 12 Feb 2013 09:25:43 -0800
changeset 15658 55b829ca2334
parent 13583 dc0017b1a452
permissions -rw-r--r--
8007392: JSR 310: DateTime API Updates 8007520: Update date/time classes in j.util and j.sql packages 8007572: Replace existing jdk timezone data at <java.home>/lib/zi with JSR310's tzdb Summary: Integration of JSR310 Date/Time API for M7 Reviewed-by: darcy, alanb, naoto Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, masayoshi.okutsu@oracle.com, patrick.zhang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
     2
 * Copyright (c) 1999, 2012, 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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * (C) Copyright IBM Corp. 1996 - 2002 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The original version of this source code and documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
    40
package sun.util.locale.provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
    42
import java.io.BufferedInputStream;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
    43
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.MissingResourceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.text.CompactByteArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.text.SupplementaryCharacterData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * This is the class that represents the list of known words used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * DictionaryBasedBreakIterator.  The conceptual data structure used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * here is a trie: there is a node hanging off the root node for every
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * letter that can start a word.  Each of these nodes has a node hanging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * off of it for every letter that can be the second letter of a word
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * if this node is the first letter, and so on.  The trie is represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * as a two-dimensional array that can be treated as a table of state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * transitions.  Indexes are used to compress this array, taking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * advantage of the fact that this array will always be very sparse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
class BreakDictionary {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    //=========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // data members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    //=========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
      * The version of the dictionary that was read in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static int supportedVersion = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Maps from characters to column numbers.  The main use of this is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * avoid making room in the array for empty columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private CompactByteArray columnMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private SupplementaryCharacterData supplementaryCharColumnMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * The number of actual columns in the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private int numCols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Columns are organized into groups of 32.  This says how many
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * column groups.  (We could calculate this, but we store the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * value to avoid having to repeatedly calculate it.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private int numColGroups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The actual compressed state table.  Each conceptual row represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * a state, and the cells in it contain the row numbers of the states
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * to transition to for each possible letter.  0 is used to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * an illegal combination of letters (i.e., the error state).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * table is compressed by eliminating all the unpopulated (i.e., zero)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * cells.  Multiple conceptual rows can then be doubled up in a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * physical row by sliding them up and possibly shifting them to one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * side or the other so the populated cells don't collide.  Indexes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * are used to identify unpopulated cells and to locate populated cells.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private short[] table = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * This index maps logical row numbers to physical row numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private short[] rowIndex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * A bitmap is used to tell which cells in the comceptual table are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * populated.  This array contains all the unique bit combinations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * in that bitmap.  If the table is more than 32 columns wide,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * successive entries in this array are used for a single row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private int[] rowIndexFlags = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * This index maps from a logical row number into the bitmap table above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * (This keeps us from storing duplicate bitmap combinations.)  Since there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * are a lot of rows with only one populated cell, instead of wasting space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * in the bitmap table, we just store a negative number in this index for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * rows with one populated cell.  The absolute value of that number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * the column number of the populated cell.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private short[] rowIndexFlagsIndex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * For each logical row, this index contains a constant that is added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * the logical column number to get the physical column number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private byte[] rowIndexShifts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    //=========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    //=========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   138
    BreakDictionary(String dictionaryName)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws IOException, MissingResourceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        readDictionaryFile(dictionaryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private void readDictionaryFile(final String dictionaryName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        throws IOException, MissingResourceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        BufferedInputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        try {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   149
            in = AccessController.doPrivileged(
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   150
                new PrivilegedExceptionAction<BufferedInputStream>() {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   151
                    @Override
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10419
diff changeset
   152
                    public BufferedInputStream run() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        return new BufferedInputStream(getClass().getResourceAsStream("/sun/text/resources/" + dictionaryName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    }
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
        catch (PrivilegedActionException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   159
            throw new InternalError(e.toString(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        byte[] buf = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (in.read(buf) != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new MissingResourceException("Wrong data length",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                               dictionaryName, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   168
        // check version
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   169
        int version = RuleBasedBreakIterator.getInt(buf, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (version != supportedVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throw new MissingResourceException("Dictionary version(" + version + ") is unsupported",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                                           dictionaryName, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // get data size
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   176
        int len = RuleBasedBreakIterator.getInt(buf, 4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        buf = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (in.read(buf) != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new MissingResourceException("Wrong data length",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                               dictionaryName, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // close the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // read in the column map for BMP characteres (this is serialized in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // its internal form: an index array followed by a data array)
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   191
        l = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        offset += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        short[] temp = new short[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        for (int i = 0; i < l; i++, offset+=2) {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   195
            temp[i] = RuleBasedBreakIterator.getShort(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   197
        l = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        offset += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        byte[] temp2 = new byte[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        for (int i = 0; i < l; i++, offset++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            temp2[i] = buf[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        columnMap = new CompactByteArray(temp, temp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // read in numCols and numColGroups
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   206
        numCols = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        offset += 4;
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   208
        numColGroups = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        offset += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // read in the row-number index
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   212
        l = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        offset += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        rowIndex = new short[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        for (int i = 0; i < l; i++, offset+=2) {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   216
            rowIndex[i] = RuleBasedBreakIterator.getShort(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // load in the populated-cells bitmap: index first, then bitmap list
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   220
        l = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        offset += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        rowIndexFlagsIndex = new short[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        for (int i = 0; i < l; i++, offset+=2) {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   224
            rowIndexFlagsIndex[i] = RuleBasedBreakIterator.getShort(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   226
        l = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        offset += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        rowIndexFlags = new int[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        for (int i = 0; i < l; i++, offset+=4) {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   230
            rowIndexFlags[i] = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        // load in the row-shift index
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   234
        l = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        offset += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        rowIndexShifts = new byte[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        for (int i = 0; i < l; i++, offset++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            rowIndexShifts[i] = buf[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // load in the actual state table
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   242
        l = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        offset += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        table = new short[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        for (int i = 0; i < l; i++, offset+=2) {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   246
            table[i] = RuleBasedBreakIterator.getShort(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // finally, prepare the column map for supplementary characters
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   250
        l = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        offset += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        int[] temp3 = new int[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        for (int i = 0; i < l; i++, offset+=4) {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   254
            temp3[i] = RuleBasedBreakIterator.getInt(buf, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        supplementaryCharColumnMap = new SupplementaryCharacterData(temp3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    //=========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    // access to the words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    //=========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Uses the column map to map the character to a column number, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * passes the row and column number to getNextState()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param row The current state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param ch The character whose column we're interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @return The new state to transition to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public final short getNextStateFromCharacter(int row, int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            col = columnMap.elementAt((char)ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            col = supplementaryCharColumnMap.getValue(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return getNextState(row, col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Returns the value in the cell with the specified (logical) row and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * column numbers.  In DictionaryBasedBreakIterator, the row number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * a state number, the column number is an input, and the return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * is the row number of the new state to transition to.  (0 is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * "error" state, and -1 is the "end of word" state in a dictionary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param row The row number of the current state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param col The column number of the input character (0 means "not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * dictionary character")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @return The row number of the new state to transition to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public final short getNextState(int row, int col) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (cellIsPopulated(row, col)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // we map from logical to physical row number by looking up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            // mapping in rowIndex; we map from logical column number to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            // physical column number by looking up a shift value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            // logical row and offsetting the logical column number by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            // the shift amount.  Then we can use internalAt() to actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // get the value out of the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return internalAt(rowIndex[row], col + rowIndexShifts[row]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Given (logical) row and column numbers, returns true if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * cell in that position is populated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   310
    private boolean cellIsPopulated(int row, int col) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // look up the entry in the bitmap index for the specified row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // If it's a negative number, it's the column number of the only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // populated cell in the row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (rowIndexFlagsIndex[row] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return col == -rowIndexFlagsIndex[row];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // if it's a positive number, it's the offset of an entry in the bitmap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        // list.  If the table is more than 32 columns wide, the bitmap is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // successive entries in the bitmap list, so we have to divide the column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // number by 32 and offset the number we got out of the index by the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        // Once we have the appropriate piece of the bitmap, test the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        // bit and return the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            int flags = rowIndexFlags[rowIndexFlagsIndex[row] + (col >> 5)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return (flags & (1 << (col & 0x1f))) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Implementation of getNextState() when we know the specified cell is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * populated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @param row The PHYSICAL row number of the cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param col The PHYSICAL column number of the cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return The value stored in the cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   337
    private short internalAt(int row, int col) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // the table is a one-dimensional array, so this just does the math necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // to treat it as a two-dimensional array (we don't just use a two-dimensional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // array because two-dimensional arrays are inefficient in Java)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return table[row * numCols + col];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
}