1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.locale.provider;
  27 
  28 import java.util.Locale;
  29 import java.util.MissingResourceException;
  30 import java.util.Set;
  31 import java.util.spi.LocaleNameProvider;
  32 import sun.util.resources.OpenListResourceBundle;
  33 
  34 /**
  35  * Concrete implementation of the
  36  * {@link java.util.spi.LocaleNameProvider LocaleNameProvider} class
  37  * for the JRE LocaleProviderAdapter.
  38  *
  39  * @author Naoto Sato
  40  * @author Masayoshi Okutsu
  41  */
  42 public class LocaleNameProviderImpl extends LocaleNameProvider implements AvailableLanguageTags {
  43     private final LocaleProviderAdapter.Type type;
  44     private final Set<String> langtags;
  45 
  46     public LocaleNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  47         this.type = type;
  48         this.langtags = langtags;
  49     }
  50 
  51     /**
  52      * Returns an array of all locales for which this locale service provider
  53      * can provide localized objects or names.
  54      *
  55      * @return An array of all locales for which this locale service provider
  56      * can provide localized objects or names.
  57      */
  58     @Override
  59     public Locale[] getAvailableLocales() {
  60         return LocaleProviderAdapter.toLocaleArray(langtags);
  61     }
  62 
  63     @Override
  64     public boolean isSupportedLocale(Locale locale) {
  65         return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
  66     }
  67 
  68     /**
  69      * Returns a localized name for the given ISO 639 language code and the
  70      * given locale that is appropriate for display to the user.
  71      * For example, if <code>languageCode</code> is "fr" and <code>locale</code>
  72      * is en_US, getDisplayLanguage() will return "French"; if <code>languageCode</code>
  73      * is "en" and <code>locale</code> is fr_FR, getDisplayLanguage() will return "anglais".
  74      * If the name returned cannot be localized according to <code>locale</code>,
  75      * (say, the provider does not have a Japanese name for Croatian),
  76      * this method returns null.
  77      * @param languageCode the ISO 639 language code string in the form of two
  78      *     lower-case letters between 'a' (U+0061) and 'z' (U+007A)
  79      * @param locale the desired locale
  80      * @return the name of the given language code for the specified locale, or null if it's not
  81      *     available.
  82      * @exception NullPointerException if <code>languageCode</code> or <code>locale</code> is null
  83      * @exception IllegalArgumentException if <code>languageCode</code> is not in the form of
  84      *     two lower-case letters, or <code>locale</code> isn't
  85      *     one of the locales returned from
  86      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
  87      *     getAvailableLocales()}.
  88      * @see java.util.Locale#getDisplayLanguage(java.util.Locale)
  89      */
  90     @Override
  91     public String getDisplayLanguage(String lang, Locale locale) {
  92         return getDisplayString(lang, locale);
  93     }
  94 
  95     /**
  96      * Returns a localized name for the given <a href="http://www.rfc-editor.org/rfc/bcp/bcp47.txt">
  97      * IETF BCP47</a> script code and the given locale that is appropriate for
  98      * display to the user.
  99      * For example, if <code>scriptCode</code> is "Latn" and <code>locale</code>
 100      * is en_US, getDisplayScript() will return "Latin"; if <code>scriptCode</code>
 101      * is "Cyrl" and <code>locale</code> is fr_FR, getDisplayScript() will return "cyrillique".
 102      * If the name returned cannot be localized according to <code>locale</code>,
 103      * (say, the provider does not have a Japanese name for Cyrillic),
 104      * this method returns null. The default implementation returns null.
 105      * @param scriptCode the four letter script code string in the form of title-case
 106      *     letters (the first letter is upper-case character between 'A' (U+0041) and
 107      *     'Z' (U+005A) followed by three lower-case character between 'a' (U+0061)
 108      *     and 'z' (U+007A)).
 109      * @param locale the desired locale
 110      * @return the name of the given script code for the specified locale, or null if it's not
 111      *     available.
 112      * @exception NullPointerException if <code>scriptCode</code> or <code>locale</code> is null
 113      * @exception IllegalArgumentException if <code>scriptCode</code> is not in the form of
 114      *     four title case letters, or <code>locale</code> isn't
 115      *     one of the locales returned from
 116      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
 117      *     getAvailableLocales()}.
 118      * @see java.util.Locale#getDisplayScript(java.util.Locale)
 119      */
 120     @Override
 121     public String getDisplayScript(String scriptCode, Locale locale) {
 122         return getDisplayString(scriptCode, locale);
 123     }
 124 
 125     /**
 126      * Returns a localized name for the given ISO 3166 country code and the
 127      * given locale that is appropriate for display to the user.
 128      * For example, if <code>countryCode</code> is "FR" and <code>locale</code>
 129      * is en_US, getDisplayCountry() will return "France"; if <code>countryCode</code>
 130      * is "US" and <code>locale</code> is fr_FR, getDisplayCountry() will return "Etats-Unis".
 131      * If the name returned cannot be localized according to <code>locale</code>,
 132      * (say, the provider does not have a Japanese name for Croatia),
 133      * this method returns null.
 134      * @param countryCode the ISO 3166 country code string in the form of two
 135      *     upper-case letters between 'A' (U+0041) and 'Z' (U+005A)
 136      * @param locale the desired locale
 137      * @return the name of the given country code for the specified locale, or null if it's not
 138      *     available.
 139      * @exception NullPointerException if <code>countryCode</code> or <code>locale</code> is null
 140      * @exception IllegalArgumentException if <code>countryCode</code> is not in the form of
 141      *     two upper-case letters, or <code>locale</code> isn't
 142      *     one of the locales returned from
 143      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
 144      *     getAvailableLocales()}.
 145      * @see java.util.Locale#getDisplayCountry(java.util.Locale)
 146      */
 147     @Override
 148     public String getDisplayCountry(String ctry, Locale locale) {
 149         return getDisplayString(ctry, locale);
 150     }
 151 
 152     /**
 153      * Returns a localized name for the given variant code and the given locale that
 154      * is appropriate for display to the user.
 155      * If the name returned cannot be localized according to <code>locale</code>,
 156      * this method returns null.
 157      * @param variant the variant string
 158      * @param locale the desired locale
 159      * @return the name of the given variant string for the specified locale, or null if it's not
 160      *     available.
 161      * @exception NullPointerException if <code>variant</code> or <code>locale</code> is null
 162      * @exception IllegalArgumentException if <code>locale</code> isn't
 163      *     one of the locales returned from
 164      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
 165      *     getAvailableLocales()}.
 166      * @see java.util.Locale#getDisplayVariant(java.util.Locale)
 167      */
 168     @Override
 169     public String getDisplayVariant(String vrnt, Locale locale) {
 170         return getDisplayString("%%"+vrnt, locale);
 171     }
 172 
 173     private String getDisplayString(String key, Locale locale) {
 174         if (key == null || locale == null) {
 175             throw new NullPointerException();
 176         }
 177 
 178         OpenListResourceBundle rb = LocaleProviderAdapter.forType(type).getLocaleData().getLocaleNames(locale);
 179         LocaleServiceProviderPool pool =
 180                 LocaleServiceProviderPool.getPool(LocaleNameProvider.class);
 181         try {
 182             if (!pool.hasProviders() ||
 183                 (rb.getLocale().equals(locale) && rb.handleGetKeys().contains(key))) {
 184                 return rb.getString(key);
 185             }
 186         } catch (MissingResourceException mre) {}
 187 
 188         return null;
 189     }
 190 
 191     @Override
 192     public Set<String> getAvailableLanguageTags() {
 193         return langtags;
 194     }
 195 }