ICU-3440 ulocale wrap up, minor tweaks to UResourceBundle/ICUResourceBundle

X-SVN-Rev: 15193
This commit is contained in:
Doug Felt 2004-05-07 18:13:39 +00:00
parent 9f8f3ace99
commit 14e67686dd
6 changed files with 712 additions and 700 deletions

View File

@ -40,10 +40,8 @@ public class CollationMiscTest extends TestFmwk{
ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(UResourceBundle.ICU_BASE_NAME,locale);
if (rb != null) {
try {
String collkey
= (String)rb.getObjectWithFallback("collations/default");
Object elements
= rb.getObjectWithFallback("collations/" + collkey);
String collkey = rb.getStringWithFallback("collations/default");
ICUResourceBundle elements = rb.getWithFallback("collations/" + collkey);
if (elements != null) {
return true;
}
@ -1588,10 +1586,8 @@ public class CollationMiscTest extends TestFmwk{
Locale l = locale[i];
try {
ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(UResourceBundle.ICU_COLLATION_BASE_NAME,l);
String collkey
= (String)rb.getObjectWithFallback("collations/default");
ICUResourceBundle elements
= (ICUResourceBundle)rb.getObjectWithFallback("collations/" + collkey);
String collkey = rb.getStringWithFallback("collations/default");
ICUResourceBundle elements = rb.getWithFallback("collations/" + collkey);
if (elements == null) {
continue;
}

View File

@ -530,15 +530,15 @@ public final class ICUResourceBundleTest extends TestFmwk {
}
}
public void TestGetObjectWithFallback(){
public void TestGetWithFallback(){
ICUResourceBundle bundle =(ICUResourceBundle) UResourceBundle.getBundleInstance(UResourceBundle.ICU_BASE_NAME,"te_IN");
String key = (String) bundle.getObjectWithFallback("Keys/collation");
String key = bundle.getStringWithFallback("Keys/collation");
if(!key.equals("COLLATION")){
errln("Did not get the expected result from getObjectWithFallback method.");
errln("Did not get the expected result from getStringWithFallback method.");
}
String type = (String) bundle.getObjectWithFallback("Types/collation/direct");
String type = bundle.getStringWithFallback("Types/collation/direct");
if(!type.equals("DIRECT")){
errln("Did not get the expected result form getObjectWithFallback method.");
errln("Did not get the expected result form getStringWithFallback method.");
}
try{
@ -546,9 +546,9 @@ public final class ICUResourceBundleTest extends TestFmwk {
if(!bundle.getULocale().equals("de")){
errln("did not get the expected bundle");
}
key = (String) bundle.getObjectWithFallback("collations/collation/default");
key = bundle.getStringWithFallback("collations/collation/default");
if(!key.equals("phonebook")){
errln("Did not get the expected result from getObjectWithFallback method.");
errln("Did not get the expected result from getStringWithFallback method.");
}
}catch(MissingResourceException ex){
@ -557,9 +557,9 @@ public final class ICUResourceBundleTest extends TestFmwk {
bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(UResourceBundle.ICU_COLLATION_BASE_NAME,"fr_FR");
key = (String) bundle.getObjectWithFallback("collations/default");
key = bundle.getStringWithFallback("collations/default");
if(!key.equals("standard")){
errln("Did not get the expected result from getObjectWithFallback method.");
errln("Did not get the expected result from getStringWithFallback method.");
}
}

View File

@ -447,22 +447,20 @@ public class ICUResourceBundle extends UResourceBundle{
* }
* If the value of "default" key needs to be accessed, then do:
* <code>
* ResourceBundle bundle = new ResourceBundle(getLocaleFromString("de__PHONEBOOK"));
* Object result = null;
* UResourceBundle bundle = UResourceBundle.getBundleInstance("de__PHONEBOOK");
* ICUResourceBundle result = null;
* if(bundle instanceof ICUListResourceBundle){
* result = ((ICUListResourceBundle) bundle).getObjectWithFallback("collations/default");
* result = ((ICUListResourceBundle) bundle).getWithFallback("collations/default");
* }
* </code>
* @param path The path to the required resource key
* @return Object represented by the key
* @return resource represented by the key
* @exception MissingResourceException
*/
public Object getObjectWithFallback(String path)
throws MissingResourceException{
public ICUResourceBundle getWithFallback(String path) throws MissingResourceException {
ICUResourceBundle result = null;
ICUResourceBundle actualBundle = this;
// now recuse to pick up sub levels of the items
result = findResourceWithFallback(path, actualBundle);
@ -473,11 +471,14 @@ public class ICUResourceBundle extends UResourceBundle{
path,
key);
}
if(result.getType()==STRING){
return result.getString();
}
return result;
}
// will throw type mismatch exception if the resource is not a string
public String getStringWithFallback(String path) throws MissingResourceException {
return getWithFallback(path).getString();
}
private ICUResourceBundle findResourceWithFallback(String path, ICUResourceBundle actualBundle) {
ICUResourceBundle sub = null;
while (actualBundle != null) {

View File

@ -1619,13 +1619,11 @@ public final class RuleBasedCollator extends Collator
try {
// TODO: this is to be updated after the key word search
// is implemented.
Object collkey
= rb.getObjectWithFallback("collations/default");
String collkey = rb.getStringWithFallback("collations/default");
// collations/default will always give a string back
// keyword for the real collation data
// if "collations/collkey" will return null if collkey == null
ICUResourceBundle elements
= (ICUResourceBundle)rb.getObjectWithFallback("collations/" + collkey);
ICUResourceBundle elements = rb.getWithFallback("collations/" + collkey);
if (elements != null) {
// TODO: Determine actual & valid locale correctly
ULocale uloc = rb.getULocale();

View File

@ -1452,7 +1452,7 @@ public final class ULocale implements Serializable {
int start = index;
while (!isDoneOrItemSeparator(next()));
--index;
return new String(id, start, index-start).trim().toLowerCase();
return new String(id, start, index-start).trim(); // leave case alone
}
/**
@ -1645,14 +1645,33 @@ public final class ULocale implements Serializable {
private static String getTableString(String tableName, String subtableName, String item, ULocale displayLocale) {
try {
ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(displayLocale);
return getTableString(tableName, subtableName, item, bundle);
} catch (Exception e) {
// System.out.println("gtsu: " + e.getMessage());
}
return item;
}
/**
* Utility to fetch locale display data from resource bundle tables.
*/
private static String getTableString(String tableName, String subtableName, String item, ICUResourceBundle bundle) {
try {
// special case currency
if ("currency".equals(subtableName)) {
ICUResourceBundle table = bundle.get("Currencies");
table = table.getWithFallback(item);
return table.getString(1);
} else {
ICUResourceBundle table = bundle.get(tableName);
if (subtableName != null) {
table = bundle.get(subtableName);
}
return table.getString(item);
}
}
catch (Exception e) {
System.out.println(e.getMessage());
// System.out.println("gtsi: " + e.getMessage());
}
return item;
}
@ -1905,8 +1924,6 @@ public final class ULocale implements Serializable {
* @draft ICU 3.0
*/
public static String getDisplayKeywordValue(String localeID, String keyword, ULocale displayLocale) {
// TODO: funky currency fallback
keyword = keyword.trim().toLowerCase();
String value = new IDParser(localeID).getKeywordValue(keyword);
return getTableString("Types", keyword, value, displayLocale);
@ -1950,27 +1967,25 @@ public final class ULocale implements Serializable {
* @draft ICU 3.0
*/
public static String getDisplayName(String localeID, ULocale displayLocale) {
// the ICU4C code takes care to generate display names when the language is empty,
// but i'm not sure when this can happen???
// lang
// lang (script, country, variant, keyword=value, ...)
// script, country, variant, keyword=value, ...
final String[] tableNames = { "Languages", "Scripts", "Countries", "Variants" };
IDParser parser = new IDParser(localeID);
String[] names = parser.getLanguageScriptCountryVariant();
Iterator keys = parser.getKeywordMap().entrySet().iterator();
ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(displayLocale);
StringBuffer buf = new StringBuffer();
IDParser parser = new IDParser(localeID);
String[] names = parser.getLanguageScriptCountryVariant();
boolean haveLanguage = names[0].length() > 0;
boolean openParen = false;
for (int i = 0; i < names.length; ++i) {
String name = names[i];
if (name.length() > 0) {
name = getTableString(tableNames[i], null, name, displayLocale);
name = getTableString(tableNames[i], null, name, bundle);
if (buf.length() > 0) { // need a separator
if (haveLanguage & !openParen) {
buf.append(" (");
@ -1983,6 +1998,7 @@ public final class ULocale implements Serializable {
}
}
Iterator keys = parser.getKeywordMap().entrySet().iterator();
while (keys.hasNext()) {
if (buf.length() > 0) {
if (haveLanguage & !openParen) {
@ -1995,9 +2011,9 @@ public final class ULocale implements Serializable {
Map.Entry e = (Map.Entry)keys.next();
String key = (String)e.getKey();
String val = (String)e.getValue();
buf.append(getTableString("Keys", null, key, displayLocale));
buf.append(getTableString("Keys", null, key, bundle));
buf.append("=");
buf.append(getTableString("Types", key, val, displayLocale));
buf.append(getTableString("Types", key, val, bundle));
}
if (openParen) {

View File

@ -149,10 +149,11 @@ public abstract class UResourceBundle extends ResourceBundle{
*
* @draft ICU 3.0
*/
public static final ResourceBundle getBundleInstance(String baseName){
public static final UResourceBundle getBundleInstance(String baseName){
return getBundleInstance( baseName, ULocale.getDefault().toString(), ICU_DATA_CLASS_LOADER );
}
public static final ResourceBundle getBundleInstance(String baseName, Locale locale){
public static final UResourceBundle getBundleInstance(String baseName, Locale locale){
return getBundleInstance(baseName, new ULocale(locale));
}