• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /*
5  *******************************************************************************
6  * Copyright (C) 2009-2014, International Business Machines Corporation and
7  * others. All Rights Reserved.
8  *******************************************************************************
9  */
10 package ohos.global.icu.impl;
11 
12 import ohos.global.icu.util.ULocale;
13 import ohos.global.icu.util.UResourceBundle;
14 
15 /**
16  * Static utility functions for probing resource tables, used by ULocale and
17  * LocaleDisplayNames.
18  * @hide exposed on OHOS
19  */
20 public class ICUResourceTableAccess {
21     /**
22      * Utility to fetch locale display data from resource bundle tables.  Convenience
23      * wrapper for {@link #getTableString(ICUResourceBundle, String, String, String, String)}.
24      */
getTableString(String path, ULocale locale, String tableName, String itemName, String defaultValue)25     public static String getTableString(String path, ULocale locale, String tableName,
26             String itemName, String defaultValue) {
27         ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.
28             getBundleInstance(path, locale.getBaseName());
29         return getTableString(bundle, tableName, null, itemName, defaultValue);
30     }
31 
32     /**
33      * Utility to fetch locale display data from resource bundle tables.  Uses fallback
34      * through the "Fallback" resource if available.
35      */
getTableString(ICUResourceBundle bundle, String tableName, String subtableName, String item, String defaultValue)36     public static String getTableString(ICUResourceBundle bundle, String tableName,
37             String subtableName, String item, String defaultValue) {
38         String result = null;
39         try {
40             for (;;) {
41                 ICUResourceBundle table = bundle.findWithFallback(tableName);
42                 if (table == null) {
43                     return defaultValue;
44                 }
45                 ICUResourceBundle stable = table;
46                 if (subtableName != null) {
47                     stable = table.findWithFallback(subtableName);
48                 }
49                 if (stable != null) {
50                     result = stable.findStringWithFallback(item);
51                     if (result != null) {
52                         break; // possible real exception
53                     }
54                 }
55 
56                 // if we get here, stable was null, or there was no string for the item
57                 if (subtableName == null) {
58                     // may be a deprecated code
59                     String currentName = null;
60                     if (tableName.equals("Countries")) {
61                         currentName = LocaleIDs.getCurrentCountryID(item);
62                     } else if (tableName.equals("Languages")) {
63                         currentName = LocaleIDs.getCurrentLanguageID(item);
64                     }
65                     if (currentName != null) {
66                         result = table.findStringWithFallback(currentName);
67                         if (result != null) {
68                             break; // possible real exception
69                         }
70                     }
71                 }
72 
73                 // still can't figure it out? try the fallback mechanism
74                 String fallbackLocale = table.findStringWithFallback("Fallback"); // again, possible exception
75                 if (fallbackLocale == null) {
76                     return defaultValue;
77                 }
78 
79                 if (fallbackLocale.length() == 0) {
80                     fallbackLocale = "root";
81                 }
82 
83                 if (fallbackLocale.equals(table.getULocale().getName())) {
84                     return defaultValue;
85                 }
86 
87                 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(
88                         bundle.getBaseName(), fallbackLocale);
89             }
90         } catch (Exception e) {
91             // If something is seriously wrong, we might call getString on a resource that is
92             // not a string.  That will throw an exception, which we catch and ignore here.
93         }
94 
95         // If the result is empty return item instead
96         return ((result != null && result.length() > 0) ? result : defaultValue);
97     }
98 }
99