jdk/make/src/classes/build/tools/generatebreakiteratordata/GenerateBreakIteratorData.java
author rfield
Mon, 13 Feb 2017 08:50:26 -0800
changeset 43856 fcdebb803c62
parent 36511 9d0388c6b336
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.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ListResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Generates datafile for BreakIterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class GenerateBreakIteratorData {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * Directory where generated data files are put in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static String outputDir = "" ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Unicode data file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static String unicodeData = "UnicodeData.txt";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Locale data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static String language = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static String country = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static String valiant = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static String localeName = "";  /* _language_country_valiant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        /* Parse command-line options */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        processArgs(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        /* Make categoryMap from Unicode data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        CharacterCategory.makeCategoryMap(unicodeData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        /* Generate files */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    65
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    66
            generateFiles();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    67
        } catch (Exception e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    68
            e.printStackTrace();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    69
            System.exit(1);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    70
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    71
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    72
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    73
    private static String localizedBundleName(String pkg, String clazz) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    74
        if (language.length() > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    75
            return pkg + ".ext." + clazz + '_' + language;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    76
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    77
            return pkg + '.' + clazz;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    78
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Generate data files whose names are included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * sun.text.resources.BreakIteratorInfo+<localeName>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    85
    private static void generateFiles() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String[] classNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ResourceBundle rules, info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    89
        info = (ResourceBundle) Class.forName(
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    90
            localizedBundleName("sun.text.resources", "BreakIteratorInfo")).newInstance();
36014
3d50784f2dc2 8148346: Reduce number of packages in jdk.localedata module
naoto
parents: 23010
diff changeset
    91
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        classNames = info.getStringArray("BreakIteratorClasses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    94
        rules = (ResourceBundle) Class.forName(
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36014
diff changeset
    95
            localizedBundleName("sun.text.resources", "BreakIteratorRules")).newInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
    97
        if (info.containsKey("CharacterData")) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
    98
            generateDataFile(info.getString("CharacterData"),
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
    99
                             rules.getString("CharacterBreakRules"),
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   100
                             classNames[0]);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   101
        }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   102
        if (info.containsKey("WordData")) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   103
            generateDataFile(info.getString("WordData"),
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   104
                             rules.getString("WordBreakRules"),
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   105
                             classNames[1]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   107
        if (info.containsKey("LineData")) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   108
            generateDataFile(info.getString("LineData"),
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   109
                             rules.getString("LineBreakRules"),
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   110
                             classNames[2]);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   111
        }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   112
        if (info.containsKey("SentenceData")) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   113
            generateDataFile(info.getString("SentenceData"),
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   114
                             rules.getString("SentenceBreakRules"),
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 10110
diff changeset
   115
                             classNames[3]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Generate a data file for break-iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static void generateDataFile(String datafile, String rule, String builder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        RuleBasedBreakIteratorBuilder bld;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (builder.equals("RuleBasedBreakIterator")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            bld = new RuleBasedBreakIteratorBuilder(rule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } else if (builder.equals("DictionaryBasedBreakIterator")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            bld = new DictionaryBasedBreakIteratorBuilder(rule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new IllegalArgumentException("Invalid break iterator class \"" + builder + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        bld.makeFile(datafile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Parses the specified arguments and sets up the variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static void processArgs(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for (int i = 0; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            String arg = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (arg.equals("-o")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                outputDir = args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } else if (arg.equals("-spec")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                unicodeData = args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } else if (arg.equals("-language")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                language = args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            } else if (arg.equals("-country")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                country = args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } else if (arg.equals("-valiant")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                valiant = args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // Set locale name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        localeName = getLocaleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Make locale name ("_language_country_valiant")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static String getLocaleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (language.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (!country.equals("") || !valiant.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                language = "en";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        sb.append('_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        sb.append(language);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (!country.equals("") || !valiant.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            sb.append('_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            sb.append(country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (!valiant.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                sb.append('_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                sb.append(valiant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Usage: Displayed when an invalid command-line option is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private static void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        System.err.println("Usage: GenerateBreakIteratorData [options]\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        "    -o outputDir                 output directory name\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        "    -spec specname               unicode text filename\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        "  and locale data:\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        "    -lang language               target language name\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        "    -country country             target country name\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        "    -valiant valiant             target valiant name\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Return the path of output directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    static String getOutputDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return outputDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
}