📄 rulebasedcollator.java
字号:
/* RuleBasedCollator.java -- Concrete Collator Class Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version. GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.text;import java.util.ArrayList;import java.util.HashMap;/* Written using "Java Class Libraries", 2nd edition, plus online * API docs for JDK 1.2 from http://www.javasoft.com. * Status: Believed complete and correct *//** * This class is a concrete subclass of <code>Collator</code> suitable * for string collation in a wide variety of languages. An instance of * this class is normally returned by the <code>getInstance</code> method * of <code>Collator</code> with rules predefined for the requested * locale. However, an instance of this class can be created manually * with any desired rules. * <p> * Rules take the form of a <code>String</code> with the following syntax * <ul> * <li> Modifier: '@'</li> * <li> Relation: '<' | ';' | ',' | '=' : <text></li> * <li> Reset: '&' : <text></li> * </ul> * The modifier character indicates that accents sort backward as is the * case with French. The modifier applies to all rules <b>after</b> * the modifier but before the next primary sequence. If placed at the end * of the sequence if applies to all unknown accented character. * The relational operators specify how the text * argument relates to the previous term. The relation characters have * the following meanings: * <ul> * <li>'<' - The text argument is greater than the prior term at the primary * difference level.</li> * <li>';' - The text argument is greater than the prior term at the secondary * difference level.</li> * <li>',' - The text argument is greater than the prior term at the tertiary * difference level.</li> * <li>'=' - The text argument is equal to the prior term</li> * </ul> * <p> * As for the text argument itself, this is any sequence of Unicode * characters not in the following ranges: 0x0009-0x000D, 0x0020-0x002F, * 0x003A-0x0040, 0x005B-0x0060, and 0x007B-0x007E. If these characters are * desired, they must be enclosed in single quotes. If any whitespace is * encountered, it is ignored. (For example, "a b" is equal to "ab"). * <p> * The reset operation inserts the following rule at the point where the * text argument to it exists in the previously declared rule string. This * makes it easy to add new rules to an existing string by simply including * them in a reset sequence at the end. Note that the text argument, or * at least the first character of it, must be present somewhere in the * previously declared rules in order to be inserted properly. If this * is not satisfied, a <code>ParseException</code> will be thrown. * <p> * This system of configuring <code>RuleBasedCollator</code> is needlessly * complex and the people at Taligent who developed it (along with the folks * at Sun who accepted it into the Java standard library) deserve a slow * and agonizing death. * <p> * Here are a couple of example of rule strings: * <p> * "< a < b < c" - This string says that a is greater than b which is * greater than c, with all differences being primary differences. * <p> * "< a,A < b,B < c,C" - This string says that 'A' is greater than 'a' with * a tertiary strength comparison. Both 'b' and 'B' are greater than 'a' and * 'A' during a primary strength comparison. But 'B' is greater than 'b' * under a tertiary strength comparison. * <p> * "< a < c & a < b " - This sequence is identical in function to the * "< a < b < c" rule string above. The '&' reset symbol indicates that * the rule "< b" is to be inserted after the text argument "a" in the * previous rule string segment. * <p> * "< a < b & y < z" - This is an error. The character 'y' does not appear * anywhere in the previous rule string segment so the rule following the * reset rule cannot be inserted. * <p> * "< a & A @ < e & E < f& F" - This sequence is equivalent to the following * "< a & A < E & e < f & F". * <p> * For a description of the various comparison strength types, see the * documentation for the <code>Collator</code> class. * <p> * As an additional complication to this already overly complex rule scheme, * if any characters precede the first rule, these characters are considered * ignorable. They will be treated as if they did not exist during * comparisons. For example, "- < a < b ..." would make '-' an ignorable * character such that the strings "high-tech" and "hightech" would * be considered identical. * <p> * A <code>ParseException</code> will be thrown for any of the following * conditions: * <ul> * <li>Unquoted punctuation characters in a text argument.</li> * <li>A relational or reset operator not followed by a text argument</li> * <li>A reset operator where the text argument is not present in * the previous rule string section.</li> * </ul> * * @author Aaron M. Renn (arenn@urbanophile.com) * @author Tom Tromey (tromey@cygnus.com) * @author Guilhem Lavaux (guilhem@kaffe.org) */public class RuleBasedCollator extends Collator{ /** * This class describes what rank has a character (or a sequence of characters) * in the lexicographic order. Each element in a rule has a collation element. */ static final class CollationElement { String key; int primary; short secondary; short tertiary; short equality; boolean ignore; String expansion; CollationElement(String key, int primary, short secondary, short tertiary, short equality, String expansion, boolean ignore) { this.key = key; this.primary = primary; this.secondary = secondary; this.tertiary = tertiary; this.equality = equality; this.ignore = ignore; this.expansion = expansion; } int getValue() { return (primary << 16) + (secondary << 8) + tertiary; } } /** * Basic collation instruction (internal format) to build the series of * collation elements. It contains an instruction which specifies the new * state of the generator. The sequence of instruction should not contain * RESET (it is used by * {@link #mergeRules(int,java.lang.String,java.util.ArrayList,java.util.ArrayList)}) * as a temporary state while merging two sets of instructions. */ static final class CollationSorter { static final int GREATERP = 0; static final int GREATERS = 1; static final int GREATERT = 2; static final int EQUAL = 3; static final int RESET = 4; static final int INVERSE_SECONDARY = 5; int comparisonType; String textElement; int hashText; int offset; boolean ignore; String expansionOrdering; } /** * This the the original rule string. */ private String rules; /** * This is the table of collation element values */ private Object[] ce_table; /** * Quick-prefix finder. */ HashMap prefix_tree; /** * This is the value of the last sequence entered into * <code>ce_table</code>. It is used to compute the * ordering value of unspecified character. */ private int last_primary_value; /** * This is the value of the last secondary sequence of the * primary 0, entered into * <code>ce_table</code>. It is used to compute the * ordering value of an unspecified accented character. */ private int last_tertiary_value; /** * This variable is true if accents need to be sorted * in the other direction. */ private boolean inverseAccentComparison; /** * This collation element is special to unknown sequence. * The JDK uses it to mark and sort the characters which has * no collation rules. */ static final CollationElement SPECIAL_UNKNOWN_SEQ = new CollationElement("", (short) 32767, (short) 0, (short) 0, (short) 0, null, false); /** * This method initializes a new instance of <code>RuleBasedCollator</code> * with the specified collation rules. Note that an application normally * obtains an instance of <code>RuleBasedCollator</code> by calling the * <code>getInstance</code> method of <code>Collator</code>. That method * automatically loads the proper set of rules for the desired locale. * * @param rules The collation rule string. * * @exception ParseException If the rule string contains syntax errors. */ public RuleBasedCollator(String rules) throws ParseException { if (rules.equals("")) throw new ParseException("empty rule set", 0); this.rules = rules; buildCollationVector(parseString(rules)); buildPrefixAccess(); } /** * This method returns the number of common characters at the beginning * of the string of the two parameters. * * @param prefix A string considered as a prefix to test against * the other string. * @param s A string to test the prefix against. * @return The number of common characters. */ static int findPrefixLength(String prefix, String s) { int index; int len = prefix.length(); for (index = 0; index < len && index < s.length(); ++index) { if (prefix.charAt(index) != s.charAt(index)) return index; } return index; } /** * Here we are merging two sets of sorting instructions: 'patch' into 'main'. This methods * checks whether it is possible to find an anchor point for the rules to be merged and * then insert them at that precise point. * * @param offset Offset in the string containing rules of the beginning of the rules * being merged in. * @param starter Text of the rules being merged. * @param main Repository of all already parsed rules. * @param patch Rules to be merged into the repository. * @throws ParseException if it is impossible to find an anchor point for the new rules. */ private void mergeRules(int offset, String starter, ArrayList main, ArrayList patch) throws ParseException { int insertion_point = -1; int max_length = 0; /* We must check that no rules conflict with another already present. If it * is the case delete the old rule. */ /* For the moment good old O(N^2) algorithm. */ for (int i = 0; i < patch.size(); i++) { int j = 0; while (j < main.size()) { CollationSorter rule1 = (CollationSorter) patch.get(i); CollationSorter rule2 = (CollationSorter) main.get(j); if (rule1.textElement.equals(rule2.textElement)) main.remove(j); else j++; } } // Find the insertion point... O(N) for (int i = 0; i < main.size(); i++) { CollationSorter sorter = (CollationSorter) main.get(i); int length = findPrefixLength(starter, sorter.textElement);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -