jdk/make/src/classes/build/tools/generatebreakiteratordata/DictionaryBasedBreakIteratorBuilder.java
author rfield
Mon, 13 Feb 2017 08:50:26 -0800
changeset 43856 fcdebb803c62
parent 23010 6dadb192ad81
permissions -rw-r--r--
8174797: jshell tool: invalid module path crashes tool 8174796: jshell tool: regression: user home (tilde) not translated Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21805
diff changeset
     2
 * Copyright (c) 2003, 2013, 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
package build.tools.generatebreakiteratordata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The Builder class for DictionaryBasedBreakIterator inherits almost all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * its functionality from RuleBasedBreakIteratorBuilder, but extends it with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * extra logic to handle the "<dictionary>" token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class DictionaryBasedBreakIteratorBuilder extends RuleBasedBreakIteratorBuilder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * A list of flags indicating which character categories are contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * the dictionary file (this is used to determine which ranges of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * to apply the dictionary to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private boolean[] categoryFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * A CharSet that contains all the characters represented in the dictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private CharSet dictionaryChars = new CharSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private String dictionaryExpression = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public DictionaryBasedBreakIteratorBuilder(String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        super(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * We override handleSpecialSubstitution() to add logic to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * the <dictionary> tag.  If we see a substitution named "<dictionary>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * parse the substitution expression and store the result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * dictionaryChars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected void handleSpecialSubstitution(String replace, String replaceWith,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                             int startPos, String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        super.handleSpecialSubstitution(replace, replaceWith, startPos, description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (replace.equals("<dictionary>")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if (replaceWith.charAt(0) == '(') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                error("Dictionary group can't be enclosed in (", startPos, description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            dictionaryExpression = replaceWith;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            dictionaryChars = CharSet.parseString(replaceWith);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The other half of the logic to handle the dictionary characters happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * here. After the inherited builder has derived the real character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * categories, we set up the categoryFlags array in the iterator. This array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * contains "true" for every character category that includes a dictionary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    81
    protected void buildCharCategories(Vector<String> tempRuleList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        super.buildCharCategories(tempRuleList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        categoryFlags = new boolean[categories.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        for (int i = 0; i < categories.size(); i++) {
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    86
            CharSet cs = categories.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (!(cs.intersection(dictionaryChars).empty())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                categoryFlags[i] = true;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // This function is actually called by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // RuleBasedBreakIteratorBuilder.buildCharCategories(), which is called by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // the function above. This gives us a way to create a separate character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // category for the dictionary characters even when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // RuleBasedBreakIteratorBuilder isn't making a distinction.
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    98
    protected void mungeExpressionList(Hashtable<String, Object> expressions) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        expressions.put(dictionaryExpression, dictionaryChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    void makeFile(String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        super.setAdditionalData(super.toByteArray(categoryFlags));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        super.makeFile(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}