1 /*
2  * Copyright (c) 2012, 2017, 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.Set;
30 import java.util.spi.LocaleNameProvider;
31 
32 /**
33  * Concrete implementation of the
34  * {@link java.util.spi.LocaleNameProvider LocaleNameProvider} class
35  * for the JRE LocaleProviderAdapter.
36  *
37  * @author Naoto Sato
38  * @author Masayoshi Okutsu
39  */
40 public class LocaleNameProviderImpl extends LocaleNameProvider implements AvailableLanguageTags {
41     private final LocaleProviderAdapter.Type type;
42     private final Set<String> langtags;
43 
LocaleNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags)44     public LocaleNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
45         this.type = type;
46         this.langtags = langtags;
47     }
48 
49     /**
50      * Returns an array of all locales for which this locale service provider
51      * can provide localized objects or names.
52      *
53      * @return An array of all locales for which this locale service provider
54      * can provide localized objects or names.
55      */
56     @Override
getAvailableLocales()57     public Locale[] getAvailableLocales() {
58         return LocaleProviderAdapter.toLocaleArray(langtags);
59     }
60 
61     @Override
isSupportedLocale(Locale locale)62     public boolean isSupportedLocale(Locale locale) {
63         return LocaleProviderAdapter.forType(type).isSupportedProviderLocale(locale, langtags);
64     }
65 
66     /**
67      * Returns a localized name for the given ISO 639 language code and the
68      * given locale that is appropriate for display to the user.
69      * For example, if <code>languageCode</code> is "fr" and <code>locale</code>
70      * is en_US, getDisplayLanguage() will return "French"; if <code>languageCode</code>
71      * is "en" and <code>locale</code> is fr_FR, getDisplayLanguage() will return "anglais".
72      * If the name returned cannot be localized according to <code>locale</code>,
73      * (say, the provider does not have a Japanese name for Croatian),
74      * this method returns null.
75      * @param lang the ISO 639 language code string in the form of two
76      *     lower-case letters between 'a' (U+0061) and 'z' (U+007A)
77      * @param locale the desired locale
78      * @return the name of the given language code for the specified locale, or null if it's not
79      *     available.
80      * @exception NullPointerException if <code>languageCode</code> or <code>locale</code> is null
81      * @exception IllegalArgumentException if <code>languageCode</code> is not in the form of
82      *     two lower-case letters, or <code>locale</code> isn't
83      *     one of the locales returned from
84      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
85      *     getAvailableLocales()}.
86      * @see java.util.Locale#getDisplayLanguage(java.util.Locale)
87      */
88     @Override
getDisplayLanguage(String lang, Locale locale)89     public String getDisplayLanguage(String lang, Locale locale) {
90         return getDisplayString(lang, locale);
91     }
92 
93     /**
94      * Returns a localized name for the given <a href="http://www.rfc-editor.org/rfc/bcp/bcp47.txt">
95      * IETF BCP47</a> script code and the given locale that is appropriate for
96      * display to the user.
97      * For example, if <code>scriptCode</code> is "Latn" and <code>locale</code>
98      * is en_US, getDisplayScript() will return "Latin"; if <code>scriptCode</code>
99      * is "Cyrl" and <code>locale</code> is fr_FR, getDisplayScript() will return "cyrillique".
100      * If the name returned cannot be localized according to <code>locale</code>,
101      * (say, the provider does not have a Japanese name for Cyrillic),
102      * this method returns null. The default implementation returns null.
103      * @param scriptCode the four letter script code string in the form of title-case
104      *     letters (the first letter is upper-case character between 'A' (U+0041) and
105      *     'Z' (U+005A) followed by three lower-case character between 'a' (U+0061)
106      *     and 'z' (U+007A)).
107      * @param locale the desired locale
108      * @return the name of the given script code for the specified locale, or null if it's not
109      *     available.
110      * @exception NullPointerException if <code>scriptCode</code> or <code>locale</code> is null
111      * @exception IllegalArgumentException if <code>scriptCode</code> is not in the form of
112      *     four title case letters, or <code>locale</code> isn't
113      *     one of the locales returned from
114      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
115      *     getAvailableLocales()}.
116      * @see java.util.Locale#getDisplayScript(java.util.Locale)
117      */
118     @Override
getDisplayScript(String scriptCode, Locale locale)119     public String getDisplayScript(String scriptCode, Locale locale) {
120         return getDisplayString(scriptCode, locale);
121     }
122 
123     /**
124      * Returns a localized name for the given ISO 3166 country code and the
125      * given locale that is appropriate for display to the user.
126      * For example, if <code>countryCode</code> is "FR" and <code>locale</code>
127      * is en_US, getDisplayCountry() will return "France"; if <code>countryCode</code>
128      * is "US" and <code>locale</code> is fr_FR, getDisplayCountry() will return "Etats-Unis".
129      * If the name returned cannot be localized according to <code>locale</code>,
130      * (say, the provider does not have a Japanese name for Croatia),
131      * this method returns null.
132      * @param ctry the ISO 3166 country code string in the form of two
133      *     upper-case letters between 'A' (U+0041) and 'Z' (U+005A)
134      * @param locale the desired locale
135      * @return the name of the given country code for the specified locale, or null if it's not
136      *     available.
137      * @exception NullPointerException if <code>countryCode</code> or <code>locale</code> is null
138      * @exception IllegalArgumentException if <code>countryCode</code> is not in the form of
139      *     two upper-case letters, or <code>locale</code> isn't
140      *     one of the locales returned from
141      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
142      *     getAvailableLocales()}.
143      * @see java.util.Locale#getDisplayCountry(java.util.Locale)
144      */
145     @Override
getDisplayCountry(String ctry, Locale locale)146     public String getDisplayCountry(String ctry, Locale locale) {
147         return getDisplayString(ctry, locale);
148     }
149 
150     /**
151      * Returns a localized name for the given variant code and the given locale that
152      * is appropriate for display to the user.
153      * If the name returned cannot be localized according to <code>locale</code>,
154      * this method returns null.
155      * @param vrnt the variant string
156      * @param locale the desired locale
157      * @return the name of the given variant string for the specified locale, or null if it's not
158      *     available.
159      * @exception NullPointerException if <code>variant</code> or <code>locale</code> is null
160      * @exception IllegalArgumentException if <code>locale</code> isn't
161      *     one of the locales returned from
162      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
163      *     getAvailableLocales()}.
164      * @see java.util.Locale#getDisplayVariant(java.util.Locale)
165      */
166     @Override
getDisplayVariant(String vrnt, Locale locale)167     public String getDisplayVariant(String vrnt, Locale locale) {
168         return getDisplayString("%%"+vrnt, locale);
169     }
170 
171     /**
172      * @inheritDoc
173      */
174     @Override
getDisplayUnicodeExtensionKey(String key, Locale locale)175     public String getDisplayUnicodeExtensionKey(String key, Locale locale) {
176         super.getDisplayUnicodeExtensionKey(key, locale); // null check
177         String rbKey = "key." + key;
178         String name = getDisplayString(rbKey, locale);
179         return rbKey.equals(name) ? key : name;
180     }
181 
182     /**
183      * @inheritDoc
184      */
185     @Override
getDisplayUnicodeExtensionType(String extType, String key, Locale locale)186     public String getDisplayUnicodeExtensionType(String extType, String key, Locale locale) {
187         super.getDisplayUnicodeExtensionType(extType, key, locale); // null check
188         String rbKey = "type." + key + "." + extType;
189         String name = getDisplayString(rbKey, locale);
190         return rbKey.equals(name) ? extType : name;
191     }
192 
getDisplayString(String key, Locale locale)193     private String getDisplayString(String key, Locale locale) {
194         if (key == null || locale == null) {
195             throw new NullPointerException();
196         }
197 
198         return LocaleProviderAdapter.forType(type).getLocaleResources(locale).getLocaleName(key);
199     }
200 
201     @Override
getAvailableLanguageTags()202     public Set<String> getAvailableLanguageTags() {
203         return langtags;
204     }
205 }
206