jdk/test/java/util/Locale/GenerateKeyList.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright (c) 2007 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 /**
       
    24  * This is a simple program that will generate a key list that can be used as the basis
       
    25  * for a LocaleData file suitable for use with LocaleDataTest.  It always sends its
       
    26  * output to standard out.
       
    27  */
       
    28 import java.util.Locale;
       
    29 import java.util.ResourceBundle;
       
    30 import java.util.Enumeration;
       
    31 import java.io.PrintStream;
       
    32 
       
    33 public class GenerateKeyList {
       
    34     public static void main(String[] args) throws Exception {
       
    35         doOutputFor("sun.util.resources", "CalendarData", System.out);
       
    36         doOutputFor("sun.util.resources", "CurrencyNames", System.out);
       
    37         doOutputFor("sun.util.resources", "LocaleNames", System.out);
       
    38         doOutputFor("sun.util.resources", "TimeZoneNames", System.out);
       
    39         doOutputFor("sun.text.resources", "CollationData", System.out);
       
    40         doOutputFor("sun.text.resources", "FormatData", System.out);
       
    41     };
       
    42 
       
    43     public static void doOutputFor(String packageName,
       
    44             String resourceBundleName, PrintStream out)
       
    45                     throws Exception {
       
    46         Locale[] availableLocales = Locale.getAvailableLocales();
       
    47 
       
    48         ResourceBundle bundle = ResourceBundle.getBundle(packageName +
       
    49                         resourceBundleName, new Locale("", "", ""));
       
    50         dumpResourceBundle(resourceBundleName + "/", bundle, out);
       
    51         for (int i = 0; i < availableLocales.length; i++) {
       
    52             bundle = ResourceBundle.getBundle(packageName + resourceBundleName,
       
    53                             availableLocales[i]);
       
    54             dumpResourceBundle(resourceBundleName + "/" + availableLocales[i].toString(),
       
    55                             bundle, out);
       
    56         }
       
    57     }
       
    58 
       
    59     public static void dumpResourceBundle(String pathName, ResourceBundle bundle,
       
    60                     PrintStream out) throws Exception {
       
    61         Enumeration keys = bundle.getKeys();
       
    62         while(keys.hasMoreElements()) {
       
    63             String key = (String)(keys.nextElement());
       
    64             dumpResource(pathName + "/" + key, bundle.getObject(key), out);
       
    65         }
       
    66     }
       
    67 
       
    68     public static void dumpResource(String pathName, Object resource, PrintStream out)
       
    69                     throws Exception {
       
    70         if (resource instanceof String[]) {
       
    71             String[] stringList = (String[])resource;
       
    72             for (int i = 0; i < stringList.length; i++)
       
    73                 out.println(pathName + "/" + i);
       
    74         }
       
    75         else if (resource instanceof String[][]) {
       
    76             String[][] stringArray = (String[][])resource;
       
    77             if (pathName.startsWith("TimeZoneNames")) {
       
    78                 for (int i = 0; i < stringArray.length; i++)
       
    79                     for (int j = 0; j < stringArray[i].length; j++)
       
    80                         out.println(pathName + "/" + i + "/" + j);
       
    81             }
       
    82             else {
       
    83                 for (int i = 0; i < stringArray.length; i++)
       
    84                     out.println(pathName + "/" + stringArray[i][0]);
       
    85             }
       
    86         }
       
    87         else
       
    88             out.println(pathName);
       
    89     }
       
    90 }