src/share/classes/sun/util/resources/LocaleData.java

Print this page




  29  *
  30  * The original version of this source code and documentation
  31  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  32  * subsidiary of IBM. These materials are provided under terms
  33  * of a License Agreement between Taligent and Sun. This technology
  34  * is protected by multiple US and International patents.
  35  *
  36  * This notice and attribution to Taligent may not be removed.
  37  * Taligent is a registered trademark of Taligent, Inc.
  38  *
  39  */
  40 
  41 package sun.util.resources;
  42 
  43 import java.security.AccessController;
  44 import java.security.PrivilegedAction;
  45 import java.util.Iterator;
  46 import java.util.List;
  47 import java.util.Locale;
  48 import java.util.ResourceBundle;

  49 import sun.util.locale.provider.LocaleProviderAdapter;
  50 import static sun.util.locale.provider.LocaleProviderAdapter.Type.JRE;
  51 import sun.util.locale.provider.LocaleDataMetaInfo;
  52 
  53 /**
  54  * Provides information about and access to resource bundles in the
  55  * sun.text.resources and sun.util.resources packages or in their corresponding
  56  * packages for CLDR.
  57  *
  58  * @author Asmus Freytag
  59  * @author Mark Davis
  60  */
  61 
  62 public class LocaleData {
  63     private final LocaleProviderAdapter.Type type;
  64 
  65     public LocaleData(LocaleProviderAdapter.Type type) {
  66         this.type = type;
  67     }
  68 
  69     /**
  70      * Gets a calendar data resource bundle, using privileges
  71      * to allow accessing a sun.* package.


  77     /**
  78      * Gets a currency names resource bundle, using privileges
  79      * to allow accessing a sun.* package.
  80      */
  81     public OpenListResourceBundle getCurrencyNames(Locale locale) {
  82         return (OpenListResourceBundle) getBundle(type.getUtilResourcesPackage() + ".CurrencyNames", locale);
  83     }
  84 
  85     /**
  86      * Gets a locale names resource bundle, using privileges
  87      * to allow accessing a sun.* package.
  88      */
  89     public OpenListResourceBundle getLocaleNames(Locale locale) {
  90         return (OpenListResourceBundle) getBundle(type.getUtilResourcesPackage() + ".LocaleNames", locale);
  91     }
  92 
  93     /**
  94      * Gets a time zone names resource bundle, using privileges
  95      * to allow accessing a sun.* package.
  96      */
  97     public OpenListResourceBundle getTimeZoneNames(Locale locale) {
  98         return (OpenListResourceBundle) getBundle(type.getUtilResourcesPackage() + ".TimeZoneNames", locale);
  99     }
 100 
 101     /**
 102      * Gets a collation data resource bundle, using privileges
 103      * to allow accessing a sun.* package.
 104      */
 105     public ResourceBundle getCollationData(Locale locale) {
 106         return getBundle(type.getTextResourcesPackage() + ".CollationData", locale);
 107     }
 108 
 109     /**
 110      * Gets a date format data resource bundle, using privileges
 111      * to allow accessing a sun.* package.
 112      */
 113     public ResourceBundle getDateFormatData(Locale locale) {
 114         return getBundle(type.getTextResourcesPackage() + ".FormatData", locale);
 115     }
 116 
 117     /**
 118      * Gets a number format data resource bundle, using privileges


 141         public static LocaleDataResourceBundleControl getRBControlInstance() {
 142             return rbControlInstance;
 143         }
 144 
 145         /*
 146          * This method overrides the default implementation to search
 147          * from a prebaked locale string list to determin the candidate
 148          * locale list.
 149          *
 150          * @param baseName the resource bundle base name.
 151          *        locale   the requested locale for the resource bundle.
 152          * @returns a list of candidate locales to search from.
 153          * @exception NullPointerException if baseName or locale is null.
 154          */
 155         @Override
 156          public List<Locale> getCandidateLocales(String baseName, Locale locale) {
 157             List<Locale> candidates = super.getCandidateLocales(baseName, locale);
 158             /* Get the locale string list from LocaleDataMetaInfo class. */
 159             String localeString = LocaleDataMetaInfo.getSupportedLocaleString(baseName);
 160 
 161             if (localeString == null || localeString.length() == 0) {
 162                 return candidates;
 163             }
 164 
 165             for (Iterator<Locale> l = candidates.iterator(); l.hasNext(); ) {
 166                 Locale loc = l.next();
 167                 String lstr;
 168                 if (loc.getScript().length() > 0) {
 169                     lstr = loc.toLanguageTag().replace('-', '_');
 170                 } else {
 171                     lstr = loc.toString();
 172                     int idx = lstr.indexOf("_#");
 173                     if (idx >= 0) {
 174                         lstr = lstr.substring(0, idx);
 175                     }
 176                 }
 177                 /* Every locale string in the locale string list returned from
 178                    the above getSupportedLocaleString is enclosed
 179                    within two white spaces so that we could check some locale
 180                    such as "en".
 181                 */
 182                 if (lstr.length() != 0 && localeString.indexOf(" " + lstr + " ") == -1) {
 183                     l.remove();
 184                 }






 185             }
 186             return candidates;
 187         }
 188 
 189         /*
 190          * Overrides "getFallbackLocale" to return null so
 191          * that the fallback locale will be null.
 192          * @param baseName the resource bundle base name.
 193          *        locale   the requested locale for the resource bundle.
 194          * @return null for the fallback locale.
 195          * @exception NullPointerException if baseName or locale is null.
 196          */
 197         @Override
 198         public Locale getFallbackLocale(String baseName, Locale locale) {
 199             if (baseName == null || locale == null) {
 200                 throw new NullPointerException();
 201             }
 202             return null;
 203         }
 204 




  29  *
  30  * The original version of this source code and documentation
  31  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  32  * subsidiary of IBM. These materials are provided under terms
  33  * of a License Agreement between Taligent and Sun. This technology
  34  * is protected by multiple US and International patents.
  35  *
  36  * This notice and attribution to Taligent may not be removed.
  37  * Taligent is a registered trademark of Taligent, Inc.
  38  *
  39  */
  40 
  41 package sun.util.resources;
  42 
  43 import java.security.AccessController;
  44 import java.security.PrivilegedAction;
  45 import java.util.Iterator;
  46 import java.util.List;
  47 import java.util.Locale;
  48 import java.util.ResourceBundle;
  49 import sun.util.locale.provider.LocaleDataMetaInfo;
  50 import sun.util.locale.provider.LocaleProviderAdapter;
  51 import static sun.util.locale.provider.LocaleProviderAdapter.Type.JRE;

  52 
  53 /**
  54  * Provides information about and access to resource bundles in the
  55  * sun.text.resources and sun.util.resources packages or in their corresponding
  56  * packages for CLDR.
  57  *
  58  * @author Asmus Freytag
  59  * @author Mark Davis
  60  */
  61 
  62 public class LocaleData {
  63     private final LocaleProviderAdapter.Type type;
  64 
  65     public LocaleData(LocaleProviderAdapter.Type type) {
  66         this.type = type;
  67     }
  68 
  69     /**
  70      * Gets a calendar data resource bundle, using privileges
  71      * to allow accessing a sun.* package.


  77     /**
  78      * Gets a currency names resource bundle, using privileges
  79      * to allow accessing a sun.* package.
  80      */
  81     public OpenListResourceBundle getCurrencyNames(Locale locale) {
  82         return (OpenListResourceBundle) getBundle(type.getUtilResourcesPackage() + ".CurrencyNames", locale);
  83     }
  84 
  85     /**
  86      * Gets a locale names resource bundle, using privileges
  87      * to allow accessing a sun.* package.
  88      */
  89     public OpenListResourceBundle getLocaleNames(Locale locale) {
  90         return (OpenListResourceBundle) getBundle(type.getUtilResourcesPackage() + ".LocaleNames", locale);
  91     }
  92 
  93     /**
  94      * Gets a time zone names resource bundle, using privileges
  95      * to allow accessing a sun.* package.
  96      */
  97     public TimeZoneNamesBundle getTimeZoneNames(Locale locale) {
  98         return (TimeZoneNamesBundle) getBundle(type.getUtilResourcesPackage() + ".TimeZoneNames", locale);
  99     }
 100 
 101     /**
 102      * Gets a collation data resource bundle, using privileges
 103      * to allow accessing a sun.* package.
 104      */
 105     public ResourceBundle getCollationData(Locale locale) {
 106         return getBundle(type.getTextResourcesPackage() + ".CollationData", locale);
 107     }
 108 
 109     /**
 110      * Gets a date format data resource bundle, using privileges
 111      * to allow accessing a sun.* package.
 112      */
 113     public ResourceBundle getDateFormatData(Locale locale) {
 114         return getBundle(type.getTextResourcesPackage() + ".FormatData", locale);
 115     }
 116 
 117     /**
 118      * Gets a number format data resource bundle, using privileges


 141         public static LocaleDataResourceBundleControl getRBControlInstance() {
 142             return rbControlInstance;
 143         }
 144 
 145         /*
 146          * This method overrides the default implementation to search
 147          * from a prebaked locale string list to determin the candidate
 148          * locale list.
 149          *
 150          * @param baseName the resource bundle base name.
 151          *        locale   the requested locale for the resource bundle.
 152          * @returns a list of candidate locales to search from.
 153          * @exception NullPointerException if baseName or locale is null.
 154          */
 155         @Override
 156          public List<Locale> getCandidateLocales(String baseName, Locale locale) {
 157             List<Locale> candidates = super.getCandidateLocales(baseName, locale);
 158             /* Get the locale string list from LocaleDataMetaInfo class. */
 159             String localeString = LocaleDataMetaInfo.getSupportedLocaleString(baseName);
 160 
 161             if (localeString != null && localeString.length() != 0) {
 162                 for (Iterator<Locale> l = candidates.iterator(); l.hasNext();) {



 163                     Locale loc = l.next();
 164                     String lstr;
 165                     if (loc.getScript().length() > 0) {
 166                         lstr = loc.toLanguageTag().replace('-', '_');
 167                     } else {
 168                         lstr = loc.toString();
 169                         int idx = lstr.indexOf("_#");
 170                         if (idx >= 0) {
 171                             lstr = lstr.substring(0, idx);
 172                         }
 173                     }
 174                     /* Every locale string in the locale string list returned from
 175                      the above getSupportedLocaleString is enclosed
 176                      within two white spaces so that we could check some locale
 177                      such as "en".
 178                      */
 179                     if (lstr.length() != 0 && localeString.indexOf(" " + lstr + " ") == -1) {
 180                         l.remove();
 181                     }
 182                 }
 183             }
 184             // Force fallback to Locale.ENGLISH for CLDR time zone names support
 185             if (locale.getLanguage() != "en"
 186                     && baseName.contains(CLDR) && baseName.endsWith("TimeZoneNames")) {
 187                 candidates.add(candidates.size() - 1, Locale.ENGLISH);
 188             }
 189             return candidates;
 190         }
 191 
 192         /*
 193          * Overrides "getFallbackLocale" to return null so
 194          * that the fallback locale will be null.
 195          * @param baseName the resource bundle base name.
 196          *        locale   the requested locale for the resource bundle.
 197          * @return null for the fallback locale.
 198          * @exception NullPointerException if baseName or locale is null.
 199          */
 200         @Override
 201         public Locale getFallbackLocale(String baseName, Locale locale) {
 202             if (baseName == null || locale == null) {
 203                 throw new NullPointerException();
 204             }
 205             return null;
 206         }
 207