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