test/sun/text/resources/LocaleDataTest.java

Print this page
rev 5615 : 6336885: RFE: Locale Data Deployment Enhancements
4609153: Provide locale data for Indic locales
5104387: Support for gl_ES locale (galician language)
6337471: desktop/system locale preferences support
7056139: (cal) SPI support for locale-dependent Calendar parameters
7058206: Provide CalendarData SPI for week params and display field value names
7073852: Support multiple scripts for digits and decimal symbols per locale
7079560: [Fmt-Da] Context dependent month names support in SimpleDateFormat
7171324: getAvailableLocales() of locale sensitive services should return the actual availability of locales
7151414: (cal) Support calendar type identification
7168528: LocaleServiceProvider needs to be aware of Locale extensions
7171372: (cal) locale's default Calendar should be created if unknown calendar is specified
Summary: JEP 127: Improve Locale Data Packaging and Adopt Unicode CLDR Data (part 1 w/o Jigsaw. by Naoto Sato and Masayoshi Okutsu)

@@ -143,10 +143,13 @@
 import java.util.ResourceBundle.Control;
 import java.util.MissingResourceException;
 
 public class LocaleDataTest
 {
+    static final String TEXT_RESOURCES_PACKAGE ="sun.text.resources";
+    static final String UTIL_RESOURCES_PACKAGE ="sun.util.resources";
+
     public static void main(String[] args) throws Exception {
 
         // set up our flags and our input and output streams based on the
         // command-line arguments (exceptions generated here will propagate out
         // to the environment)

@@ -284,23 +287,23 @@
             String fullName = null;
             if (rbName.equals("CalendarData")
                     || rbName.equals("CurrencyNames")
                     || rbName.equals("LocaleNames")
                     || rbName.equals("TimeZoneNames")) {
-                fullName = "sun.util.resources." + rbName;
+                fullName = UTIL_RESOURCES_PACKAGE + "." + rbName;
             } else {
-                fullName = "sun.text.resources." + rbName;
+                fullName = TEXT_RESOURCES_PACKAGE + "." + rbName;
             }
             Locale locale;
             if (use_tag) {
                 locale = Locale.forLanguageTag(localeName);
             } else {
                 locale = new Locale(language, country, variant);
             }
             ResourceBundle bundle = ResourceBundle.getBundle(fullName,
                            locale,
-                           ResourceBundle.Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
+                                                             JRELocaleResourceBundleControl.INSTANCE);
             resource = bundle.getObject(resTag);
         }
         catch (MissingResourceException e) {
         }
 

@@ -309,11 +312,11 @@
                 retrievedValue = (String)resource;
             }
             else if (resource instanceof String[]) {
                 int element = Integer.valueOf(qualifier).intValue();
                 String[] stringList = (String[])resource;
-                if (element >= 0 || element < stringList.length)
+                if (element >= 0 && element < stringList.length)
                     retrievedValue = stringList[element];
             }
             else if (resource instanceof String[][]) {
                 String[][] stringArray = (String[][])resource;
                 int slash = qualifier.indexOf("/");

@@ -324,11 +327,11 @@
                     }
                 }
                 else {
                     int row = Integer.valueOf(qualifier.substring(0, slash)).intValue();
                     int column = Integer.valueOf(qualifier.substring(slash + 1)).intValue();
-                    if (row >= 0 || row < stringArray.length || column >= 0 || column <
+                    if (row >= 0 && row < stringArray.length && column >= 0 && column <
                                     stringArray[row].length)
                         retrievedValue = stringArray[row][column];
                 }
             }
         }

@@ -350,10 +353,58 @@
             if (writeNewFile)
                 out.println(key + "=" + expectedValue);
         }
         return true;
     }
+
+    private static class JRELocaleResourceBundleControl extends ResourceBundle.Control {
+        static final JRELocaleResourceBundleControl INSTANCE = new JRELocaleResourceBundleControl();
+
+        private JRELocaleResourceBundleControl() {
+        }
+
+        @Override
+        public Locale getFallbackLocale(String baseName, Locale locale) {
+            if (baseName == null || locale == null) {
+                throw new NullPointerException();
+            }
+            return null;
+        }
+
+        private static final String CLDR      = ".cldr";
+
+        /**
+         * Changes baseName to its per-language package name and
+         * calls the super class implementation. For example,
+         * if the baseName is "sun.text.resources.FormatData" and locale is ja_JP,
+         * the baseName is changed to "sun.text.resources.ja.FormatData". If
+         * baseName contains "cldr", such as "sun.text.resources.cldr.FormatData",
+         * the name is changed to "sun.text.resources.cldr.jp.FormatData".
+         */
+        @Override
+        public String toBundleName(String baseName, Locale locale) {
+            String newBaseName = baseName;
+            String lang = locale.getLanguage();
+            if (lang.length() > 0) {
+                if (baseName.startsWith(UTIL_RESOURCES_PACKAGE)
+                    || baseName.startsWith(TEXT_RESOURCES_PACKAGE)) {
+                    // Assume the lengths are the same.
+                    if (UTIL_RESOURCES_PACKAGE.length()
+                        != TEXT_RESOURCES_PACKAGE.length()) {
+                        throw new InternalError("The resources package names have different lengths.");
+                    }
+                    int index = TEXT_RESOURCES_PACKAGE.length();
+                    if (baseName.indexOf(CLDR, index) > 0) {
+                        index += CLDR.length();
+                    }
+                    newBaseName = baseName.substring(0, index + 1) + lang
+                                      + baseName.substring(index);
+                }
+            }
+            return super.toBundleName(newBaseName, locale);
+        }
+    }
 }
 
 class EscapeReader extends FilterReader {
     public EscapeReader(Reader in) {
         super(in);