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