?? rbtablebuilder.java
字號:
/* * * %W% %E% * * Portions Copyright 2000-2008 Sun Microsystems, Inc. All Rights * Reserved. Use is subject to license terms. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. *//* * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved * * The original version of this source code and documentation is copyrighted * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These * materials are provided under terms of a License Agreement between Taligent * and Sun. This technology is protected by multiple US and International * patents. This notice and attribution to Taligent may not be removed. * Taligent is a registered trademark of Taligent, Inc. * */package java.text;import java.util.Vector;import sun.text.CompactIntArray;import sun.text.IntHashtable;import sun.text.Normalizer;import sun.text.ComposedCharIter;import sun.text.NormalizerUtilities;/** * This class contains all the code to parse a RuleBasedCollator pattern * and build a RBCollationTables object from it. A particular instance * of tis class exists only during the actual build process-- once an * RBCollationTables object has been built, the RBTableBuilder object * goes away. This object carries all of the state which is only needed * during the build process, plus a "shadow" copy of all of the state * that will go into the tables object itself. This object communicates * with RBCollationTables through a separate class, RBCollationTables.BuildAPI, * this is an inner class of RBCollationTables and provides a separate * private API for communication with RBTableBuilder. * This class isn't just an inner class of RBCollationTables itself because * of its large size. For source-code readability, it seemed better for the * builder to have its own source file. */final class RBTableBuilder { public RBTableBuilder(RBCollationTables.BuildAPI tables) { this.tables = tables; } /** * Create a table-based collation object with the given rules. * This is the main function that actually builds the tables and * stores them back in the RBCollationTables object. It is called * ONLY by the RBCollationTables constructor. * @see java.util.RuleBasedCollator#RuleBasedCollator * @exception ParseException If the rules format is incorrect. */ public void build(String pattern, int decmp) throws ParseException { boolean isSource = true; int i = 0; String expChars; String groupChars; if (pattern.length() == 0) throw new ParseException("Build rules empty.", 0); // This array maps Unicode characters to their collation ordering mapping = new CompactIntArray((int)RBCollationTables.UNMAPPED); // Normalize the build rules. Find occurances of all decomposed characters // and normalize the rules before feeding into the builder. By "normalize", // we mean that all precomposed Unicode characters must be converted into // a base character and one or more combining characters (such as accents). // When there are multiple combining characters attached to a base character, // the combining characters must be in their canonical order // Normalizer.Mode mode = NormalizerUtilities.toNormalizerMode(decmp); pattern = Normalizer.normalize(pattern, mode, 0, true); // Build the merged collation entries // Since rules can be specified in any order in the string // (e.g. "c , C < d , D < e , E .... C < CH") // this splits all of the rules in the string out into separate // objects and then sorts them. In the above example, it merges the // "C < CH" rule in just before the "C < D" rule. // mPattern = new MergeCollation(pattern); int order = 0; // Now walk though each entry and add it to my own tables for (i = 0; i < mPattern.getCount(); ++i) { PatternEntry entry = mPattern.getItemAt(i); if (entry != null) { groupChars = entry.getChars(); if (groupChars.length() > 1) { switch(groupChars.charAt(groupChars.length()-1)) { case '@': frenchSec = true; groupChars = groupChars.substring(0, groupChars.length()-1); break; case '!': seAsianSwapping = true; groupChars = groupChars.substring(0, groupChars.length()-1); break; } } order = increment(entry.getStrength(), order); expChars = entry.getExtension(); if (expChars.length() != 0) { addExpandOrder(groupChars, expChars, order); } else if (groupChars.length() > 1) { addContractOrder(groupChars, order); } else { char ch = groupChars.charAt(0); addOrder(ch, order); } } } addComposedChars(); commit(); mapping.compact(); tables.fillInTables(frenchSec, seAsianSwapping, mapping, contractTable, expandTable, contractFlags, maxSecOrder, maxTerOrder); } /** Add expanding entries for pre-composed unicode characters so that this * collator can be used reasonably well with decomposition turned off. */ private void addComposedChars() throws ParseException { StringBuffer buf = new StringBuffer(1); // Iterate through all of the pre-composed characters in Unicode ComposedCharIter iter = new ComposedCharIter(false, Normalizer.IGNORE_HANGUL); while (iter.hasNext()) { char c = iter.next(); if (getCharOrder(c) == RBCollationTables.UNMAPPED) { // // We don't already have an ordering for this pre-composed character. // // First, see if the decomposed string is already in our // tables as a single contracting-string ordering. // If so, just map the precomposed character to that order. // // TODO: What we should really be doing here is trying to find the // longest initial substring of the decomposition that is present // in the tables as a contracting character sequence, and find its // ordering. Then do this recursively with the remaining chars // so that we build a list of orderings, and add that list to // the expansion table. // That would be more correct but also significantly slower, so // I'm not totally sure it's worth doing. // String s = iter.decomposition(); int contractOrder = getContractOrder(s); if (contractOrder != RBCollationTables.UNMAPPED) { addOrder(c, contractOrder); } else { // // We don't have a contracting ordering for the entire string // that results from the decomposition, but if we have orders // for each individual character, we can add an expanding // table entry for the pre-composed character // boolean allThere = true; for (int i = 0; i < s.length(); i++) { if (getCharOrder(s.charAt(i)) == RBCollationTables.UNMAPPED) { allThere = false; break; } } if (allThere) { buf.setLength(0); buf.append(c); addExpandOrder(buf.toString(), s, RBCollationTables.UNMAPPED); } } } } } /** * Look up for unmapped values in the expanded character table. * * When the expanding character tables are built by addExpandOrder, * it doesn't know what the final ordering of each character * in the expansion will be. Instead, it just puts the raw character * code into the table, adding CHARINDEX as a flag. Now that we've * finished building the mapping table, we can go back and look up * that character to see what its real collation order is and * stick that into the expansion table. That lets us avoid doing * a two-stage lookup later. */ private final void commit() { if (expandTable != null) { for (int i = 0; i < expandTable.size(); i++) { int[] valueList = (int [])expandTable.elementAt(i); for (int j = 0; j < valueList.length; j++) { int order = valueList[j]; if (order < RBCollationTables.EXPANDCHARINDEX && order > CHARINDEX) { // found a expanding character that isn't filled in yet char ch = (char)(order - CHARINDEX); // Get the real values for the non-filled entry int realValue = getCharOrder(ch); if (realValue == RBCollationTables.UNMAPPED) { // The real value is still unmapped, maybe it's ignorable valueList[j] = IGNORABLEMASK & ch; } else { // just fill in the value valueList[j] = realValue; } } } } } } /** * Increment of the last order based on the comparison level. */ private final int increment(int aStrength, int lastValue) { switch(aStrength) { case Collator.PRIMARY: // increment priamry order and mask off secondary and tertiary difference lastValue += PRIMARYORDERINCREMENT; lastValue &= RBCollationTables.PRIMARYORDERMASK; isOverIgnore = true; break;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -