Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/util/resources/LocaleData.java
38918 views
/*1* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved27* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved28*29* The original version of this source code and documentation30* is copyrighted and owned by Taligent, Inc., a wholly-owned31* subsidiary of IBM. These materials are provided under terms32* of a License Agreement between Taligent and Sun. This technology33* is protected by multiple US and International patents.34*35* This notice and attribution to Taligent may not be removed.36* Taligent is a registered trademark of Taligent, Inc.37*38*/3940package sun.util.resources;4142import java.security.AccessController;43import java.security.PrivilegedAction;44import java.util.Arrays;45import java.util.Iterator;46import java.util.List;47import java.util.Locale;48import java.util.MissingResourceException;49import java.util.ResourceBundle;50import java.util.Set;51import sun.util.locale.provider.JRELocaleProviderAdapter;52import sun.util.locale.provider.LocaleDataMetaInfo;53import sun.util.locale.provider.LocaleProviderAdapter;54import static sun.util.locale.provider.LocaleProviderAdapter.Type.CLDR;55import static sun.util.locale.provider.LocaleProviderAdapter.Type.JRE;5657/**58* Provides information about and access to resource bundles in the59* sun.text.resources and sun.util.resources packages or in their corresponding60* packages for CLDR.61*62* @author Asmus Freytag63* @author Mark Davis64*/6566public class LocaleData {67private final LocaleProviderAdapter.Type type;6869public LocaleData(LocaleProviderAdapter.Type type) {70this.type = type;71}7273/**74* Gets a calendar data resource bundle, using privileges75* to allow accessing a sun.* package.76*/77public ResourceBundle getCalendarData(Locale locale) {78return getBundle(type.getUtilResourcesPackage() + ".CalendarData", locale);79}8081/**82* Gets a currency names resource bundle, using privileges83* to allow accessing a sun.* package.84*/85public OpenListResourceBundle getCurrencyNames(Locale locale) {86return (OpenListResourceBundle) getBundle(type.getUtilResourcesPackage() + ".CurrencyNames", locale);87}8889/**90* Gets a locale names resource bundle, using privileges91* to allow accessing a sun.* package.92*/93public OpenListResourceBundle getLocaleNames(Locale locale) {94return (OpenListResourceBundle) getBundle(type.getUtilResourcesPackage() + ".LocaleNames", locale);95}9697/**98* Gets a time zone names resource bundle, using privileges99* to allow accessing a sun.* package.100*/101public TimeZoneNamesBundle getTimeZoneNames(Locale locale) {102return (TimeZoneNamesBundle) getBundle(type.getUtilResourcesPackage() + ".TimeZoneNames", locale);103}104105/**106* Gets a break iterator info resource bundle, using privileges107* to allow accessing a sun.* package.108*/109public ResourceBundle getBreakIteratorInfo(Locale locale) {110return getBundle(type.getTextResourcesPackage() + ".BreakIteratorInfo", locale);111}112113/**114* Gets a collation data resource bundle, using privileges115* to allow accessing a sun.* package.116*/117public ResourceBundle getCollationData(Locale locale) {118return getBundle(type.getTextResourcesPackage() + ".CollationData", locale);119}120121/**122* Gets a date format data resource bundle, using privileges123* to allow accessing a sun.* package.124*/125public ResourceBundle getDateFormatData(Locale locale) {126return getBundle(type.getTextResourcesPackage() + ".FormatData", locale);127}128129public void setSupplementary(ParallelListResourceBundle formatData) {130if (!formatData.areParallelContentsComplete()) {131String suppName = type.getTextResourcesPackage() + ".JavaTimeSupplementary";132setSupplementary(suppName, formatData);133}134}135136private boolean setSupplementary(String suppName, ParallelListResourceBundle formatData) {137ParallelListResourceBundle parent = (ParallelListResourceBundle) formatData.getParent();138boolean resetKeySet = false;139if (parent != null) {140resetKeySet = setSupplementary(suppName, parent);141}142OpenListResourceBundle supp = getSupplementary(suppName, formatData.getLocale());143formatData.setParallelContents(supp);144resetKeySet |= supp != null;145// If any parents or this bundle has parallel data, reset keyset to create146// a new keyset with the data.147if (resetKeySet) {148formatData.resetKeySet();149}150return resetKeySet;151}152153/**154* Gets a number format data resource bundle, using privileges155* to allow accessing a sun.* package.156*/157public ResourceBundle getNumberFormatData(Locale locale) {158return getBundle(type.getTextResourcesPackage() + ".FormatData", locale);159}160161public static ResourceBundle getBundle(final String baseName, final Locale locale) {162return AccessController.doPrivileged(new PrivilegedAction<ResourceBundle>() {163@Override164public ResourceBundle run() {165return ResourceBundle166.getBundle(baseName, locale, LocaleDataResourceBundleControl.INSTANCE);167}168});169}170171private static OpenListResourceBundle getSupplementary(final String baseName, final Locale locale) {172return AccessController.doPrivileged(new PrivilegedAction<OpenListResourceBundle>() {173@Override174public OpenListResourceBundle run() {175OpenListResourceBundle rb = null;176try {177rb = (OpenListResourceBundle) ResourceBundle.getBundle(baseName,178locale, SupplementaryResourceBundleControl.INSTANCE);179180} catch (MissingResourceException e) {181// return null if no supplementary is available182}183return rb;184}185});186}187188private static class LocaleDataResourceBundleControl extends ResourceBundle.Control {189/* Singlton instance of ResourceBundle.Control. */190private static final LocaleDataResourceBundleControl INSTANCE =191new LocaleDataResourceBundleControl();192193private LocaleDataResourceBundleControl() {194}195196/*197* This method overrides the default implementation to search198* from a prebaked locale string list to determin the candidate199* locale list.200*201* @param baseName the resource bundle base name.202* locale the requested locale for the resource bundle.203* @returns a list of candidate locales to search from.204* @exception NullPointerException if baseName or locale is null.205*/206@Override207public List<Locale> getCandidateLocales(String baseName, Locale locale) {208List<Locale> candidates = super.getCandidateLocales(baseName, locale);209// Weed out Locales which are known to have no resource bundles210int lastDot = baseName.lastIndexOf('.');211String category = (lastDot >= 0) ? baseName.substring(lastDot + 1) : baseName;212LocaleProviderAdapter.Type type = baseName.contains(DOTCLDR) ? CLDR : JRE;213LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);214Set<String> langtags = ((JRELocaleProviderAdapter)adapter).getLanguageTagSet(category);215if (!langtags.isEmpty()) {216for (Iterator<Locale> itr = candidates.iterator(); itr.hasNext();) {217if (!LocaleProviderAdapter.isSupportedLocale(itr.next(), type, langtags)) {218itr.remove();219}220}221}222223// Force fallback to Locale.ENGLISH for CLDR time zone names support224if (locale.getLanguage() != "en"225&& type == CLDR && category.equals("TimeZoneNames")) {226candidates.add(candidates.size() - 1, Locale.ENGLISH);227}228return candidates;229}230231/*232* Overrides "getFallbackLocale" to return null so233* that the fallback locale will be null.234* @param baseName the resource bundle base name.235* locale the requested locale for the resource bundle.236* @return null for the fallback locale.237* @exception NullPointerException if baseName or locale is null.238*/239@Override240public Locale getFallbackLocale(String baseName, Locale locale) {241if (baseName == null || locale == null) {242throw new NullPointerException();243}244return null;245}246247private static final String DOTCLDR = ".cldr";248249/**250* Changes baseName to its per-language package name and251* calls the super class implementation. For example,252* if the baseName is "sun.text.resources.FormatData" and locale is ja_JP,253* the baseName is changed to "sun.text.resources.ja.FormatData". If254* baseName contains "cldr", such as "sun.text.resources.cldr.FormatData",255* the name is changed to "sun.text.resources.cldr.jp.FormatData".256*/257@Override258public String toBundleName(String baseName, Locale locale) {259String newBaseName = baseName;260String lang = locale.getLanguage();261if (lang.length() > 0) {262if (baseName.startsWith(JRE.getUtilResourcesPackage())263|| baseName.startsWith(JRE.getTextResourcesPackage())) {264// Assume the lengths are the same.265assert JRE.getUtilResourcesPackage().length()266== JRE.getTextResourcesPackage().length();267int index = JRE.getUtilResourcesPackage().length();268if (baseName.indexOf(DOTCLDR, index) > 0) {269index += DOTCLDR.length();270}271newBaseName = baseName.substring(0, index + 1) + lang272+ baseName.substring(index);273}274}275return super.toBundleName(newBaseName, locale);276}277}278279private static class SupplementaryResourceBundleControl extends LocaleDataResourceBundleControl {280private static final SupplementaryResourceBundleControl INSTANCE =281new SupplementaryResourceBundleControl();282283private SupplementaryResourceBundleControl() {284}285286@Override287public List<Locale> getCandidateLocales(String baseName, Locale locale) {288// Specifiy only the given locale289return Arrays.asList(locale);290}291292@Override293public long getTimeToLive(String baseName, Locale locale) {294assert baseName.contains("JavaTimeSupplementary");295return TTL_DONT_CACHE;296}297}298}299300301