1 8 9 package com.ibm.icu.impl; 10 11 import java.io.BufferedReader ; 12 import java.io.InputStream ; 13 import java.io.InputStreamReader ; 14 import java.io.IOException ; 15 import java.lang.ref.SoftReference ; 16 import java.net.URL ; 17 import java.util.ArrayList ; 18 import java.util.Arrays ; 19 import java.util.Collections ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 import java.util.MissingResourceException ; 26 import java.util.Set ; 27 import java.util.Vector ; 28 29 import com.ibm.icu.impl.ByteBuffer; 33 import com.ibm.icu.impl.URLHandler.URLVisitor; 35 import com.ibm.icu.util.StringTokenizer; 36 import com.ibm.icu.util.ULocale; 37 import com.ibm.icu.util.UResourceBundle; 38 import com.ibm.icu.util.UResourceTypeMismatchException; 39 import com.ibm.icu.util.VersionInfo; 40 41 public abstract class ICUResourceBundle extends UResourceBundle { 42 46 protected static final String ICU_DATA_PATH = "com/ibm/icu/impl/"; 47 51 public static final String ICU_BUNDLE = "data/icudt" + VersionInfo.ICU_DATA_VERSION; 52 53 57 public static final String ICU_BASE_NAME = ICU_DATA_PATH + ICU_BUNDLE; 58 59 63 public static final String ICU_COLLATION_BASE_NAME = ICU_BASE_NAME + "/coll"; 64 65 69 public static final String ICU_BRKITR_NAME = "/brkitr"; 70 71 75 public static final String ICU_BRKITR_BASE_NAME = ICU_BASE_NAME + ICU_BRKITR_NAME; 76 77 81 public static final String ICU_RBNF_BASE_NAME = ICU_BASE_NAME + "/rbnf"; 82 83 87 public static final String ICU_TRANSLIT_BASE_NAME = ICU_BASE_NAME + "/translit"; 88 89 93 public static final ClassLoader ICU_DATA_CLASS_LOADER; 94 static { 95 ClassLoader loader = ICUData.class.getClassLoader(); 96 if (loader == null) { loader = ClassLoader.getSystemClassLoader(); 98 } 99 ICU_DATA_CLASS_LOADER = loader; 100 } 101 102 106 protected static final String INSTALLED_LOCALES = "InstalledLocales"; 107 108 112 public static final int NONE = -1; 113 114 118 public static final int STRING = 0; 119 120 124 public static final int BINARY = 1; 125 126 130 public static final int TABLE = 2; 131 132 140 protected static final int ALIAS = 3; 141 142 149 protected static final int TABLE32 = 4; 150 151 157 public static final int INT = 7; 158 159 163 public static final int ARRAY = 8; 164 165 170 public static final int INT_VECTOR = 14; 171 172 public static final int FROM_FALLBACK = 1, FROM_ROOT = 2, FROM_DEFAULT = 3, FROM_LOCALE = 4; 173 174 private int loadingStatus = -1; 175 176 public void setLoadingStatus(int newStatus) { 177 loadingStatus = newStatus; 178 } 179 186 public int getLoadingStatus() { 187 return loadingStatus; 188 } 189 190 194 protected boolean getNoFallback() { 195 return false; 196 } 197 198 204 public VersionInfo getVersion() { 205 return null; 206 } 207 208 219 public String getString() { 220 throw new UResourceTypeMismatchException(""); 221 } 222 223 226 public String [] getStringArray() { 227 throw new UResourceTypeMismatchException(""); 228 } 229 230 241 245 255 public ByteBuffer getBinary() { 256 throw new UResourceTypeMismatchException(""); 257 } 258 259 272 public byte [] getBinary(byte []ba) { 273 throw new UResourceTypeMismatchException(""); 274 } 275 276 286 public int[] getIntVector() { 287 throw new UResourceTypeMismatchException(""); 288 } 289 290 300 public int getInt() { 301 throw new UResourceTypeMismatchException(""); 302 } 303 304 315 public int getUInt() { 316 throw new UResourceTypeMismatchException(""); 317 } 318 327 public int getSize() { 328 return size; 329 } 330 331 340 public int getType() { 341 int type = ICUResourceBundleImpl.RES_GET_TYPE(resource); 342 if(type==TABLE32){ 343 return TABLE; } 345 return type; 346 } 347 348 354 public String getKey() { 355 return key; 356 } 357 358 363 public ICUResourceBundleIterator getIterator() { 364 return new ICUResourceBundleIterator(this); 365 } 366 367 375 public ICUResourceBundle get(int index) { 376 return getImpl(index, null, this); 377 } 378 protected ICUResourceBundle getImpl(int index, HashMap table, 379 ICUResourceBundle requested) { 380 ICUResourceBundle obj = handleGet(index, table, requested); 381 if (obj == null) { 382 obj = (ICUResourceBundle) getParent(); 383 if (obj != null) { 384 obj = obj.getImpl(index, table, requested); 385 } 386 if (obj == null) 387 throw new MissingResourceException ( 388 "Can't find resource for bundle " 389 + this.getClass().getName() + ", key " 390 + getKey(), this.getClass().getName(), getKey()); 391 } 392 setLoadingStatus(obj, requested.getLocaleID()); 393 return obj; 394 } 395 397 405 public ICUResourceBundle get(String key) { 406 return getImpl(key, null, this); 407 } 408 protected ICUResourceBundle getImpl(String key, HashMap table, 409 ICUResourceBundle requested) { 410 ICUResourceBundle obj = handleGet(key, table, requested); 411 if (obj == null) { 412 obj = (ICUResourceBundle) getParent(); 413 if (obj != null) { 414 obj = obj.getImpl(key, table, requested); 416 } 417 if (obj == null) { 418 String fullName = ICUResourceBundleReader.getFullName( 419 getBaseName(), getLocaleID()); 420 throw new MissingResourceException ( 421 "Can't find resource for bundle " + fullName + ", key " 422 + key, this.getClass().getName(), key); 423 } 424 } 425 setLoadingStatus(obj, requested.getLocaleID()); 426 return obj; 427 } 428 429 private static void setLoadingStatus(ICUResourceBundle bundle, String requestedLocale){ 430 String locale = bundle.getLocaleID(); 431 if(locale.equals("root")){ 432 bundle.setLoadingStatus(FROM_ROOT); 433 return; 434 } 435 if(locale.equals(requestedLocale)){ 436 bundle.setLoadingStatus(FROM_LOCALE); 437 }else{ 438 bundle.setLoadingStatus(FROM_FALLBACK); 439 } 440 } 441 442 451 public String getString(int index) { 452 ICUResourceBundle temp = get(index); 453 if (temp.getType() == STRING) { 454 return temp.getString(); 455 } 456 throw new UResourceTypeMismatchException(""); 457 } 458 459 464 public abstract UResourceBundle getParent(); 465 466 471 protected abstract String getLocaleID(); 472 473 485 public static final ULocale getFunctionalEquivalent(String baseName, 486 String resName, String keyword, ULocale locID, 487 boolean fillinIsAvailable[]) { 488 String kwVal = locID.getKeywordValue(keyword); 489 String baseLoc = locID.getBaseName(); 490 String defStr = null; 491 ULocale parent = new ULocale(baseLoc); 492 ULocale found = locID; 493 ULocale defLoc = null; boolean lookForDefault = false; ULocale fullBase = null; int defDepth = 0; int resDepth = 0; if (fillinIsAvailable != null) { 499 fillinIsAvailable[0] = true; 500 } 501 502 if ((kwVal == null) || (kwVal.length() == 0) 503 || kwVal.equals(DEFAULT_TAG)) { 504 kwVal = ""; lookForDefault = true; 506 } 507 508 ICUResourceBundle r = null; 510 511 r = (ICUResourceBundle) UResourceBundle.getBundleInstance(baseName, parent); 512 found = r.getULocale(); 513 if (fillinIsAvailable != null) { 514 if (!found.equals(parent)) { 515 fillinIsAvailable[0] = false; 516 } 517 } 518 do { 520 try { 521 ICUResourceBundle irb = r.get(resName); 522 defStr = irb.getString(DEFAULT_TAG); 523 if (lookForDefault == true) { 524 kwVal = defStr; 525 lookForDefault = false; 526 } 527 defLoc = r.getULocale(); 528 } catch (MissingResourceException t) { 529 } 531 if (defLoc == null) { 532 r = (ICUResourceBundle) r.getParent(); 533 defDepth++; 534 } 535 } while ((r != null) && (defLoc == null)); 536 537 parent = new ULocale(baseLoc); 539 r = (ICUResourceBundle) UResourceBundle.getBundleInstance(baseName, parent); 540 do { 542 try { 543 ICUResourceBundle irb = r.get(resName); 544 irb.get(kwVal); 545 fullBase = irb.getULocale(); 546 if ((fullBase != null) && ((resDepth) > defDepth)) { 550 defStr = irb.getString(DEFAULT_TAG); 551 defLoc = r.getULocale(); 552 defDepth = resDepth; 553 } 554 } catch (MissingResourceException t) { 555 } 557 if (fullBase == null) { 558 r = (ICUResourceBundle) r.getParent(); 559 resDepth++; 560 } 561 } while ((r != null) && (fullBase == null)); 562 563 if (fullBase == null && (defStr != null) && !defStr.equals(kwVal)) { kwVal = defStr; parent = new ULocale(baseLoc); 569 r = (ICUResourceBundle) UResourceBundle.getBundleInstance(baseName, parent); 570 resDepth = 0; 571 do { 573 try { 574 ICUResourceBundle irb = r.get(resName); 575 UResourceBundle urb = irb.get(kwVal); 576 577 fullBase = r.getULocale(); 579 580 if(!fullBase.toString().equals(urb.getLocale().toString())) { 583 fullBase = null; } 585 586 if ((fullBase != null) && ((resDepth) > defDepth)) { 589 defStr = irb.getString(DEFAULT_TAG); 590 defLoc = r.getULocale(); 591 defDepth = resDepth; 592 } 593 } catch (MissingResourceException t) { 594 } 596 if (fullBase == null) { 597 r = (ICUResourceBundle) r.getParent(); 598 resDepth++; 599 } 600 } while ((r != null) && (fullBase == null)); 601 } 602 603 if (fullBase == null) { 604 throw new MissingResourceException ( 605 "Could not find locale containing requested or default keyword.", 606 baseName, keyword + "=" + kwVal); 607 } 608 609 if (defStr.equals(kwVal) && resDepth <= defDepth) { return fullBase; } else { 613 return new ULocale(fullBase.toString() + "@" + keyword + "=" + kwVal); 614 } 615 } 616 617 624 public static final String [] getKeywordValues(String baseName, String keyword) { 625 Set keywords = new HashSet (); 626 ULocale locales[] = createULocaleList(baseName, ICU_DATA_CLASS_LOADER); 627 int i; 628 629 for (i = 0; i < locales.length; i++) { 630 try { 631 UResourceBundle b = UResourceBundle.getBundleInstance(baseName, locales[i]); 632 ICUResourceBundle irb = (ICUResourceBundle) (b.getObject(keyword)); 634 Enumeration e = irb.getKeys(); 635 Object s; 636 while (e.hasMoreElements()) { 637 s = e.nextElement(); 638 if ((s instanceof String ) && !DEFAULT_TAG.equals(s)) { 639 keywords.add(s); 641 } 642 } 643 } catch (Throwable t) { 644 } 648 } 649 return (String [])keywords.toArray(new String [0]); 650 } 651 652 669 public ICUResourceBundle getWithFallback(String path) 670 throws MissingResourceException { 671 ICUResourceBundle result = null; 672 ICUResourceBundle actualBundle = this; 673 674 result = findResourceWithFallback(path, actualBundle, null); 676 677 if (result == null) { 678 throw new MissingResourceException ( 679 "Can't find resource for bundle " 680 + this.getClass().getName() + ", key " + getType(), 681 path, getKey()); 682 } 683 return result; 684 } 685 686 public String getStringWithFallback(String path) throws MissingResourceException { 688 return getWithFallback(path).getString(); 689 } 690 691 697 public static Set getAvailableLocaleNameSet(String bundlePrefix) { 698 return getAvailEntry(bundlePrefix).getLocaleNameSet(); 699 } 700 701 705 public static Set getFullLocaleNameSet() { 706 return getFullLocaleNameSet(ICU_BASE_NAME); 707 } 708 709 715 public static Set getFullLocaleNameSet(String bundlePrefix) { 716 return getAvailEntry(bundlePrefix).getFullLocaleNameSet(); 717 } 718 719 723 public static Set getAvailableLocaleNameSet() { 724 return getAvailableLocaleNameSet(ICU_BASE_NAME); 725 } 726 727 732 public static final ULocale[] getAvailableULocales(String baseName) { 733 return getAvailEntry(baseName).getULocaleList(); 734 } 735 736 741 public static final ULocale[] getAvailableULocales() { 742 return getAvailableULocales(ICU_BASE_NAME); 743 } 744 745 750 public static final Locale [] getAvailableLocales(String baseName) { 751 return getAvailEntry(baseName).getLocaleList(); 752 } 753 754 759 public static final Locale [] getAvailableLocales() { 760 return getAvailEntry(ICU_BASE_NAME).getLocaleList(); 761 } 762 763 771 public static final Locale [] getLocaleList(ULocale[] ulocales) { 772 ArrayList list = new ArrayList (); 773 for (int i = 0; i < ulocales.length; i++) { 774 if (ulocales[i].getScript().length() == 0) { 777 list.add(ulocales[i].toLocale()); 778 } 779 } 780 return (Locale []) list.toArray(new Locale [list.size()]); 781 } 782 783 public Enumeration getKeys() { 784 initKeysVector(); 785 return keys.elements(); 786 } 787 private Vector keys = null; 788 private synchronized void initKeysVector(){ 789 if(keys!=null){ 790 return; 791 } 792 keys = new Vector (); 794 Enumeration e = this.handleGetKeys(); 796 while(e.hasMoreElements()){ 797 String elem = (String )e.nextElement(); 798 if(!keys.contains(elem)){ 799 keys.add(elem); 800 } 801 } 802 } 805 protected Enumeration handleGetKeys(){ 806 Vector keys = new Vector (); 807 ICUResourceBundle item = null; 808 for (int i = 0; i < size; i++) { 809 item = get(i); 810 keys.add(item.getKey()); 811 } 812 return keys.elements(); 813 } 814 815 public static ICUResourceBundle createBundle(String baseName, 816 String localeID, ClassLoader root) { 817 ICUResourceBundle b = ICUResourceBundleImpl.createBundle(baseName, localeID, root); 818 if(b==null){ 819 throw new MissingResourceException ("Could not find the bundle "+ baseName+"/"+ localeID+".res","",""); 820 } 821 return b; 822 } 823 824 protected String key; 826 protected int size = 1; 827 protected String resPath; 828 protected long resource = RES_BOGUS; 829 protected boolean isTopLevel = false; 830 831 protected static final long UNSIGNED_INT_MASK = 0xffffffffL; 832 833 protected static final long RES_BOGUS = 0xffffffff; 834 835 protected ICUResourceBundle handleGet(String key, HashMap table, 836 ICUResourceBundle requested) { 837 throw new UResourceTypeMismatchException(""); 838 } 839 protected ICUResourceBundle handleGet(int index, HashMap table, 840 ICUResourceBundle requested) { 841 throw new UResourceTypeMismatchException(""); 842 } 843 844 851 public Locale getLocale() { 852 return getULocale().toLocale(); 853 } 854 855 protected Object handleGetObject(String key) { 858 return handleGetObjectImpl(key, this); 859 } 860 861 private Object handleGetObjectImpl(String key, ICUResourceBundle requested) { 868 Object obj = resolveObject(key, requested); 869 if (obj == null) { 870 ICUResourceBundle parent = (ICUResourceBundle) getParent(); 871 if (parent != null) { 872 obj = parent.handleGetObjectImpl(key, requested); 873 } 874 if (obj == null) 875 throw new MissingResourceException ( 876 "Can't find resource for bundle " 877 + this.getClass().getName() + ", key " + key, 878 this.getClass().getName(), key); 879 } 880 return obj; 881 } 882 883 private Object resolveObject(String key, ICUResourceBundle requested) { 884 if (getType() == STRING) { 885 return getString(); 886 } 887 ICUResourceBundle obj = handleGet(key, requested); 888 if (obj != null) { 889 if (obj.getType() == STRING) { 890 return obj.getString(); 891 } 892 try { 893 if (obj.getType() == ARRAY) { 894 return obj.handleGetStringArray(); 895 } 896 } catch (UResourceTypeMismatchException ex) { 897 return obj; 898 } 899 } 900 return obj; 901 } 902 903 protected ICUResourceBundle handleGet(int index, ICUResourceBundle requested) { 904 return null; 905 } 906 907 protected ICUResourceBundle handleGet(String key, 908 ICUResourceBundle requested) { 909 return null; 910 } 911 912 protected String [] handleGetStringArray() { 913 return null; 914 } 915 916 private static final String ICU_RESOURCE_INDEX = "res_index"; 918 919 private static final String DEFAULT_TAG = "default"; 920 921 private static final boolean DEBUG = ICUDebug.enabled("localedata"); 923 924 private static SoftReference GET_AVAILABLE_CACHE; 926 private static final ULocale[] createULocaleList(String baseName, 927 ClassLoader root) { 928 ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.instantiateBundle(baseName, ICU_RESOURCE_INDEX, root, true); 934 935 bundle = bundle.get(INSTALLED_LOCALES); 936 int length = bundle.getSize(); 937 int i = 0; 938 ULocale[] locales = new ULocale[length]; 939 ICUResourceBundleIterator iter = bundle.getIterator(); 940 iter.reset(); 941 while (iter.hasNext()) { 942 locales[i++] = new ULocale(iter.next().getKey()); 943 } 944 bundle = null; 945 return locales; 946 } 947 948 private static final Locale [] createLocaleList(String baseName) { 949 ULocale[] ulocales = getAvailEntry(baseName).getULocaleList(); 950 return getLocaleList(ulocales); 951 } 952 953 private static final String [] createLocaleNameArray(String baseName, 954 ClassLoader root) { 955 ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.instantiateBundle( baseName, ICU_RESOURCE_INDEX, root, true); 956 bundle = bundle.get(INSTALLED_LOCALES); 957 int length = bundle.getSize(); 958 int i = 0; 959 String [] locales = new String [length]; 960 ICUResourceBundleIterator iter = bundle.getIterator(); 961 iter.reset(); 962 while (iter.hasNext()) { 963 locales[i++] = iter.next().getKey(); 964 } 965 bundle = null; 966 return locales; 967 } 968 969 private static final ArrayList createFullLocaleNameArray( 970 final String baseName, final ClassLoader root) { 971 972 ArrayList list = (ArrayList ) java.security.AccessController 973 .doPrivileged(new java.security.PrivilegedAction () { 974 public Object run() { 975 String bn = baseName.endsWith("/") 978 ? baseName 979 : baseName + "/"; 980 981 try { 983 InputStream s = root.getResourceAsStream(bn + ICU_RESOURCE_INDEX + ".txt"); 984 if (s != null) { 985 ArrayList list = new ArrayList (); 986 BufferedReader br = new BufferedReader (new InputStreamReader (s, "ASCII")); 987 String line; 988 while ((line = br.readLine()) != null) { 989 if (line.length() != 0 && !line.startsWith("#")) { 990 list.add(line); 991 } 992 } 993 return list; 994 } 995 } catch (IOException e) { 996 } 998 999 URL url = root.getResource(bn); 1000 URLHandler handler = URLHandler.get(url); 1001 if (handler != null) { 1002 final ArrayList list = new ArrayList (); 1003 URLVisitor v = new URLVisitor() { 1004 public void visit(String s) { 1005 if (s.endsWith(".res") && !"res_index.res".equals(s)) { 1006 list.add(s.substring(0, s.length() - 4)); } 1008 } 1009 }; 1010 handler.guide(v, false); 1011 return list; 1012 } 1013 1014 return null; 1015 } 1016 }); 1017 1018 return list; 1019 } 1020 1021 private static Set createFullLocaleNameSet(String baseName) { 1022 ArrayList list = createFullLocaleNameArray(baseName,ICU_DATA_CLASS_LOADER); 1023 HashSet set = new HashSet (); 1024 if(list==null){ 1025 throw new MissingResourceException ("Could not find "+ ICU_RESOURCE_INDEX, "", ""); 1026 } 1027 set.addAll(list); 1028 return Collections.unmodifiableSet(set); 1029 } 1030 1031 private static Set createLocaleNameSet(String baseName) { 1032 try { 1033 String [] locales = createLocaleNameArray(baseName, ICU_DATA_CLASS_LOADER); 1034 1035 HashSet set = new HashSet (); 1036 set.addAll(Arrays.asList(locales)); 1037 return Collections.unmodifiableSet(set); 1038 } catch (MissingResourceException e) { 1039 if (DEBUG) { 1040 System.out.println("couldn't find index for bundleName: " + baseName); 1041 Thread.dumpStack(); 1042 } 1043 } 1044 return Collections.EMPTY_SET; 1045 } 1046 1047 1051 private static final class AvailEntry { 1052 private String prefix; 1053 private ULocale[] ulocales; 1054 private Locale [] locales; 1055 private Set nameSet; 1056 private Set fullNameSet; 1057 1058 AvailEntry(String prefix) { 1059 this.prefix = prefix; 1060 } 1061 1062 ULocale[] getULocaleList() { 1063 if (ulocales == null) { 1064 ulocales = createULocaleList(prefix, ICU_DATA_CLASS_LOADER); 1065 } 1066 return ulocales; 1067 } 1068 Locale [] getLocaleList() { 1069 if (locales == null) { 1070 locales = createLocaleList(prefix); 1071 } 1072 return locales; 1073 } 1074 Set getLocaleNameSet() { 1075 if (nameSet == null) { 1076 nameSet = createLocaleNameSet(prefix); 1077 } 1078 return nameSet; 1079 } 1080 Set getFullLocaleNameSet() { 1081 if (fullNameSet == null) { 1082 fullNameSet = createFullLocaleNameSet(prefix); 1083 } 1084 return fullNameSet; 1085 } 1086 } 1087 1088 1093 private static AvailEntry getAvailEntry(String key) { 1094 AvailEntry ae = null; 1095 Map lcache = null; 1096 if (GET_AVAILABLE_CACHE != null) { 1097 lcache = (Map ) GET_AVAILABLE_CACHE.get(); 1098 if (lcache != null) { 1099 ae = (AvailEntry) lcache.get(key); 1100 } 1101 } 1102 1103 if (ae == null) { 1104 ae = new AvailEntry(key); 1105 if (lcache == null) { 1106 lcache = new HashMap (); 1107 lcache.put(key, ae); 1108 GET_AVAILABLE_CACHE = new SoftReference (lcache); 1109 } else { 1110 lcache.put(key, ae); 1111 } 1112 } 1113 1114 return ae; 1115 } 1116 1117 protected static final ICUResourceBundle findResourceWithFallback(String path, 1118 ICUResourceBundle actualBundle, ICUResourceBundle requested) { 1119 ICUResourceBundle sub = null; 1120 if (requested == null) { 1121 requested = actualBundle; 1122 } 1123 while (actualBundle != null) { 1124 StringTokenizer st = new StringTokenizer(path, "/"); 1125 ICUResourceBundle current = actualBundle; 1126 while (st.hasMoreTokens()) { 1127 String subKey = st.nextToken(); 1128 sub = current.handleGet(subKey, requested); 1129 if (sub == null) { 1130 break; 1131 } 1132 current = sub; 1133 } 1134 if (sub != null) { 1135 break; 1137 } 1138 if (actualBundle.resPath.length() != 0) { 1139 path = actualBundle.resPath + "/" + path; 1140 } 1141 actualBundle = (ICUResourceBundle) actualBundle.getParent(); 1143 1144 } 1145 if(sub != null){ 1146 setLoadingStatus(sub, requested.getLocaleID()); 1147 } 1148 return sub; 1149 } 1150 public boolean equals(Object other) { 1151 if (other instanceof ICUResourceBundle) { 1152 ICUResourceBundle o = (ICUResourceBundle) other; 1153 if (getBaseName().equals(o.getBaseName()) 1154 && getLocaleID().equals(o.getLocaleID())) { 1155 return true; 1156 } 1157 } 1158 return false; 1159 } 1160 public static UResourceBundle getBundleInstance(String baseName, String localeID, 1162 ClassLoader root, boolean disableFallback){ 1163 UResourceBundle b = instantiateBundle(baseName, localeID, root, disableFallback); 1164 if(b==null){ 1165 throw new MissingResourceException ("Could not find the bundle "+ baseName+"/"+ localeID+".res","",""); 1166 } 1167 return b; 1168 } 1169 protected synchronized static UResourceBundle instantiateBundle(String baseName, String localeID, 1171 ClassLoader root, boolean disableFallback){ 1172 ULocale defaultLocale = ULocale.getDefault(); 1173 String localeName = localeID; 1174 if(localeName.indexOf('@')>0){ 1175 localeName = ULocale.getBaseName(localeID); 1176 } 1177 String fullName = ICUResourceBundleReader.getFullName(baseName, localeName); 1178 ICUResourceBundle b = (ICUResourceBundle)loadFromCache(root, fullName, defaultLocale); 1179 1180 final String rootLocale = (baseName.indexOf('.')==-1) ? "root" : ""; 1188 final String defaultID = ULocale.getDefault().toString(); 1189 1190 if(localeName.equals("")){ 1191 localeName = rootLocale; 1192 } 1193 if(DEBUG) System.out.println("Creating "+fullName+ " currently b is "+b); 1194 if (b == null) { 1195 b = ICUResourceBundleImpl.createBundle(baseName, localeName, root); 1196 1197 if(DEBUG)System.out.println("The bundle created is: "+b+" and disableFallback="+disableFallback+" and bundle.getNoFallback="+(b!=null && b.getNoFallback())); 1198 if(disableFallback || (b!=null && b.getNoFallback())){ 1199 addToCache(root, fullName, defaultLocale, b); 1200 return b; 1202 } 1203 1204 if(b == null){ 1206 int i = localeName.lastIndexOf('_'); 1207 if (i != -1) { 1208 String temp = localeName.substring(0, i); 1209 b = (ICUResourceBundle)instantiateBundle(baseName, temp, root, disableFallback); 1210 if(b!=null && b.getULocale().equals(temp)){ 1211 b.setLoadingStatus(ICUResourceBundle.FROM_FALLBACK); 1212 } 1213 }else{ 1214 if(defaultID.indexOf(localeName)==-1){ 1215 b = (ICUResourceBundle)instantiateBundle(baseName, defaultID, root, disableFallback); 1216 if(b!=null){ 1217 b.setLoadingStatus(ICUResourceBundle.FROM_DEFAULT); 1218 } 1219 }else if(rootLocale.length()!=0){ 1220 b = ICUResourceBundleImpl.createBundle(baseName, rootLocale, root); 1221 if(b!=null){ 1222 b.setLoadingStatus(ICUResourceBundle.FROM_ROOT); 1223 } 1224 } 1225 } 1226 }else{ 1227 UResourceBundle parent = null; 1228 localeName = b.getLocaleID(); 1229 int i = localeName.lastIndexOf('_'); 1230 1231 addToCache(root, fullName, defaultLocale, b); 1232 1233 if (i != -1) { 1234 parent = instantiateBundle(baseName, localeName.substring(0, i), root, disableFallback); 1235 }else if(!localeName.equals(rootLocale)){ 1236 parent = instantiateBundle(baseName, rootLocale, root, true); 1237 } 1238 1239 if(!b.equals(parent)){ 1240 b.setParent(parent); 1241 } 1242 } 1243 } 1244 return b; 1245 } 1246} 1247 | Popular Tags |