1 /* 2 ******************************************************************************* 3 * Copyright (C) 2009-2014, 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 */ getTableString(String path, ULocale locale, String tableName, String itemName)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 */ getTableString(ICUResourceBundle bundle, String tableName, String subtableName, String item)32 public static String getTableString(ICUResourceBundle bundle, String tableName, 33 String subtableName, String item) { 34 String result = null; 35 try { 36 for (;;) { 37 ICUResourceBundle table = bundle.findWithFallback(tableName); 38 if (table == null) { 39 return item; 40 } 41 ICUResourceBundle stable = table; 42 if (subtableName != null) { 43 stable = table.findWithFallback(subtableName); 44 } 45 if (stable != null) { 46 result = stable.findStringWithFallback(item); 47 if (result != null) { 48 break; // possible real exception 49 } 50 } 51 52 // if we get here, stable was null, or there was no string for the item 53 if (subtableName == null) { 54 // may be a deprecated code 55 String currentName = null; 56 if (tableName.equals("Countries")) { 57 currentName = LocaleIDs.getCurrentCountryID(item); 58 } else if (tableName.equals("Languages")) { 59 currentName = LocaleIDs.getCurrentLanguageID(item); 60 } 61 if (currentName != null) { 62 result = table.findStringWithFallback(currentName); 63 if (result != null) { 64 break; // possible real exception 65 } 66 } 67 } 68 69 // still can't figure it out? try the fallback mechanism 70 String fallbackLocale = table.findStringWithFallback("Fallback"); // again, possible exception 71 if (fallbackLocale == null) { 72 return item; 73 } 74 75 if (fallbackLocale.length() == 0) { 76 fallbackLocale = "root"; 77 } 78 79 if (fallbackLocale.equals(table.getULocale().getName())) { 80 return item; 81 } 82 83 bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance( 84 bundle.getBaseName(), fallbackLocale); 85 } 86 } catch (Exception e) { 87 // If something is seriously wrong, we might call getString on a resource that is 88 // not a string. That will throw an exception, which we catch and ignore here. 89 } 90 91 // If the result is empty return item instead 92 return ((result != null && result.length() > 0) ? result : item); 93 } 94 } 95