2 *******************************************************************************
3 * Copyright (C) 2009, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.impl;
9 import com.ibm.icu.util.ULocale;
10 import com.ibm.icu.util.UResourceBundle;
13 * Static utility functions for probing resource tables, used by ULocale and
16 public class ICUResourceTableAccess {
18 * Utility to fetch locale display data from resource bundle tables. Convenience
19 * wrapper for {@link #getTableString(ICUResourceBundle, String, String, String)}.
21 public static String getTableString(String path, ULocale locale, String tableName,
23 ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.
24 getBundleInstance(path, locale.getBaseName());
25 return getTableString(bundle, tableName, null, itemName);
29 * Utility to fetch locale display data from resource bundle tables. Uses fallback
30 * through the "Fallback" resource if available.
32 public static String getTableString(ICUResourceBundle bundle, String tableName,
33 String subtableName, String item) {
36 // special case currency
37 if ("currency".equals(subtableName)) {
38 ICUResourceBundle table = bundle.getWithFallback("Currencies");
39 table = table.getWithFallback(item);
40 return table.getString(1);
42 ICUResourceBundle table = lookup(bundle, tableName);
46 ICUResourceBundle stable = table;
47 if (subtableName != null) {
48 stable = lookup(table, subtableName);
51 ICUResourceBundle sbundle = lookup(stable, item);
52 if (sbundle != null) {
53 return sbundle.getString(); // possible real exception
57 // if we get here, stable was null, or sbundle was null
58 if (subtableName == null) {
59 // may be a deprecated code
60 String currentName = null;
61 if (tableName.equals("Countries")) {
62 currentName = LocaleIDs.getCurrentCountryID(item);
63 } else if (tableName.equals("Languages")) {
64 currentName = LocaleIDs.getCurrentLanguageID(item);
66 ICUResourceBundle sbundle = lookup(table, currentName);
67 if (sbundle != null) {
68 return sbundle.getString(); // possible real exception
72 // still can't figure it out? try the fallback mechanism
73 ICUResourceBundle fbundle = lookup(table, "Fallback");
74 if (fbundle == null) {
78 String fallbackLocale = fbundle.getString(); // again, possible exception
79 if (fallbackLocale.length() == 0) {
80 fallbackLocale = "root";
83 if (fallbackLocale.equals(table.getULocale().getName())) {
87 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
88 bundle.getBaseName(), fallbackLocale);
91 } catch (Exception e) {
92 // If something is seriously wrong, we might call getString on a resource that is
93 // not a string. That will throw an exception, which we catch and ignore here.
99 // utility to make the call sites in the above code cleaner
100 private static ICUResourceBundle lookup(ICUResourceBundle bundle, String resName) {
101 return ICUResourceBundle.findResourceWithFallback(resName, bundle, null);