2 *******************************************************************************
3 * Copyright (C) 2009-2011, 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) {
37 // special case currency
38 if ("currency".equals(subtableName)) {
39 ICUResourceBundle table = bundle.getWithFallback("Currencies");
40 table = table.getWithFallback(item);
41 return table.getString(1);
43 ICUResourceBundle table = lookup(bundle, tableName);
47 ICUResourceBundle stable = table;
48 if (subtableName != null) {
49 stable = lookup(table, subtableName);
52 ICUResourceBundle sbundle = lookup(stable, item);
53 if (sbundle != null) {
54 result = sbundle.getString(); // possible real exception
59 // if we get here, stable was null, or sbundle was null
60 if (subtableName == null) {
61 // may be a deprecated code
62 String currentName = null;
63 if (tableName.equals("Countries")) {
64 currentName = LocaleIDs.getCurrentCountryID(item);
65 } else if (tableName.equals("Languages")) {
66 currentName = LocaleIDs.getCurrentLanguageID(item);
68 ICUResourceBundle sbundle = lookup(table, currentName);
69 if (sbundle != null) {
70 result = sbundle.getString(); // possible real exception
75 // still can't figure it out? try the fallback mechanism
76 ICUResourceBundle fbundle = lookup(table, "Fallback");
77 if (fbundle == null) {
81 String fallbackLocale = fbundle.getString(); // again, possible exception
82 if (fallbackLocale.length() == 0) {
83 fallbackLocale = "root";
86 if (fallbackLocale.equals(table.getULocale().getName())) {
90 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
91 bundle.getBaseName(), fallbackLocale);
94 } catch (Exception e) {
95 // If something is seriously wrong, we might call getString on a resource that is
96 // not a string. That will throw an exception, which we catch and ignore here.
99 // If the result is empty return item instead
100 return ((result != null && result.length() > 0) ? result : item);
103 // utility to make the call sites in the above code cleaner
104 private static ICUResourceBundle lookup(ICUResourceBundle bundle, String resName) {
105 return ICUResourceBundle.findResourceWithFallback(resName, bundle, null);