Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/text/PatternEntry.java
38829 views
/*1* Copyright (c) 1996, 2000, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved27* (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved28*29* The original version of this source code and documentation is copyrighted30* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These31* materials are provided under terms of a License Agreement between Taligent32* and Sun. This technology is protected by multiple US and International33* patents. This notice and attribution to Taligent may not be removed.34* Taligent is a registered trademark of Taligent, Inc.35*36*/3738package java.text;3940import java.lang.Character;4142/**43* Utility class for normalizing and merging patterns for collation.44* This is to be used with MergeCollation for adding patterns to an45* existing rule table.46* @see MergeCollation47* @author Mark Davis, Helena Shih48*/4950class PatternEntry {51/**52* Gets the current extension, quoted53*/54public void appendQuotedExtension(StringBuffer toAddTo) {55appendQuoted(extension,toAddTo);56}5758/**59* Gets the current chars, quoted60*/61public void appendQuotedChars(StringBuffer toAddTo) {62appendQuoted(chars,toAddTo);63}6465/**66* WARNING this is used for searching in a Vector.67* Because Vector.indexOf doesn't take a comparator,68* this method is ill-defined and ignores strength.69*/70public boolean equals(Object obj) {71if (obj == null) return false;72PatternEntry other = (PatternEntry) obj;73boolean result = chars.equals(other.chars);74return result;75}7677public int hashCode() {78return chars.hashCode();79}8081/**82* For debugging.83*/84public String toString() {85StringBuffer result = new StringBuffer();86addToBuffer(result, true, false, null);87return result.toString();88}8990/**91* Gets the strength of the entry.92*/93final int getStrength() {94return strength;95}9697/**98* Gets the expanding characters of the entry.99*/100final String getExtension() {101return extension;102}103104/**105* Gets the core characters of the entry.106*/107final String getChars() {108return chars;109}110111// ===== privates =====112113void addToBuffer(StringBuffer toAddTo,114boolean showExtension,115boolean showWhiteSpace,116PatternEntry lastEntry)117{118if (showWhiteSpace && toAddTo.length() > 0)119if (strength == Collator.PRIMARY || lastEntry != null)120toAddTo.append('\n');121else122toAddTo.append(' ');123if (lastEntry != null) {124toAddTo.append('&');125if (showWhiteSpace)126toAddTo.append(' ');127lastEntry.appendQuotedChars(toAddTo);128appendQuotedExtension(toAddTo);129if (showWhiteSpace)130toAddTo.append(' ');131}132switch (strength) {133case Collator.IDENTICAL: toAddTo.append('='); break;134case Collator.TERTIARY: toAddTo.append(','); break;135case Collator.SECONDARY: toAddTo.append(';'); break;136case Collator.PRIMARY: toAddTo.append('<'); break;137case RESET: toAddTo.append('&'); break;138case UNSET: toAddTo.append('?'); break;139}140if (showWhiteSpace)141toAddTo.append(' ');142appendQuoted(chars,toAddTo);143if (showExtension && extension.length() != 0) {144toAddTo.append('/');145appendQuoted(extension,toAddTo);146}147}148149static void appendQuoted(String chars, StringBuffer toAddTo) {150boolean inQuote = false;151char ch = chars.charAt(0);152if (Character.isSpaceChar(ch)) {153inQuote = true;154toAddTo.append('\'');155} else {156if (PatternEntry.isSpecialChar(ch)) {157inQuote = true;158toAddTo.append('\'');159} else {160switch (ch) {161case 0x0010: case '\f': case '\r':162case '\t': case '\n': case '@':163inQuote = true;164toAddTo.append('\'');165break;166case '\'':167inQuote = true;168toAddTo.append('\'');169break;170default:171if (inQuote) {172inQuote = false; toAddTo.append('\'');173}174break;175}176}177}178toAddTo.append(chars);179if (inQuote)180toAddTo.append('\'');181}182183//========================================================================184// Parsing a pattern into a list of PatternEntries....185//========================================================================186187PatternEntry(int strength,188StringBuffer chars,189StringBuffer extension)190{191this.strength = strength;192this.chars = chars.toString();193this.extension = (extension.length() > 0) ? extension.toString()194: "";195}196197static class Parser {198private String pattern;199private int i;200201public Parser(String pattern) {202this.pattern = pattern;203this.i = 0;204}205206public PatternEntry next() throws ParseException {207int newStrength = UNSET;208209newChars.setLength(0);210newExtension.setLength(0);211212boolean inChars = true;213boolean inQuote = false;214mainLoop:215while (i < pattern.length()) {216char ch = pattern.charAt(i);217if (inQuote) {218if (ch == '\'') {219inQuote = false;220} else {221if (newChars.length() == 0) newChars.append(ch);222else if (inChars) newChars.append(ch);223else newExtension.append(ch);224}225} else switch (ch) {226case '=': if (newStrength != UNSET) break mainLoop;227newStrength = Collator.IDENTICAL; break;228case ',': if (newStrength != UNSET) break mainLoop;229newStrength = Collator.TERTIARY; break;230case ';': if (newStrength != UNSET) break mainLoop;231newStrength = Collator.SECONDARY; break;232case '<': if (newStrength != UNSET) break mainLoop;233newStrength = Collator.PRIMARY; break;234case '&': if (newStrength != UNSET) break mainLoop;235newStrength = RESET; break;236case '\t':237case '\n':238case '\f':239case '\r':240case ' ': break; // skip whitespace TODO use Character241case '/': inChars = false; break;242case '\'':243inQuote = true;244ch = pattern.charAt(++i);245if (newChars.length() == 0) newChars.append(ch);246else if (inChars) newChars.append(ch);247else newExtension.append(ch);248break;249default:250if (newStrength == UNSET) {251throw new ParseException252("missing char (=,;<&) : " +253pattern.substring(i,254(i+10 < pattern.length()) ?255i+10 : pattern.length()),256i);257}258if (PatternEntry.isSpecialChar(ch) && (inQuote == false))259throw new ParseException260("Unquoted punctuation character : " + Integer.toString(ch, 16), i);261if (inChars) {262newChars.append(ch);263} else {264newExtension.append(ch);265}266break;267}268i++;269}270if (newStrength == UNSET)271return null;272if (newChars.length() == 0) {273throw new ParseException274("missing chars (=,;<&): " +275pattern.substring(i,276(i+10 < pattern.length()) ?277i+10 : pattern.length()),278i);279}280281return new PatternEntry(newStrength, newChars, newExtension);282}283284// We re-use these objects in order to improve performance285private StringBuffer newChars = new StringBuffer();286private StringBuffer newExtension = new StringBuffer();287288}289290static boolean isSpecialChar(char ch) {291return ((ch == '\u0020') ||292((ch <= '\u002F') && (ch >= '\u0022')) ||293((ch <= '\u003F') && (ch >= '\u003A')) ||294((ch <= '\u0060') && (ch >= '\u005B')) ||295((ch <= '\u007E') && (ch >= '\u007B')));296}297298299static final int RESET = -2;300static final int UNSET = -1;301302int strength = UNSET;303String chars = "";304String extension = "";305}306307308