]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/classes/core/src/com/ibm/icu/impl/ICUResourceTableAccess.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / classes / core / src / com / ibm / icu / impl / ICUResourceTableAccess.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2009, International Business Machines Corporation and         *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.impl;
8
9 import com.ibm.icu.util.ULocale;
10 import com.ibm.icu.util.UResourceBundle;
11
12 /**
13  * Static utility functions for probing resource tables, used by ULocale and
14  * LocaleDisplayNames.
15  */
16 public class ICUResourceTableAccess {
17     /**
18      * Utility to fetch locale display data from resource bundle tables.  Convenience
19      * wrapper for {@link #getTableString(ICUResourceBundle, String, String, String)}.
20      */
21     public static String getTableString(String path, ULocale locale, String tableName,
22             String itemName) {
23         ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.
24             getBundleInstance(path, locale.getBaseName());
25         return getTableString(bundle, tableName, null, itemName);
26     }
27
28     /**
29      * Utility to fetch locale display data from resource bundle tables.  Uses fallback
30      * through the "Fallback" resource if available.
31      */
32     public static String getTableString(ICUResourceBundle bundle, String tableName,
33             String subtableName, String item) {
34         try {
35             for (;;) {
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);
41                 } else {
42                     ICUResourceBundle table = lookup(bundle, tableName);
43                     if (table == null) {
44                         return item;
45                     }
46                     ICUResourceBundle stable = table;
47                     if (subtableName != null) {
48                         stable = lookup(table, subtableName);
49                     }
50                     if (stable != null) {
51                         ICUResourceBundle sbundle = lookup(stable, item);
52                         if (sbundle != null) {
53                             return sbundle.getString(); // possible real exception
54                         }
55                     }
56
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);
65                         }
66                         ICUResourceBundle sbundle = lookup(table, currentName);
67                         if (sbundle != null) {
68                             return sbundle.getString(); // possible real exception
69                         }
70                     }
71
72                     // still can't figure it out? try the fallback mechanism
73                     ICUResourceBundle fbundle = lookup(table, "Fallback");
74                     if (fbundle == null) {
75                         return item;
76                     }
77
78                     String fallbackLocale = fbundle.getString(); // again, possible exception
79                     if (fallbackLocale.length() == 0) {
80                         fallbackLocale = "root";
81                     }
82
83                     if (fallbackLocale.equals(table.getULocale().getName())) {
84                         return item;
85                     }
86
87                     bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
88                             bundle.getBaseName(), fallbackLocale);
89                 }
90             }
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.
94         }
95
96         return item;
97     }
98
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);
102     }
103 }