private Object FindResourceWithFallback(String[] keys, ICUListResourceBundle actualBundle) { Object obj = null; while (actualBundle != null) { // get the top level resource // getObject is a method on the ResourceBundle class that // performs the normal fallback obj = actualBundle.GetObject(keys[0], actualBundle); // now find the bundle from the actual bundle // if this bundle does not contain the top level resource, // then we can be sure that it does not contain the sub elements obj = FindResourceWithFallback(obj, keys, 1, 0); // did we get the contents? the break if (obj != null) { break; } // if not try the parent bundle actualBundle = (ICUListResourceBundle)actualBundle.parent; } return(obj); }
/// <summary> /// This method performs multilevel fallback for fetching items from the /// bundle e.g: If resource is in the form de__PHONEBOOK{ collations{ /// default{ "phonebook"} } } If the value of "default" key needs to be /// accessed, then do: <code> /// ResourceBundle bundle = new ResourceBundle(getLocaleFromString("de__PHONEBOOK")); /// Object result = null; /// if(bundle instanceof ICUListResourceBundle){ /// result = ((ICUListResourceBundle) bundle).getObjectWithFallback("collations/default"); /// } /// </code> /// </summary> /// /// <param name="path">The path to the required resource key</param> /// <returns>Object represented by the key</returns> /// <exception cref="MissingResourceException"></exception> public Object GetObjectWithFallback(String path) { String[] keys = Split(path, RES_PATH_SEP_CHAR); Object result = null; ICUListResourceBundle actualBundle = this; // now recuse to pick up sub levels of the items result = FindResourceWithFallback(keys, actualBundle); if (result == null) { throw new MissingManifestResourceException("Could not find the resource in "); } return(result); }
private Object GetObject(String key, ICUListResourceBundle actualBundle) { Object obj = HandleGetObject(key); if (obj == null) { ICUListResourceBundle p = (ICUListResourceBundle)this.parent; while (p != null) { obj = p.HandleGetObject(key); if (obj != null) { actualBundle = p; break; } p = (ICUListResourceBundle)p.parent; } } return(obj); }
// recursively build bundle private static ResourceBundle Instantiate(String name) { ResourceBundle b = LoadFromCache(name); if (b == null) { ResourceBundle parent = null; int i = name.LastIndexOf('_'); // TODO: convert this to use ULocale Locale rootLocale = new Locale("", "", ""); if (i != -1) { parent = Instantiate(name.Substring(0, (i) - (0))); } try { Locale locale = rootLocale; i = name.IndexOf('_'); if (i != -1) { locale = IBM.ICU.Impl.LocaleUtility.GetLocaleFromName(name.Substring(i + 1)); } else { i = name.Length; } Assembly cl = typeof(ICULocaleData).Assembly; if (cl == null) { // we're on the bootstrap cl = System.Reflection.Assembly.GetEntryAssembly(); } Type cls = cl.GetType(name); if (typeof(ICUListResourceBundle).IsAssignableFrom(cls)) { ICUListResourceBundle bx = (ICUListResourceBundle)Activator.CreateInstance(cls); if (parent != null) { bx.SetParentX(parent); } bx.icuLocale = locale; b = bx; // System.out.println("iculistresourcebundle: " + name + // " is " + b); } else { b = ResourceBundle.GetBundle(name.Substring(0, (i) - (0)), locale); // System.out.println("resourcebundle: " + name + " is " + // b); } AddToCache(name, b); } catch (TypeLoadException e) { int j = name.IndexOf('_'); int k = name.LastIndexOf('_'); // if a bogus locale is passed then the parent should be // the default locale not the root locale! if (k == j && j != -1) { String locName = name.Substring(j + 1, (name.Length) - (j + 1)); String defaultName = IBM.ICU.Util.ULocale.GetDefault().ToString(); if (!locName.Equals(rootLocale.ToString()) && defaultName.IndexOf(locName) == -1) { String bundle = name.Substring(0, (j) - (0)); parent = Instantiate(bundle + "_" + defaultName); } } b = parent; } catch (Exception e_0) { System.Console.Out.WriteLine("failure"); System.Console.Out.WriteLine(e_0); } } // if(b==null){ // throw new // MissingResourceException("Could not load data "+name,"",""); // } return(b); }