*/
protected static final String INSTALLED_LOCALES = "InstalledLocales";
- public static final int FROM_FALLBACK = 1, FROM_ROOT = 2, FROM_DEFAULT = 3, FROM_LOCALE = 4;
-
- private int loadingStatus = -1;
-
- public void setLoadingStatus(int newStatus) {
- loadingStatus = newStatus;
- }
- /**
- * Returns the loading status of a particular resource.
- *
- * @return FROM_FALLBACK if the resource is fetched from fallback bundle
- * FROM_ROOT if the resource is fetched from root bundle.
- * FROM_DEFAULT if the resource is fetched from the default locale.
- */
- public int getLoadingStatus() {
- return loadingStatus;
- }
-
- public void setLoadingStatus(String requestedLocale){
- String locale = getLocaleID();
- if(locale.equals("root")) {
- setLoadingStatus(FROM_ROOT);
- } else if(locale.equals(requestedLocale)) {
- setLoadingStatus(FROM_LOCALE);
- } else {
- setLoadingStatus(FROM_FALLBACK);
- }
- }
-
/**
* Fields for a whole bundle, rather than any specific resource in the bundle.
* Corresponds roughly to ICU4C/source/common/uresimp.h struct UResourceDataEntry.
}
if (depth == keys.length) {
// We found it.
- sub.setLoadingStatus(((ICUResourceBundle)requested).getLocaleID());
return sub;
}
base = sub;
* such as case mappings, collation, and segmentation (BreakIterator).
*/
LOCALE_ROOT,
+ /**
+ * Open a resource bundle for the locale;
+ * if there is not even a base language bundle, then fail;
+ * never fall back to the default locale nor to the root locale.
+ *
+ * <p>This is used when fallback to another language is not desired
+ * and the root locale is not generally useful.
+ * For example, {@link com.ibm.icu.util.LocaleData#setNoSubstitute(boolean)}
+ * or currency display names for {@link com.ibm.icu.text.LocaleDisplayNames}.
+ */
+ LOCALE_ONLY,
/**
* Open a resource bundle for the exact bundle name as requested;
* no fallbacks, do not load parent bundles.
if(b == null){
int i = localeName.lastIndexOf('_');
if (i != -1) {
+ // Chop off the last underscore and the subtag after that.
String temp = localeName.substring(0, i);
b = (ICUResourceBundle)instantiateBundle(baseName, temp, root, openType);
- if(b!=null && b.getULocale().getName().equals(temp)){
- b.setLoadingStatus(ICUResourceBundle.FROM_FALLBACK);
- }
}else{
+ // No underscore, only a base language subtag.
if(openType == OpenType.LOCALE_DEFAULT_ROOT &&
!defaultLocale.getLanguage().equals(localeName)) {
+ // Go to the default locale before root.
b = (ICUResourceBundle)instantiateBundle(baseName, defaultID, root, openType);
- if(b!=null){
- b.setLoadingStatus(ICUResourceBundle.FROM_DEFAULT);
- }
- }else if(rootLocale.length()!=0){
+ } else if(openType != OpenType.LOCALE_ONLY && !rootLocale.isEmpty()) {
+ // Ultimately go to root.
b = ICUResourceBundle.createBundle(baseName, rootLocale, root);
- if(b!=null){
- b.setLoadingStatus(ICUResourceBundle.FROM_ROOT);
- }
}
}
}else{
+ aKey, this.getClass().getName(), aKey);
}
}
- obj.setLoadingStatus(((ICUResourceBundle)requested).getLocaleID());
return obj;
}
return wholeBundle.ulocale;
}
+ /**
+ * Returns true if this is the root bundle, or an item in the root bundle.
+ */
+ public boolean isRoot() {
+ return wholeBundle.localeID.isEmpty() || wholeBundle.localeID.equals("root");
+ }
+
public UResourceBundle getParent() {
return (UResourceBundle) parent;
}
/*
******************************************************************************
-* Copyright (C) 2004-2015, International Business Machines Corporation and *
-* others. All Rights Reserved. *
+* Copyright (C) 2004-2016, International Business Machines Corporation and
+* others. All Rights Reserved.
******************************************************************************
*/
private String localeID = null;
private String baseName = null;
private List<String> keys = null;
-// private int loadingStatus = -1;
-
+
private ResourceBundleWrapper(ResourceBundle bundle){
this.bundle=bundle;
}
- protected void setLoadingStatus(int newStatus){
-// loadingStatus = newStatus;
- }
-
protected Object handleGetObject(String aKey){
ResourceBundleWrapper current = this;
Object obj = null;
final String aKey = exemplarSetTypes[extype]; // will throw an out-of-bounds exception
ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get(aKey);
- if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) ) {
+ if (noSubstitute && !bundle.isRoot() && stringBundle.isRoot()) {
return null;
}
String unicodeSetPattern = stringBundle.getString();
// Only some of the quotation marks may be here. So we make sure that we do a multilevel fallback.
ICUResourceBundle stringBundle = delimitersBundle.getWithFallback(DELIMITER_TYPES[type]);
- if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) )
+ if (noSubstitute && !bundle.isRoot() && stringBundle.isRoot()) {
return null;
-
+ }
return stringBundle.getString();
}
for (UResourceBundle res = this; res != null; res = res.getParent()) {
UResourceBundle obj = res.handleGet(aKey, null, this);
if (obj != null) {
- ((ICUResourceBundle) obj).setLoadingStatus(getLocaleID());
return obj;
}
}
+ this.getClass().getName() + ", key "
+ getKey(), this.getClass().getName(), getKey());
}
- ((ICUResourceBundle)obj).setLoadingStatus(getLocaleID());
return obj;
}
for (UResourceBundle res = this; res != null; res = res.getParent()) {
UResourceBundle obj = res.handleGet(index, null, this);
if (obj != null) {
- ((ICUResourceBundle) obj).setLoadingStatus(getLocaleID());
return obj;
}
}
return obj;
}
- /**
- * This method is for setting the loading status of the resource.
- * The status is analogous to the warning status in ICU4C.
- * @internal
- * @deprecated This API is ICU internal only.
- */
- @Deprecated
- protected abstract void setLoadingStatus(int newStatus);
-
/**
* Is this a top-level resource, that is, a whole bundle?
* @return true if this is a top-level resource
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
+import java.util.MissingResourceException;
import java.util.Set;
import java.util.TreeMap;
import com.ibm.icu.impl.CurrencyData.CurrencyDisplayInfoProvider;
import com.ibm.icu.impl.CurrencyData.CurrencyFormatInfo;
import com.ibm.icu.impl.CurrencyData.CurrencySpacingInfo;
+import com.ibm.icu.impl.ICUResourceBundle.OpenType;
import com.ibm.icu.util.ULocale;
import com.ibm.icu.util.UResourceBundle;
}
public CurrencyDisplayInfo getInstance(ULocale locale, boolean withFallback) {
- ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(
- ICUData.ICU_CURR_BASE_NAME, locale);
- if (!withFallback) {
- int status = rb.getLoadingStatus();
- if (status == ICUResourceBundle.FROM_DEFAULT || status == ICUResourceBundle.FROM_ROOT) {
+ ICUResourceBundle rb;
+ if (withFallback) {
+ rb = (ICUResourceBundle) ICUResourceBundle.getBundleInstance(
+ ICUData.ICU_CURR_BASE_NAME, locale, OpenType.LOCALE_DEFAULT_ROOT);
+ } else {
+ try {
+ rb = (ICUResourceBundle) ICUResourceBundle.getBundleInstance(
+ ICUData.ICU_CURR_BASE_NAME, locale, OpenType.LOCALE_ONLY);
+ } catch (MissingResourceException e) {
return null;
}
}
if (currencies != null) {
ICUResourceBundle result = currencies.findWithFallback(isoCode);
if (result != null) {
- if (!fallback) {
- int status = result.getLoadingStatus();
- if (status == ICUResourceBundle.FROM_DEFAULT ||
- status == ICUResourceBundle.FROM_ROOT) {
- return null;
- }
+ if (!fallback && !rb.isRoot() && result.isRoot()) {
+ return null;
}
return result.getString(symbolName ? 0 : 1);
}
}
}
- private String getLSString(int status){
- switch(status){
- case ICUResourceBundle.FROM_FALLBACK:
- return "FROM_FALLBACK";
- case ICUResourceBundle.FROM_DEFAULT:
- return "FROM_DEFAULT";
- case ICUResourceBundle.FROM_ROOT:
- return "FROM_ROOT";
- case ICUResourceBundle.FROM_LOCALE:
- return "FROM_LOCALE";
- default:
- return "UNKNOWN";
- }
- }
-
- private void assertEqualLoadingStatus(String msg, int target, int result) {
- if (result != target) {
- errln(msg + " expected: "+ getLSString(target)
- + " got: " + getLSString(result));
- }
- }
-
- @SuppressWarnings("unused")
- private void assertDefaultLoadingStatus(String msg, int result) {
- assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_DEFAULT, result);
- }
-
- private void assertFallbackLoadingStatus(String msg, int result) {
- assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_FALLBACK, result);
- }
-
- private void assertRootLoadingStatus(String msg, int result) {
- assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_ROOT, result);
- }
-
- private void assertLocaleLoadingStatus(String msg, int result) {
- assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_LOCALE, result);
- }
-
- public void TestLoadingStatus(){
- ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "yi_IL");
- assertFallbackLoadingStatus("base/yi_IL", bundle.getLoadingStatus());
- bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "eo_DE");
- assertFallbackLoadingStatus("base/eo_DE", bundle.getLoadingStatus());
-
- logln("Test to verify loading status of get(String)");
- bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_LANG_BASE_NAME, "zh_Hant_TW");
- ICUResourceBundle countries = (ICUResourceBundle) bundle.get("Languages");
- assertFallbackLoadingStatus("lang/Languages/zh_Hant_TW", countries.getLoadingStatus());
-
- /*
- UResourceBundle auxExemplar = bundle.get("AuxExemplarCharacters");
- status = auxExemplar.getLoadingStatus();
- if(status != UResourceBundle.FROM_ROOT){
- errln("Did not get the expected value for loading status. Expected "+ getLSString(UResourceBundle.FROM_ROOT)
- + " Got: " + getLSString(status));
- }
- */
-
- logln("Test to verify root loading status of get()");
- bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "te_IN");
- ICUResourceBundle ms = (ICUResourceBundle) bundle.get("layout");
- assertRootLoadingStatus("base/layout/te_IN", ms.getLoadingStatus());
-
- logln("Test to verify loading status of getwithFallback");
- bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "sh_YU", testLoader);
- ICUResourceBundle temp = (ICUResourceBundle) bundle.getWithFallback("a/a2");
- assertLocaleLoadingStatus("testdata/a/a2/sh_YU", temp.getLoadingStatus());
-
- temp = bundle.getWithFallback("a/a1");
- assertFallbackLoadingStatus("testdata/a/a1/sh_YU", temp.getLoadingStatus());
-
- temp = bundle.getWithFallback("a/a4");
- assertRootLoadingStatus("testdata/a/a4/sh_YU", temp.getLoadingStatus());
- }
-
public void TestCoverage(){
UResourceBundle bundle;
bundle = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME);
protected String getLocaleID() {return null;}
protected String getBaseName() {return null;}
protected UResourceBundle getParent() {return null;}
- protected void setLoadingStatus(int newStatus) {}
public Enumeration getKeys() {return null;}
protected Object handleGetObject(String aKey) {return null;}
}