ICU-12551 replace state-changing ICUResourceBundle.loadingStatus with isRoot() and OpenType.LOCALE_ONLY

X-SVN-Rev: 38750
This commit is contained in:
Markus Scherer 2016-05-18 06:59:08 +00:00
parent 3f05ba059e
commit 1824b6f66c
6 changed files with 42 additions and 152 deletions

View file

@ -46,35 +46,6 @@ public class ICUResourceBundle extends UResourceBundle {
*/
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.
@ -872,7 +843,6 @@ public class ICUResourceBundle extends UResourceBundle {
}
if (depth == keys.length) {
// We found it.
sub.setLoadingStatus(((ICUResourceBundle)requested).getLocaleID());
return sub;
}
base = sub;
@ -1109,6 +1079,17 @@ public class ICUResourceBundle extends UResourceBundle {
* 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.
@ -1205,23 +1186,18 @@ public class ICUResourceBundle extends UResourceBundle {
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{
@ -1263,7 +1239,6 @@ public class ICUResourceBundle extends UResourceBundle {
+ aKey, this.getClass().getName(), aKey);
}
}
obj.setLoadingStatus(((ICUResourceBundle)requested).getLocaleID());
return obj;
}
@ -1337,6 +1312,13 @@ public class ICUResourceBundle extends UResourceBundle {
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;
}

View file

@ -1,7 +1,7 @@
/*
******************************************************************************
* 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.
******************************************************************************
*/
@ -29,16 +29,11 @@ public class ResourceBundleWrapper extends UResourceBundle {
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;

View file

@ -199,7 +199,7 @@ public final class LocaleData {
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();
@ -284,9 +284,9 @@ public final class LocaleData {
// 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();
}

View file

@ -659,7 +659,6 @@ public abstract class UResourceBundle extends ResourceBundle {
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;
}
}
@ -705,7 +704,6 @@ public abstract class UResourceBundle extends ResourceBundle {
+ this.getClass().getName() + ", key "
+ getKey(), this.getClass().getName(), getKey());
}
((ICUResourceBundle)obj).setLoadingStatus(getLocaleID());
return obj;
}
@ -730,7 +728,6 @@ public abstract class UResourceBundle extends ResourceBundle {
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;
}
}
@ -1024,15 +1021,6 @@ public abstract class UResourceBundle extends ResourceBundle {
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

View file

@ -11,6 +11,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Set;
import java.util.TreeMap;
@ -18,6 +19,7 @@ import com.ibm.icu.impl.CurrencyData.CurrencyDisplayInfo;
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;
@ -26,11 +28,15 @@ public class ICUCurrencyDisplayInfoProvider implements CurrencyDisplayInfoProvid
}
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;
}
}
@ -75,12 +81,8 @@ public class ICUCurrencyDisplayInfoProvider implements CurrencyDisplayInfoProvid
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);
}

View file

@ -829,83 +829,7 @@ public final class ICUResourceBundleTest extends TestFmwk {
}
}
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);
@ -926,7 +850,6 @@ public final class ICUResourceBundleTest extends TestFmwk {
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;}
}