1 /*
2 * Copyright (c) 1996, 2013, 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
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.Arrays;
46 import java.util.Iterator;
47 import java.util.List;
48 import java.util.Locale;
49 import java.util.MissingResourceException;
50 import java.util.ResourceBundle;
51 import sun.util.locale.provider.LocaleDataMetaInfo;
52 import sun.util.locale.provider.LocaleProviderAdapter;
53 import static sun.util.locale.provider.LocaleProviderAdapter.Type.JRE;
54
55 /**
56 * Provides information about and access to resource bundles in the
57 * sun.text.resources and sun.util.resources packages or in their corresponding
58 * packages for CLDR.
59 *
60 * @author Asmus Freytag
61 * @author Mark Davis
62 */
63
64 public class LocaleData {
65 private final LocaleProviderAdapter.Type type;
66
67 public LocaleData(LocaleProviderAdapter.Type type) {
68 this.type = type;
69 }
70
71 /**
72 * Gets a calendar data resource bundle, using privileges
187 /* Singlton instance of ResourceBundle.Control. */
188 private static final LocaleDataResourceBundleControl INSTANCE =
189 new LocaleDataResourceBundleControl();
190
191 private LocaleDataResourceBundleControl() {
192 }
193
194 /*
195 * This method overrides the default implementation to search
196 * from a prebaked locale string list to determin the candidate
197 * locale list.
198 *
199 * @param baseName the resource bundle base name.
200 * locale the requested locale for the resource bundle.
201 * @returns a list of candidate locales to search from.
202 * @exception NullPointerException if baseName or locale is null.
203 */
204 @Override
205 public List<Locale> getCandidateLocales(String baseName, Locale locale) {
206 List<Locale> candidates = super.getCandidateLocales(baseName, locale);
207 /* Get the locale string list from LocaleDataMetaInfo class. */
208 String localeString = LocaleDataMetaInfo.getSupportedLocaleString(baseName);
209
210 if (localeString != null && localeString.length() != 0) {
211 for (Iterator<Locale> l = candidates.iterator(); l.hasNext();) {
212 Locale loc = l.next();
213 String lstr;
214 if (loc.getScript().length() > 0) {
215 lstr = loc.toLanguageTag().replace('-', '_');
216 } else {
217 lstr = loc.toString();
218 int idx = lstr.indexOf("_#");
219 if (idx >= 0) {
220 lstr = lstr.substring(0, idx);
221 }
222 }
223 /* Every locale string in the locale string list returned from
224 the above getSupportedLocaleString is enclosed
225 within two white spaces so that we could check some locale
226 such as "en".
227 */
228 if (lstr.length() != 0 && localeString.indexOf(" " + lstr + " ") == -1) {
229 l.remove();
230 }
231 }
232 }
233 // Force fallback to Locale.ENGLISH for CLDR time zone names support
234 if (locale.getLanguage() != "en"
235 && baseName.contains(CLDR) && baseName.endsWith("TimeZoneNames")) {
236 candidates.add(candidates.size() - 1, Locale.ENGLISH);
237 }
238 return candidates;
239 }
240
241 /*
242 * Overrides "getFallbackLocale" to return null so
243 * that the fallback locale will be null.
244 * @param baseName the resource bundle base name.
245 * locale the requested locale for the resource bundle.
246 * @return null for the fallback locale.
247 * @exception NullPointerException if baseName or locale is null.
248 */
249 @Override
250 public Locale getFallbackLocale(String baseName, Locale locale) {
251 if (baseName == null || locale == null) {
252 throw new NullPointerException();
253 }
254 return null;
255 }
256
257 private static final String CLDR = ".cldr";
258
259 /**
260 * Changes baseName to its per-language package name and
261 * calls the super class implementation. For example,
262 * if the baseName is "sun.text.resources.FormatData" and locale is ja_JP,
263 * the baseName is changed to "sun.text.resources.ja.FormatData". If
264 * baseName contains "cldr", such as "sun.text.resources.cldr.FormatData",
265 * the name is changed to "sun.text.resources.cldr.jp.FormatData".
266 */
267 @Override
268 public String toBundleName(String baseName, Locale locale) {
269 String newBaseName = baseName;
270 String lang = locale.getLanguage();
271 if (lang.length() > 0) {
272 if (baseName.startsWith(JRE.getUtilResourcesPackage())
273 || baseName.startsWith(JRE.getTextResourcesPackage())) {
274 // Assume the lengths are the same.
275 assert JRE.getUtilResourcesPackage().length()
276 == JRE.getTextResourcesPackage().length();
277 int index = JRE.getUtilResourcesPackage().length();
278 if (baseName.indexOf(CLDR, index) > 0) {
279 index += CLDR.length();
280 }
281 newBaseName = baseName.substring(0, index + 1) + lang
282 + baseName.substring(index);
283 }
284 }
285 return super.toBundleName(newBaseName, locale);
286 }
287 }
288
289 private static class SupplementaryResourceBundleControl extends LocaleDataResourceBundleControl {
290 private static final SupplementaryResourceBundleControl INSTANCE =
291 new SupplementaryResourceBundleControl();
292
293 private SupplementaryResourceBundleControl() {
294 }
295
296 @Override
297 public List<Locale> getCandidateLocales(String baseName, Locale locale) {
298 // Specifiy only the given locale
299 return Arrays.asList(locale);
|
1 /*
2 * Copyright (c) 1996, 2015, 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
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.Arrays;
46 import java.util.Iterator;
47 import java.util.List;
48 import java.util.Locale;
49 import java.util.MissingResourceException;
50 import java.util.ResourceBundle;
51 import java.util.Set;
52 import sun.util.locale.provider.JRELocaleProviderAdapter;
53 import sun.util.locale.provider.LocaleDataMetaInfo;
54 import sun.util.locale.provider.LocaleProviderAdapter;
55 import static sun.util.locale.provider.LocaleProviderAdapter.Type.CLDR;
56 import static sun.util.locale.provider.LocaleProviderAdapter.Type.JRE;
57
58 /**
59 * Provides information about and access to resource bundles in the
60 * sun.text.resources and sun.util.resources packages or in their corresponding
61 * packages for CLDR.
62 *
63 * @author Asmus Freytag
64 * @author Mark Davis
65 */
66
67 public class LocaleData {
68 private final LocaleProviderAdapter.Type type;
69
70 public LocaleData(LocaleProviderAdapter.Type type) {
71 this.type = type;
72 }
73
74 /**
75 * Gets a calendar data resource bundle, using privileges
190 /* Singlton instance of ResourceBundle.Control. */
191 private static final LocaleDataResourceBundleControl INSTANCE =
192 new LocaleDataResourceBundleControl();
193
194 private LocaleDataResourceBundleControl() {
195 }
196
197 /*
198 * This method overrides the default implementation to search
199 * from a prebaked locale string list to determin the candidate
200 * locale list.
201 *
202 * @param baseName the resource bundle base name.
203 * locale the requested locale for the resource bundle.
204 * @returns a list of candidate locales to search from.
205 * @exception NullPointerException if baseName or locale is null.
206 */
207 @Override
208 public List<Locale> getCandidateLocales(String baseName, Locale locale) {
209 List<Locale> candidates = super.getCandidateLocales(baseName, locale);
210 // Weed out Locales which are known to have no resource bundles
211 int lastDot = baseName.lastIndexOf('.');
212 String category = (lastDot >= 0) ? baseName.substring(lastDot + 1) : baseName;
213 LocaleProviderAdapter.Type type = baseName.contains(DOTCLDR) ? CLDR : JRE;
214 LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
215 Set<String> langtags = ((JRELocaleProviderAdapter)adapter).getLanguageTagSet(category);
216 if (!langtags.isEmpty()) {
217 for (Iterator<Locale> itr = candidates.iterator(); itr.hasNext();) {
218 if (!LocaleProviderAdapter.isSupportedLocale(itr.next(), type, langtags)) {
219 itr.remove();
220 }
221 }
222 }
223
224 // Force fallback to Locale.ENGLISH for CLDR time zone names support
225 if (locale.getLanguage() != "en"
226 && type == CLDR && category.equals("TimeZoneNames")) {
227 candidates.add(candidates.size() - 1, Locale.ENGLISH);
228 }
229 return candidates;
230 }
231
232 /*
233 * Overrides "getFallbackLocale" to return null so
234 * that the fallback locale will be null.
235 * @param baseName the resource bundle base name.
236 * locale the requested locale for the resource bundle.
237 * @return null for the fallback locale.
238 * @exception NullPointerException if baseName or locale is null.
239 */
240 @Override
241 public Locale getFallbackLocale(String baseName, Locale locale) {
242 if (baseName == null || locale == null) {
243 throw new NullPointerException();
244 }
245 return null;
246 }
247
248 private static final String DOTCLDR = ".cldr";
249
250 /**
251 * Changes baseName to its per-language package name and
252 * calls the super class implementation. For example,
253 * if the baseName is "sun.text.resources.FormatData" and locale is ja_JP,
254 * the baseName is changed to "sun.text.resources.ja.FormatData". If
255 * baseName contains "cldr", such as "sun.text.resources.cldr.FormatData",
256 * the name is changed to "sun.text.resources.cldr.jp.FormatData".
257 */
258 @Override
259 public String toBundleName(String baseName, Locale locale) {
260 String newBaseName = baseName;
261 String lang = locale.getLanguage();
262 if (lang.length() > 0) {
263 if (baseName.startsWith(JRE.getUtilResourcesPackage())
264 || baseName.startsWith(JRE.getTextResourcesPackage())) {
265 // Assume the lengths are the same.
266 assert JRE.getUtilResourcesPackage().length()
267 == JRE.getTextResourcesPackage().length();
268 int index = JRE.getUtilResourcesPackage().length();
269 if (baseName.indexOf(DOTCLDR, index) > 0) {
270 index += DOTCLDR.length();
271 }
272 newBaseName = baseName.substring(0, index + 1) + lang
273 + baseName.substring(index);
274 }
275 }
276 return super.toBundleName(newBaseName, locale);
277 }
278 }
279
280 private static class SupplementaryResourceBundleControl extends LocaleDataResourceBundleControl {
281 private static final SupplementaryResourceBundleControl INSTANCE =
282 new SupplementaryResourceBundleControl();
283
284 private SupplementaryResourceBundleControl() {
285 }
286
287 @Override
288 public List<Locale> getCandidateLocales(String baseName, Locale locale) {
289 // Specifiy only the given locale
290 return Arrays.asList(locale);
|