|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
/* |
|
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved |
|
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved |
|
* |
|
* The original version of this source code and documentation |
|
* is copyrighted and owned by Taligent, Inc., a wholly-owned |
|
* subsidiary of IBM. These materials are provided under terms |
|
* of a License Agreement between Taligent and Sun. This technology |
|
* is protected by multiple US and International patents. |
|
* |
|
* This notice and attribution to Taligent may not be removed. |
|
* Taligent is a registered trademark of Taligent, Inc. |
|
* |
|
*/ |
|
|
|
package sun.util.resources; |
|
|
|
import java.security.AccessController; |
|
import java.security.PrivilegedAction; |
|
import java.util.Arrays; |
|
import java.util.Iterator; |
|
import java.util.List; |
|
import java.util.Locale; |
|
import java.util.MissingResourceException; |
|
import java.util.ResourceBundle; |
|
import java.util.Set; |
|
import sun.util.locale.provider.JRELocaleProviderAdapter; |
|
import sun.util.locale.provider.LocaleDataMetaInfo; |
|
import sun.util.locale.provider.LocaleProviderAdapter; |
|
import static sun.util.locale.provider.LocaleProviderAdapter.Type.CLDR; |
|
import static sun.util.locale.provider.LocaleProviderAdapter.Type.JRE; |
|
|
|
/** |
|
* Provides information about and access to resource bundles in the |
|
* sun.text.resources and sun.util.resources packages or in their corresponding |
|
* packages for CLDR. |
|
* |
|
* @author Asmus Freytag |
|
* @author Mark Davis |
|
*/ |
|
|
|
public class LocaleData { |
|
private final LocaleProviderAdapter.Type type; |
|
|
|
public LocaleData(LocaleProviderAdapter.Type type) { |
|
this.type = type; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public ResourceBundle getCalendarData(Locale locale) { |
|
return getBundle(type.getUtilResourcesPackage() + ".CalendarData", locale); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public OpenListResourceBundle getCurrencyNames(Locale locale) { |
|
return (OpenListResourceBundle) getBundle(type.getUtilResourcesPackage() + ".CurrencyNames", locale); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public OpenListResourceBundle getLocaleNames(Locale locale) { |
|
return (OpenListResourceBundle) getBundle(type.getUtilResourcesPackage() + ".LocaleNames", locale); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public TimeZoneNamesBundle getTimeZoneNames(Locale locale) { |
|
return (TimeZoneNamesBundle) getBundle(type.getUtilResourcesPackage() + ".TimeZoneNames", locale); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public ResourceBundle getBreakIteratorInfo(Locale locale) { |
|
return getBundle(type.getTextResourcesPackage() + ".BreakIteratorInfo", locale); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public ResourceBundle getCollationData(Locale locale) { |
|
return getBundle(type.getTextResourcesPackage() + ".CollationData", locale); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public ResourceBundle getDateFormatData(Locale locale) { |
|
return getBundle(type.getTextResourcesPackage() + ".FormatData", locale); |
|
} |
|
|
|
public void setSupplementary(ParallelListResourceBundle formatData) { |
|
if (!formatData.areParallelContentsComplete()) { |
|
String suppName = type.getTextResourcesPackage() + ".JavaTimeSupplementary"; |
|
setSupplementary(suppName, formatData); |
|
} |
|
} |
|
|
|
private boolean setSupplementary(String suppName, ParallelListResourceBundle formatData) { |
|
ParallelListResourceBundle parent = (ParallelListResourceBundle) formatData.getParent(); |
|
boolean resetKeySet = false; |
|
if (parent != null) { |
|
resetKeySet = setSupplementary(suppName, parent); |
|
} |
|
OpenListResourceBundle supp = getSupplementary(suppName, formatData.getLocale()); |
|
formatData.setParallelContents(supp); |
|
resetKeySet |= supp != null; |
|
// If any parents or this bundle has parallel data, reset keyset to create |
|
|
|
if (resetKeySet) { |
|
formatData.resetKeySet(); |
|
} |
|
return resetKeySet; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public ResourceBundle getNumberFormatData(Locale locale) { |
|
return getBundle(type.getTextResourcesPackage() + ".FormatData", locale); |
|
} |
|
|
|
public static ResourceBundle getBundle(final String baseName, final Locale locale) { |
|
return AccessController.doPrivileged(new PrivilegedAction<ResourceBundle>() { |
|
@Override |
|
public ResourceBundle run() { |
|
return ResourceBundle |
|
.getBundle(baseName, locale, LocaleDataResourceBundleControl.INSTANCE); |
|
} |
|
}); |
|
} |
|
|
|
private static OpenListResourceBundle getSupplementary(final String baseName, final Locale locale) { |
|
return AccessController.doPrivileged(new PrivilegedAction<OpenListResourceBundle>() { |
|
@Override |
|
public OpenListResourceBundle run() { |
|
OpenListResourceBundle rb = null; |
|
try { |
|
rb = (OpenListResourceBundle) ResourceBundle.getBundle(baseName, |
|
locale, SupplementaryResourceBundleControl.INSTANCE); |
|
|
|
} catch (MissingResourceException e) { |
|
// return null if no supplementary is available |
|
} |
|
return rb; |
|
} |
|
}); |
|
} |
|
|
|
private static class LocaleDataResourceBundleControl extends ResourceBundle.Control { |
|
|
|
private static final LocaleDataResourceBundleControl INSTANCE = |
|
new LocaleDataResourceBundleControl(); |
|
|
|
private LocaleDataResourceBundleControl() { |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@Override |
|
public List<Locale> getCandidateLocales(String baseName, Locale locale) { |
|
List<Locale> candidates = super.getCandidateLocales(baseName, locale); |
|
|
|
int lastDot = baseName.lastIndexOf('.'); |
|
String category = (lastDot >= 0) ? baseName.substring(lastDot + 1) : baseName; |
|
LocaleProviderAdapter.Type type = baseName.contains(DOTCLDR) ? CLDR : JRE; |
|
LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type); |
|
Set<String> langtags = ((JRELocaleProviderAdapter)adapter).getLanguageTagSet(category); |
|
if (!langtags.isEmpty()) { |
|
for (Iterator<Locale> itr = candidates.iterator(); itr.hasNext();) { |
|
if (!LocaleProviderAdapter.isSupportedLocale(itr.next(), type, langtags)) { |
|
itr.remove(); |
|
} |
|
} |
|
} |
|
|
|
|
|
if (locale.getLanguage() != "en" |
|
&& type == CLDR && category.equals("TimeZoneNames")) { |
|
candidates.add(candidates.size() - 1, Locale.ENGLISH); |
|
} |
|
return candidates; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@Override |
|
public Locale getFallbackLocale(String baseName, Locale locale) { |
|
if (baseName == null || locale == null) { |
|
throw new NullPointerException(); |
|
} |
|
return null; |
|
} |
|
|
|
private static final String DOTCLDR = ".cldr"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@Override |
|
public String toBundleName(String baseName, Locale locale) { |
|
String newBaseName = baseName; |
|
String lang = locale.getLanguage(); |
|
if (lang.length() > 0) { |
|
if (baseName.startsWith(JRE.getUtilResourcesPackage()) |
|
|| baseName.startsWith(JRE.getTextResourcesPackage())) { |
|
|
|
assert JRE.getUtilResourcesPackage().length() |
|
== JRE.getTextResourcesPackage().length(); |
|
int index = JRE.getUtilResourcesPackage().length(); |
|
if (baseName.indexOf(DOTCLDR, index) > 0) { |
|
index += DOTCLDR.length(); |
|
} |
|
newBaseName = baseName.substring(0, index + 1) + lang |
|
+ baseName.substring(index); |
|
} |
|
} |
|
return super.toBundleName(newBaseName, locale); |
|
} |
|
} |
|
|
|
private static class SupplementaryResourceBundleControl extends LocaleDataResourceBundleControl { |
|
private static final SupplementaryResourceBundleControl INSTANCE = |
|
new SupplementaryResourceBundleControl(); |
|
|
|
private SupplementaryResourceBundleControl() { |
|
} |
|
|
|
@Override |
|
public List<Locale> getCandidateLocales(String baseName, Locale locale) { |
|
|
|
return Arrays.asList(locale); |
|
} |
|
|
|
@Override |
|
public long getTimeToLive(String baseName, Locale locale) { |
|
assert baseName.contains("JavaTimeSupplementary"); |
|
return TTL_DONT_CACHE; |
|
} |
|
} |
|
} |