]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/impl/ICUResourceTableAccess.java
Added flags.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / impl / ICUResourceTableAccess.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2009-2011, 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         String result = null;
35         try {
36             for (;;) {
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);
42                 } else {
43                     ICUResourceBundle table = lookup(bundle, tableName);
44                     if (table == null) {
45                         return item;
46                     }
47                     ICUResourceBundle stable = table;
48                     if (subtableName != null) {
49                         stable = lookup(table, subtableName);
50                     }
51                     if (stable != null) {
52                         ICUResourceBundle sbundle = lookup(stable, item);
53                         if (sbundle != null) {
54                             result = sbundle.getString(); // possible real exception
55                             break;
56                         }
57                     }
58
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);
67                         }
68                         ICUResourceBundle sbundle = lookup(table, currentName);
69                         if (sbundle != null) {
70                             result = sbundle.getString(); // possible real exception
71                             break;
72                         }
73                     }
74
75                     // still can't figure it out? try the fallback mechanism
76                     ICUResourceBundle fbundle = lookup(table, "Fallback");
77                     if (fbundle == null) {
78                         return item;
79                     }
80
81                     String fallbackLocale = fbundle.getString(); // again, possible exception
82                     if (fallbackLocale.length() == 0) {
83                         fallbackLocale = "root";
84                     }
85
86                     if (fallbackLocale.equals(table.getULocale().getName())) {
87                         return item;
88                     }
89
90                     bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
91                             bundle.getBaseName(), fallbackLocale);
92                 }
93             }
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.
97         }
98
99         // If the result is empty return item instead
100         return ((result != null && result.length() > 0) ? result : item);
101     }
102
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);
106     }
107 }