jdk/test/sun/text/resources/LocaleDataTest.java
author mikejwre
Thu, 03 Dec 2009 12:52:46 -0800
changeset 4400 6f7b7cb74a72
parent 891 8ef167bd8428
child 4600 d49895808acd
permissions -rw-r--r--
Added tag jdk7-b77 for changeset a9713976af74
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright (c) 2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4052473 4052679 4055602 4066550 4067619 4068012 4068073 4070174 4070452
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *      4070178 4070450 4070695 4070725 4070795 4071003 4071183 4071782 4072013
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *      4072388 4072773 4075404 4084356 4087238 4092361 4094033 4094371 4098518
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *      4099810 4103218 4103220 4103861 4112136 4113638 4113654 4117054 4122468
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *      4122840 4139860 4156708 4175306 4215747 4209960 4290801 4900884 4942982
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *      4518811 4945388 4936845 4794068 4461740 4965260 4984277 4826794 5032580
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *      5102005 5074431 6182685 6208712 6277020 6245766 6351682 6386647 6379382
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *      6414459 6455680 6498742 6558863 6488119 6547501 6497154 6558856 6481177
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *      6379214 6485516 6486607 4225362 4494727 6533691 6531591 6531593 6570259
891
8ef167bd8428 6509039: Swedish localization has incorrect am/pm markers in FormatData_sv
jtusla
parents: 2
diff changeset
    34
 *      6509039
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @summary Verify locale data
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Portions copyright (c) 2007 Sun Microsystems, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The original version of this source code and documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Permission to use, copy, modify, and distribute this software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * and its documentation for NON-COMMERCIAL purposes and without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * fee is hereby granted provided that this copyright notice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * appears in all copies. Please refer to the file "copyright.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * for further important copyright and licensing information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
/*    This test is a generalized test for verifying changes to the locale data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *    It is driven by an external file that specifies the particular pieces of locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *    data to check.  That file is in .properties file format: a series of key/value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *    pairs delimited by newline characters, with the keys separated from the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *    by = signs.  The keys are similar in syntax to a Unix pathname, with keys at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *    successive levels of containment in the resource-data hierarchy separated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *    slashes.  The file is in ISO 8859-1 encoding, with control characters and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *    non-ASCII characters denoted with backslash-u escape sequences.  The program also allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *    blank lines and comment lines to be interspersed with the data.  Comment lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *    begin with '#'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *    A data file for this test would look something like this:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *        FormatData//MonthNames/0=January
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *        FormatData//MonthNames/1=February
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *        LocaleNames//US=United States
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *        LocaleNames//FR=France
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *        FormatData/fr_FR/MonthNames/0=janvier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *        FormatData/fr_FR/MonthNames/1=f\u00e9vrier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *        LocaleNames/fr_FR/US=\u00c9tats-Unis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *        LocaleNames/fr_FR/FR=France</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *    The command-line syntax of this test is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *        <tt>java LocaleDataTest [-w] [{ -s | <filename> }]</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *    This program always sends its results to standard output.   If -w is not specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *    this program prints out only the differences between the data file and the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *    resource data.  If -w is specified, the program prints out every entry, comment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *    and blank line from the data file.  Where there is a difference between the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *    file and the resource data, the data is the data from the resources.  This feature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *    can be used to quickly generate a new data file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *    The user can specify an optional filename or -s.  If the user specifies a filename,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *    the program uses that file as the data file.  If the user specifies -s, the program
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *    reads its input from standard input rather than from a file.  If the user specifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *    neither, the program reads its input from a file called LocaleData in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *    directory the program itself resides in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *    The -nothrow option prevents the program from throwing an exception when it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *    gets an error.  -w implies -nothrow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *    Other command-line options can be specified, but are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *    It's important to note what this test will NOT test.  Certain changes to the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *    data are meant to have certain effects on the internationalization frameworks.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *    instance, we could ensure round-trip formatting/parsing integrity for the full
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *    date/time format of SimpleDateFormat by making sure that the full date and time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *    patterns include sufficient data.  The test of this is not whether changes were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *    made to the locale data; it's whether using this data gives round-trip integrity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *    Likewise, changing the currency patterns to use \u00a4 instead of local currency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *    symbols isn't something that can be tested by this test; instead, you want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *    actually format currency values and make sure the proper currency symbol was used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *    This test by itself doesn't do an exhaustive comparison of locale data.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *    possible to do this manually, however:  Use the GenerateKeyList tool to produce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *    a complete list of keys for the two versions of the locales you want to compare,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *    and then diff them.  This will flag additions and deletions.  Generate a data file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *    for the base version of the data using the -w option and the output from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *    GenerateKeyList, and then use the resultant file as the data file when you run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *    this test against the new version of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
import java.util.ResourceBundle.Control;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
import java.util.MissingResourceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
public class LocaleDataTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // set up our flags and our input and output streams based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // command-line arguments (exceptions generated here will propagate out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // to the environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        BufferedReader in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        PrintWriter out = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        boolean writeNewFile = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        boolean doThrow = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (int i = 0; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (args[i].equals("-w")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                writeNewFile = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                doThrow = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            else if (args[i].equals("-nothrow"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                doThrow = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            else if (args[i].equals("-s") && in == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                in = new BufferedReader(new EscapeReader(new InputStreamReader(System.in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                "ISO8859_1")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            else if (!args[i].startsWith("-") && in == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                in = new BufferedReader(new EscapeReader(new InputStreamReader(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                FileInputStream(args[i]), "ISO8859_1")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (in == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            File localeData = new File(System.getProperty("test.src", "."), "LocaleData");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            in = new BufferedReader(new EscapeReader(new InputStreamReader(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                            FileInputStream(localeData), "ISO8859_1")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        out = new PrintWriter(new EscapeWriter(new OutputStreamWriter(System.out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        "ISO8859_1")), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // perform the actual test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int errorCount = doTest(in, out, writeNewFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // write out the error count, and throw an exception out into the environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // if there were any errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (errorCount != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (!writeNewFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                out.println("Test failed.  " + errorCount + " errors.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (doThrow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                throw new Exception("Test failed.  " + errorCount + " errors.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        else if (!writeNewFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    static int doTest(BufferedReader in, PrintWriter out, boolean writeNewFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int errorCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        String key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        String expectedValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        String line = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        while (line != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (line.startsWith("#") || line.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                if (writeNewFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    out.println(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                int index  = line.indexOf("=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    key = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    expectedValue = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    key = line.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    if (index + 1 == line.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        expectedValue = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        expectedValue = line.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                if (!processLine(key, expectedValue, out, writeNewFile))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    ++errorCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            line = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return errorCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    static boolean processLine(String key, String expectedValue, PrintWriter out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    boolean writeNewFile) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        String rbName, localeName, resTag, qualifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        String language = "", country = "", variant = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int index, oldIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        index = key.indexOf("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (index == -1 || index + 1 == key.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new Exception("Malformed input file: no slashes in \"" + key + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        rbName = key.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        oldIndex = index + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        index = key.indexOf("/", oldIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (index == -1 || index + 1 == key.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new Exception("Malformed input file: \"" + key + "\" is missing locale name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        localeName = key.substring(oldIndex, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (localeName.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            language = localeName.substring(0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (localeName.length() > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                country = localeName.substring(3, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                if (localeName.length() > 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    variant = localeName.substring(6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        oldIndex = index + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        index = key.indexOf("/", oldIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (index == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            index = key.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        resTag = key.substring(oldIndex, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (index < key.length() - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            qualifier = key.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            qualifier = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        String retrievedValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        Object resource = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            String fullName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (rbName.equals("CalendarData")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    || rbName.equals("CurrencyNames")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    || rbName.equals("LocaleNames")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    || rbName.equals("TimeZoneNames")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                fullName = "sun.util.resources." + rbName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                fullName = "sun.text.resources." + rbName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            ResourceBundle bundle = ResourceBundle.getBundle(fullName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                           new Locale(language, country, variant),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                           ResourceBundle.Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            resource = bundle.getObject(resTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (resource != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (resource instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                retrievedValue = (String)resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            else if (resource instanceof String[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                int element = Integer.valueOf(qualifier).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                String[] stringList = (String[])resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                if (element >= 0 || element < stringList.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    retrievedValue = stringList[element];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            else if (resource instanceof String[][]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                String[][] stringArray = (String[][])resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                int slash = qualifier.indexOf("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                if (slash == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    for (int i = 0; i < stringArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        if (stringArray[i][0].equals(qualifier))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                            retrievedValue = stringArray[i][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    int row = Integer.valueOf(qualifier.substring(0, slash)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    int column = Integer.valueOf(qualifier.substring(slash + 1)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    if (row >= 0 || row < stringArray.length || column >= 0 || column <
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                    stringArray[row].length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        retrievedValue = stringArray[row][column];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (retrievedValue == null || !retrievedValue.equals(expectedValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (retrievedValue == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                retrievedValue = "<MISSING!>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (writeNewFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                out.println(key + "=" + retrievedValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                out.println("Mismatch in " + key + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                out.println("  file = \"" + expectedValue + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                out.println("   jvm = \"" + retrievedValue + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (writeNewFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                out.println(key + "=" + expectedValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
class EscapeReader extends FilterReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public EscapeReader(Reader in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (buffer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            String b = buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            int result = b.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (b.length() > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                buffer = new StringBuffer(b.substring(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            int result = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (result != '\\')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                buffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                result = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                buffer.append((char)result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                if (result == 'u') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        result = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        if (result == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        buffer.append((char)result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    String number = buffer.toString().substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    result = Integer.parseInt(number, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                return '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public int read(char[] cbuf, int start, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        int end = start + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        while (c != -1 && p < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (c != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                cbuf[p++] = (char)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (c == -1 && p == start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return p - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private StringBuffer buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
class EscapeWriter extends FilterWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public EscapeWriter(Writer out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public void write(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if ((c >= ' ' && c <= '\u007e') || c == '\r' || c == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            super.write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            super.write('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            super.write('u');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            String number = Integer.toHexString(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (number.length() < 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                number = zeros.substring(0, 4 - number.length()) + number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            super.write(number.charAt(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            super.write(number.charAt(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            super.write(number.charAt(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            super.write(number.charAt(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public void write(char[] cbuf, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int end = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        while (off < end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            write(cbuf[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    public void write(String str, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        int end = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        while (off < end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            write(str.charAt(off++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    private static String zeros = "0000";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
}