src/java.base/share/classes/java/text/PatternEntry.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
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) 1996, 2000, 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, 1997 - 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.lang.Character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Utility class for normalizing and merging patterns for collation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This is to be used with MergeCollation for adding patterns to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * existing rule table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see        MergeCollation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author     Mark Davis, Helena Shih
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class PatternEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Gets the current extension, quoted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public void appendQuotedExtension(StringBuffer toAddTo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        appendQuoted(extension,toAddTo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Gets the current chars, quoted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public void appendQuotedChars(StringBuffer toAddTo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        appendQuoted(chars,toAddTo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * WARNING this is used for searching in a Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Because Vector.indexOf doesn't take a comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * this method is ill-defined and ignores strength.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (obj == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        PatternEntry other = (PatternEntry) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        boolean result = chars.equals(other.chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return result;
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 int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return chars.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * For debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        StringBuffer result = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        addToBuffer(result, true, false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Gets the strength of the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    final int getStrength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return strength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Gets the expanding characters of the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    final String getExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return extension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Gets the core characters of the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    final String getChars() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return chars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // ===== privates =====
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    void addToBuffer(StringBuffer toAddTo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                     boolean showExtension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                     boolean showWhiteSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                     PatternEntry lastEntry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (showWhiteSpace && toAddTo.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (strength == Collator.PRIMARY || lastEntry != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                toAddTo.append('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                toAddTo.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (lastEntry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            toAddTo.append('&');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (showWhiteSpace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                toAddTo.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            lastEntry.appendQuotedChars(toAddTo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            appendQuotedExtension(toAddTo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (showWhiteSpace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                toAddTo.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        switch (strength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        case Collator.IDENTICAL: toAddTo.append('='); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        case Collator.TERTIARY:  toAddTo.append(','); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        case Collator.SECONDARY: toAddTo.append(';'); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        case Collator.PRIMARY:   toAddTo.append('<'); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        case RESET: toAddTo.append('&'); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        case UNSET: toAddTo.append('?'); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (showWhiteSpace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            toAddTo.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        appendQuoted(chars,toAddTo);
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   144
        if (showExtension && !extension.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            toAddTo.append('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            appendQuoted(extension,toAddTo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    static void appendQuoted(String chars, StringBuffer toAddTo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        boolean inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        char ch = chars.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (Character.isSpaceChar(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            inQuote = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            toAddTo.append('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
          if (PatternEntry.isSpecialChar(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                inQuote = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                toAddTo.append('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    case 0x0010: case '\f': case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    case '\t': case '\n':  case '@':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    inQuote = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    toAddTo.append('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                case '\'':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    inQuote = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    toAddTo.append('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        inQuote = false; toAddTo.append('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        toAddTo.append(chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (inQuote)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            toAddTo.append('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    //========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // Parsing a pattern into a list of PatternEntries....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    //========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    PatternEntry(int strength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                 StringBuffer chars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                 StringBuffer extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        this.strength = strength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this.chars = chars.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        this.extension = (extension.length() > 0) ? extension.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                                  : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    static class Parser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        private String pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        private int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        public Parser(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            this.pattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            this.i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        public PatternEntry next() throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            int newStrength = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            newChars.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            newExtension.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            boolean inChars = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            boolean inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        mainLoop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            while (i < pattern.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                char ch = pattern.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    if (ch == '\'') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        if (newChars.length() == 0) newChars.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        else if (inChars) newChars.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        else newExtension.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                } else switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                case '=': if (newStrength != UNSET) break mainLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    newStrength = Collator.IDENTICAL; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                case ',': if (newStrength != UNSET) break mainLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    newStrength = Collator.TERTIARY; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                case ';': if (newStrength != UNSET) break mainLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    newStrength = Collator.SECONDARY; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                case '<': if (newStrength != UNSET) break mainLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    newStrength = Collator.PRIMARY; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                case '&': if (newStrength != UNSET) break mainLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    newStrength = RESET; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                case '\f':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                case ' ': break; // skip whitespace TODO use Character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                case '/': inChars = false; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                case '\'':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    inQuote = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    ch = pattern.charAt(++i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    if (newChars.length() == 0) newChars.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    else if (inChars) newChars.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    else newExtension.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    if (newStrength == UNSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        throw new ParseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                            ("missing char (=,;<&) : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                             pattern.substring(i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                (i+10 < pattern.length()) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                 i+10 : pattern.length()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                             i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    if (PatternEntry.isSpecialChar(ch) && (inQuote == false))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        throw new ParseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                            ("Unquoted punctuation character : " + Integer.toString(ch, 16), i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    if (inChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        newChars.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        newExtension.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (newStrength == UNSET)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (newChars.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                throw new ParseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    ("missing chars (=,;<&): " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                      pattern.substring(i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                          (i+10 < pattern.length()) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                           i+10 : pattern.length()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                     i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return new PatternEntry(newStrength, newChars, newExtension);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // We re-use these objects in order to improve performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        private StringBuffer newChars = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        private StringBuffer newExtension = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    static boolean isSpecialChar(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return ((ch == '\u0020') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                ((ch <= '\u002F') && (ch >= '\u0022')) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                ((ch <= '\u003F') && (ch >= '\u003A')) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                ((ch <= '\u0060') && (ch >= '\u005B')) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                ((ch <= '\u007E') && (ch >= '\u007B')));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    static final int RESET = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    static final int UNSET = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    int strength = UNSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    String chars = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    String extension = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
}