1 7 8 package com.ibm.icu.text; 9 10 import java.io.Serializable ; 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.Locale ; 15 import java.util.MissingResourceException ; 16 import java.util.ResourceBundle ; 17 18 import com.ibm.icu.impl.CalendarData; 19 import com.ibm.icu.impl.ICUCache; 20 import com.ibm.icu.impl.ICUResourceBundle; 21 import com.ibm.icu.impl.SimpleCache; 22 import com.ibm.icu.impl.TextTrieMap; 23 import com.ibm.icu.impl.Utility; 24 import com.ibm.icu.impl.ZoneMeta; 25 import com.ibm.icu.util.Calendar; 26 import com.ibm.icu.util.GregorianCalendar; 27 import com.ibm.icu.util.TimeZone; 28 import com.ibm.icu.util.ULocale; 29 import com.ibm.icu.util.UResourceBundle; 30 31 77 public class DateFormatSymbols implements Serializable , Cloneable { 78 79 84 90 public static final int FORMAT = 0; 91 92 97 public static final int STANDALONE = 1; 98 99 104 public static final int DT_CONTEXT_COUNT = 2; 105 106 108 113 public static final int ABBREVIATED = 0; 114 115 120 public static final int WIDE = 1; 121 122 127 public static final int NARROW = 2; 128 129 134 public static final int DT_WIDTH_COUNT = 3; 135 136 145 public DateFormatSymbols() 146 { 147 initializeData(ULocale.getDefault(), ""); } 149 150 159 public DateFormatSymbols(Locale locale) 160 { 161 initializeData(ULocale.forLocale(locale), ""); } 163 164 174 public DateFormatSymbols(ULocale locale) 175 { 176 initializeData(locale, ""); } 178 179 184 String eras[] = null; 185 186 191 String eraNames[] = null; 192 193 198 String narrowEras[] = null; 199 200 206 String months[] = null; 207 208 215 String shortMonths[] = null; 216 217 224 String narrowMonths[] = null; 225 226 232 String standaloneMonths[] = null; 233 234 241 String standaloneShortMonths[] = null; 242 243 250 String standaloneNarrowMonths[] = null; 251 252 259 String weekdays[] = null; 260 261 268 String shortWeekdays[] = null; 269 270 277 String narrowWeekdays[] = null; 278 279 286 String standaloneWeekdays[] = null; 287 288 295 String standaloneShortWeekdays[] = null; 296 297 304 String standaloneNarrowWeekdays[] = null; 305 306 312 String ampms[] = null; 313 314 319 String shortQuarters[] = null; 320 321 326 String quarters[] = null; 327 328 333 String standaloneShortQuarters[] = null; 334 335 340 String standaloneQuarters[] = null; 341 342 372 private String zoneStrings[][] = null; 373 374 378 static final String patternChars = "GyMdkHmsSEDFwWahKzYeugAZvcLQq"; 379 380 390 String localPatternChars = null; 391 392 393 private static final long serialVersionUID = -5987973545549424702L; 394 395 400 public String [] getEras() { 401 return duplicate(eras); 402 } 403 404 409 public void setEras(String [] newEras) { 410 eras = duplicate(newEras); 411 } 412 413 419 public String [] getEraNames() { 420 return duplicate(eraNames); 421 } 422 423 429 public void setEraNames(String [] newEraNames) { 430 eraNames = duplicate(newEraNames); 431 } 432 433 438 public String [] getMonths() { 439 return duplicate(months); 440 } 441 442 451 public String [] getMonths(int context, int width) { 452 String [] returnValue = null; 453 switch (context) { 454 case FORMAT : 455 switch(width) { 456 case WIDE : 457 returnValue = months; 458 break; 459 case ABBREVIATED : 460 returnValue = shortMonths; 461 break; 462 case NARROW : 463 returnValue = narrowMonths; 464 break; 465 } 466 break; 467 case STANDALONE : 468 switch(width) { 469 case WIDE : 470 returnValue = standaloneMonths; 471 break; 472 case ABBREVIATED : 473 returnValue = standaloneShortMonths; 474 break; 475 case NARROW : 476 returnValue = standaloneNarrowMonths; 477 break; 478 } 479 break; 480 } 481 return duplicate(returnValue); 482 } 483 484 489 public void setMonths(String [] newMonths) { 490 months = duplicate(newMonths); 491 } 492 493 502 public void setMonths(String [] newMonths, int context, int width) { 503 switch (context) { 504 case FORMAT : 505 switch(width) { 506 case WIDE : 507 months = duplicate(newMonths); 508 break; 509 case ABBREVIATED : 510 shortMonths = duplicate(newMonths); 511 break; 512 case NARROW : 513 narrowMonths = duplicate(newMonths); 514 break; 515 } 516 break; 517 case STANDALONE : 518 switch(width) { 519 case WIDE : 520 standaloneMonths = duplicate(newMonths); 521 break; 522 case ABBREVIATED : 523 standaloneShortMonths = duplicate(newMonths); 524 break; 525 case NARROW : 526 standaloneNarrowMonths = duplicate(newMonths); 527 break; 528 } 529 break; 530 } 531 } 532 533 538 public String [] getShortMonths() { 539 return duplicate(shortMonths); 540 } 541 542 547 public void setShortMonths(String [] newShortMonths) { 548 shortMonths = duplicate(newShortMonths); 549 } 550 551 557 public String [] getWeekdays() { 558 return duplicate(weekdays); 559 } 560 561 571 public String [] getWeekdays(int context, int width) { 572 String [] returnValue = null; 573 switch (context) { 574 case FORMAT : 575 switch(width) { 576 case WIDE : 577 returnValue = weekdays; 578 break; 579 case ABBREVIATED : 580 returnValue = shortWeekdays; 581 break; 582 case NARROW : 583 returnValue = narrowWeekdays; 584 break; 585 } 586 break; 587 case STANDALONE : 588 switch(width) { 589 case WIDE : 590 returnValue = standaloneWeekdays; 591 break; 592 case ABBREVIATED : 593 returnValue = standaloneShortWeekdays; 594 break; 595 case NARROW : 596 returnValue = standaloneNarrowWeekdays; 597 break; 598 } 599 break; 600 } 601 return duplicate(returnValue); 602 } 603 604 613 public void setWeekdays(String [] newWeekdays, int context, int width) { 614 switch (context) { 615 case FORMAT : 616 switch(width) { 617 case WIDE : 618 weekdays = duplicate(newWeekdays); 619 break; 620 case ABBREVIATED : 621 shortWeekdays = duplicate(newWeekdays); 622 break; 623 case NARROW : 624 narrowWeekdays = duplicate(newWeekdays); 625 break; 626 } 627 break; 628 case STANDALONE : 629 switch(width) { 630 case WIDE : 631 standaloneWeekdays = duplicate(newWeekdays); 632 break; 633 case ABBREVIATED : 634 standaloneShortWeekdays = duplicate(newWeekdays); 635 break; 636 case NARROW : 637 standaloneNarrowWeekdays = duplicate(newWeekdays); 638 break; 639 } 640 break; 641 } 642 } 643 644 651 public void setWeekdays(String [] newWeekdays) { 652 weekdays = duplicate(newWeekdays); 653 } 654 655 661 public String [] getShortWeekdays() { 662 return duplicate(shortWeekdays); 663 } 664 665 672 public void setShortWeekdays(String [] newShortWeekdays) { 673 shortWeekdays = duplicate(newShortWeekdays); 674 } 675 684 public String [] getQuarters(int context, int width) { 685 String [] returnValue = null; 686 switch (context) { 687 case FORMAT : 688 switch(width) { 689 case WIDE : 690 returnValue = quarters; 691 break; 692 case ABBREVIATED : 693 returnValue = shortQuarters; 694 break; 695 case NARROW : 696 returnValue = null; 697 break; 698 } 699 break; 700 701 case STANDALONE : 702 switch(width) { 703 case WIDE : 704 returnValue = standaloneQuarters; 705 break; 706 case ABBREVIATED : 707 returnValue = standaloneShortQuarters; 708 break; 709 case NARROW: 710 returnValue = null; 711 break; 712 } 713 break; 714 } 715 return duplicate(returnValue); 716 } 717 718 727 public void setQuarters(String [] newQuarters, int context, int width) { 728 switch (context) { 729 case FORMAT : 730 switch(width) { 731 case WIDE : 732 quarters = duplicate(newQuarters); 733 break; 734 case ABBREVIATED : 735 shortQuarters = duplicate(newQuarters); 736 break; 737 case NARROW : 738 break; 740 } 741 break; 742 case STANDALONE : 743 switch(width) { 744 case WIDE : 745 standaloneQuarters = duplicate(newQuarters); 746 break; 747 case ABBREVIATED : 748 standaloneShortQuarters = duplicate(newQuarters); 749 break; 750 case NARROW : 751 break; 753 } 754 break; 755 } 756 } 757 758 763 public String [] getAmPmStrings() { 764 return duplicate(ampms); 765 } 766 767 772 public void setAmPmStrings(String [] newAmpms) { 773 ampms = duplicate(newAmpms); 774 } 775 776 781 public String [][] getZoneStrings() { 782 String [][] strings = zoneStrings; 783 if(strings == null){ 784 ZoneItemInfo zii = getDefaultZoneItemInfo(); 786 strings = zii.tzStrings; 787 } 788 return duplicate(strings); 789 } 790 791 796 public void setZoneStrings(String [][] newZoneStrings) { 797 zoneStrings = duplicate(newZoneStrings); 798 localZoneItemInfo = null; 800 } 801 802 807 public String getLocalPatternChars() { 808 return new String (localPatternChars); 809 } 810 811 817 public void setLocalPatternChars(String newLocalPatternChars) { 818 localPatternChars = newLocalPatternChars; 819 } 820 821 825 public Object clone() 826 { 827 try { 828 DateFormatSymbols other = (DateFormatSymbols)super.clone(); 829 return other; 830 } catch (CloneNotSupportedException e) { 831 throw new IllegalStateException (); 833 } 835 } 836 837 842 public int hashCode() { 843 int hashcode = 0; 844 hashcode ^= requestedLocale.toString().hashCode(); 845 String [][] tzStrings = zoneStrings; 846 if (tzStrings == null){ 847 ZoneItemInfo zii = getDefaultZoneItemInfo(); 848 tzStrings = zii.tzStrings; 849 } 850 for(int i = 0; i < tzStrings.length; i++) { 851 for (int j = 0; j < tzStrings[i].length; j++) { 852 if (tzStrings[i][j] != null) { 853 hashcode ^= tzStrings[i][j].hashCode(); 854 } 855 } 856 } 857 return hashcode; 858 } 859 860 864 public boolean equals(Object obj) 865 { 866 if (this == obj) return true; 867 if (obj == null || getClass() != obj.getClass()) return false; 868 DateFormatSymbols that = (DateFormatSymbols) obj; 869 return (Utility.arrayEquals(eras, that.eras) 870 && Utility.arrayEquals(eraNames, that.eraNames) 871 && Utility.arrayEquals(months, that.months) 872 && Utility.arrayEquals(shortMonths, that.shortMonths) 873 && Utility.arrayEquals(narrowMonths, that.narrowMonths) 874 && Utility.arrayEquals(standaloneMonths, that.standaloneMonths) 875 && Utility.arrayEquals(standaloneShortMonths, that.standaloneShortMonths) 876 && Utility.arrayEquals(standaloneNarrowMonths, that.standaloneNarrowMonths) 877 && Utility.arrayEquals(weekdays, that.weekdays) 878 && Utility.arrayEquals(shortWeekdays, that.shortWeekdays) 879 && Utility.arrayEquals(narrowWeekdays, that.narrowWeekdays) 880 && Utility.arrayEquals(standaloneWeekdays, that.standaloneWeekdays) 881 && Utility.arrayEquals(standaloneShortWeekdays, that.standaloneShortWeekdays) 882 && Utility.arrayEquals(standaloneNarrowWeekdays, that.standaloneNarrowWeekdays) 883 && Utility.arrayEquals(ampms, that.ampms) 884 && arrayOfArrayEquals(zoneStrings, that.zoneStrings) 885 && requestedLocale.getDisplayName().equals(that.requestedLocale.getDisplayName()) 889 && Utility.arrayEquals(localPatternChars, 890 that.localPatternChars)); 891 } 892 893 895 898 static final int millisPerHour = 60*60*1000; 899 900 private static ICUCache DFSCACHE = new SimpleCache(); 902 903 909 protected void initializeData(ULocale desiredLocale, String type) 910 { 911 String key = desiredLocale.toString() + "+" + type; 912 DateFormatSymbols dfs = (DateFormatSymbols)DFSCACHE.get(key); 913 if (dfs == null) { 914 CalendarData calData = new CalendarData(desiredLocale, type); 916 initializeData(desiredLocale, calData); 917 dfs = (DateFormatSymbols)this.clone(); 918 DFSCACHE.put(key, dfs); 919 } else { 920 initializeData(dfs); 921 } 922 } 923 924 929 void initializeData(DateFormatSymbols dfs) { 930 this.eras = dfs.eras; 931 this.eraNames = dfs.eraNames; 932 this.narrowEras = dfs.narrowEras; 933 this.months = dfs.months; 934 this.shortMonths = dfs.shortMonths; 935 this.narrowMonths = dfs.narrowMonths; 936 this.standaloneMonths = dfs.standaloneMonths; 937 this.standaloneShortMonths = dfs.standaloneShortMonths; 938 this.standaloneNarrowMonths = dfs.standaloneNarrowMonths; 939 this.weekdays = dfs.weekdays; 940 this.shortWeekdays = dfs.shortWeekdays; 941 this.narrowWeekdays = dfs.narrowWeekdays; 942 this.standaloneWeekdays = dfs.standaloneWeekdays; 943 this.standaloneShortWeekdays = dfs.standaloneShortWeekdays; 944 this.standaloneNarrowWeekdays = dfs.standaloneNarrowWeekdays; 945 this.ampms = dfs.ampms; 946 this.shortQuarters = dfs.shortQuarters; 947 this.quarters = dfs.quarters; 948 this.standaloneShortQuarters = dfs.standaloneShortQuarters; 949 this.standaloneQuarters = dfs.standaloneQuarters; 950 951 this.zoneStrings = dfs.zoneStrings; this.localPatternChars = dfs.localPatternChars; 953 954 this.actualLocale = dfs.actualLocale; 955 this.validLocale = dfs.validLocale; 956 this.requestedLocale = dfs.requestedLocale; 957 } 958 959 965 protected void initializeData(ULocale desiredLocale, CalendarData calData) 966 { 967 968 eras = calData.getEras("abbreviated"); 972 973 try { 974 eraNames = calData.getEras("wide"); 975 } 976 catch (MissingResourceException e) { 977 eraNames = calData.getEras("abbreviated"); 978 } 979 980 try { 983 narrowEras = calData.getEras("narrow"); 984 } catch (MissingResourceException e) { 985 narrowEras = calData.getEras("abbreviated"); 986 } 987 988 months = calData.getStringArray("monthNames", "wide"); 989 shortMonths = calData.getStringArray("monthNames", "abbreviated"); 990 991 try { 992 narrowMonths = calData.getStringArray("monthNames", "narrow"); 993 } 994 catch (MissingResourceException e) { 995 try { 996 narrowMonths = calData.getStringArray("monthNames", "stand-alone", "narrow"); 997 } 998 catch (MissingResourceException e1) { 999 narrowMonths = calData.getStringArray("monthNames", "abbreviated"); 1000 } 1001 } 1002 1003 try { 1004 standaloneMonths = calData.getStringArray("monthNames", "stand-alone", "wide"); 1005 } 1006 catch (MissingResourceException e) { 1007 standaloneMonths = calData.getStringArray("monthNames", "format", "wide"); 1008 } 1009 1010 try { 1011 standaloneShortMonths = calData.getStringArray("monthNames", "stand-alone", "abbreviated"); 1012 } 1013 catch (MissingResourceException e) { 1014 standaloneShortMonths = calData.getStringArray("monthNames", "format", "abbreviated"); 1015 } 1016 1017 try { 1018 standaloneNarrowMonths = calData.getStringArray("monthNames", "stand-alone", "narrow"); 1019 } 1020 catch (MissingResourceException e) { 1021 try { 1022 standaloneNarrowMonths = calData.getStringArray("monthNames", "format", "narrow"); 1023 } 1024 catch (MissingResourceException e1) { 1025 standaloneNarrowMonths = calData.getStringArray("monthNames", "format", "abbreviated"); 1026 } 1027 } 1028 1029 String [] lWeekdays = calData.getStringArray("dayNames", "wide"); 1030 weekdays = new String [8]; 1031 weekdays[0] = ""; System.arraycopy(lWeekdays, 0, weekdays, 1, lWeekdays.length); 1033 1034 String [] sWeekdays = calData.getStringArray("dayNames", "abbreviated"); 1035 shortWeekdays = new String [8]; 1036 shortWeekdays[0] = ""; System.arraycopy(sWeekdays, 0, shortWeekdays, 1, sWeekdays.length); 1038 1039 String [] nWeekdays = null; 1040 try { 1041 nWeekdays = calData.getStringArray("dayNames", "narrow"); 1042 } 1043 catch (MissingResourceException e) { 1044 try { 1045 nWeekdays = calData.getStringArray("dayNames", "stand-alone", "narrow"); 1046 } 1047 catch (MissingResourceException e1) { 1048 nWeekdays = calData.getStringArray("dayNames", "abbreviated"); 1049 } 1050 } 1051 narrowWeekdays = new String [8]; 1052 narrowWeekdays[0] = ""; System.arraycopy(nWeekdays, 0, narrowWeekdays, 1, nWeekdays.length); 1054 1055 String [] saWeekdays = null; 1056 try { 1057 saWeekdays = calData.getStringArray("dayNames", "stand-alone", "wide"); 1058 } 1059 catch (MissingResourceException e) { 1060 saWeekdays = calData.getStringArray("dayNames", "format", "wide"); 1061 } 1062 standaloneWeekdays = new String [8]; 1063 standaloneWeekdays[0] = ""; System.arraycopy(saWeekdays, 0, standaloneWeekdays, 1, saWeekdays.length); 1065 1066 String [] ssWeekdays = null; 1067 try { 1068 ssWeekdays = calData.getStringArray("dayNames", "stand-alone", "abbreviated"); 1069 } 1070 catch (MissingResourceException e) { 1071 ssWeekdays = calData.getStringArray("dayNames", "format", "abbreviated"); 1072 } 1073 standaloneShortWeekdays = new String [8]; 1074 standaloneShortWeekdays[0] = ""; System.arraycopy(ssWeekdays, 0, standaloneShortWeekdays, 1, ssWeekdays.length); 1076 1077 String [] snWeekdays = null; 1078 try { 1079 snWeekdays = calData.getStringArray("dayNames", "stand-alone", "narrow"); 1080 } 1081 catch (MissingResourceException e) { 1082 try { 1083 snWeekdays = calData.getStringArray("dayNames", "format", "narrow"); 1084 } 1085 catch (MissingResourceException e1) { 1086 snWeekdays = calData.getStringArray("dayNames", "format", "abbreviated"); 1087 } 1088 } 1089 standaloneNarrowWeekdays = new String [8]; 1090 standaloneNarrowWeekdays[0] = ""; System.arraycopy(snWeekdays, 0, standaloneNarrowWeekdays, 1, snWeekdays.length); 1092 1093 ampms = calData.getStringArray("AmPmMarkers"); 1094 1095 quarters = calData.getStringArray("quarters", "wide"); 1096 shortQuarters = calData.getStringArray("quarters", "abbreviated"); 1097 1098 try { 1099 standaloneQuarters = calData.getStringArray("quarters", "stand-alone", "wide"); 1100 } 1101 catch (MissingResourceException e) { 1102 standaloneQuarters = calData.getStringArray("quarters", "format", "wide"); 1103 } 1104 1105 try { 1106 standaloneShortQuarters = calData.getStringArray("quarters", "stand-alone", "abbreviated"); 1107 } 1108 catch (MissingResourceException e) { 1109 standaloneShortQuarters = calData.getStringArray("quarters", "format", "abbreviated"); 1110 } 1111 1112 1128 requestedLocale = desiredLocale; 1129 1130 ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, desiredLocale); 1131 localPatternChars = rb.getString("localPatternChars"); 1132 1133 ULocale uloc = rb.getULocale(); 1135 setLocale(uloc, uloc); 1136 } 1137 1138 private static final boolean arrayOfArrayEquals(Object [][] aa1, Object [][]aa2) { 1139 if (aa1 == aa2) { return true; 1141 } 1142 if (aa1 == null || aa2 == null) { return false; 1144 } 1145 if (aa1.length != aa2.length) { 1146 return false; 1147 } 1148 boolean equal = true; 1149 for (int i = 0; i < aa1.length; i++) { 1150 equal = Utility.arrayEquals(aa1[i], aa2[i]); 1151 if (!equal) { 1152 break; 1153 } 1154 } 1155 return equal; 1156 } 1157 1158 1165 String getZoneString(String zid, int type) { 1166 String zoneString = getZoneString(getLocalZoneItemInfo(), zid, type); 1168 if (zoneString == null) { 1169 zoneString = getZoneString(getDefaultZoneItemInfo(), zid, type); 1171 } 1172 return zoneString; 1173 } 1174 1175 1178 private String getZoneString(ZoneItemInfo zinfo, String zid, int type) { 1179 if (zinfo == null) { 1180 return null; 1181 } 1182 String [] names = (String [])zinfo.tzidMap.get(zid); 1183 if (names != null) { 1184 int index = -1; 1186 switch (type) { 1187 case TIMEZONE_LONG_STANDARD: 1188 index = 1; 1189 break; 1190 case TIMEZONE_SHORT_STANDARD: 1191 index = 2; 1192 break; 1193 case TIMEZONE_LONG_DAYLIGHT: 1194 index = 3; 1195 break; 1196 case TIMEZONE_SHORT_DAYLIGHT: 1197 index = 4; 1198 break; 1199 case TIMEZONE_EXEMPLAR_CITY: 1200 if (names.length == 6 || names.length == 8) { 1201 index = 5; 1202 } 1203 break; 1204 case TIMEZONE_LONG_GENERIC: 1205 if (names.length == 8) { 1206 index = 6; 1207 } else { 1208 index = 5; 1209 } 1210 break; 1211 case TIMEZONE_SHORT_GENERIC: 1212 if (names.length == 8) { 1213 index = 7; 1214 } else { 1215 index = 6; 1216 } 1217 } 1218 if (index < names.length) { 1219 return names[index]; 1220 } 1221 } 1222 return null; 1223 } 1224 1225 class ZoneItem{ 1226 String value; 1227 int type; 1228 String zid; 1229 } 1230 1231 1240 ZoneItem findZoneIDTypeValue(String text, int start){ 1241 ZoneItem item = null; 1242 int textLength = text.length() - start; 1243 if (lastZoneItem != null && textLength == lastZoneItem.value.length()) { 1244 if (text.regionMatches(true, start, lastZoneItem.value, 0, textLength)) { 1245 item = new ZoneItem(); 1246 item.type = lastZoneItem.type; 1247 item.value = lastZoneItem.value; 1248 item.zid = lastZoneItem.zid; 1249 return item; 1250 } 1251 } 1252 1253 ZoneItemInfo zinfo = getLocalZoneItemInfo(); 1254 if (zinfo != null) { 1255 item = (ZoneItem)zinfo.tzStringMap.get(text, start); 1257 } 1258 1259 zinfo = getDefaultZoneItemInfo(); 1261 ZoneItem itemForLocale = (ZoneItem)zinfo.tzStringMap.get(text, start); 1262 if (itemForLocale != null) { 1263 if (item == null || itemForLocale.value.length() > item.value.length()) { 1265 item = itemForLocale; 1266 } 1267 } 1268 1269 if (item != null && textLength == item.value.length()) { 1270 lastZoneItem = new ZoneItem(); 1274 lastZoneItem.type = item.type; 1275 lastZoneItem.value = item.value; 1276 lastZoneItem.zid = item.zid; 1277 } 1278 return item; 1279 } 1280 1281 1284 private class ZoneItemInfo { 1285 String [][] tzStrings; 1286 HashMap tzidMap; 1287 TextTrieMap tzStringMap; 1288 } 1289 1290 1293 private static ICUCache zoneItemInfoCache = new SimpleCache(); 1294 1295 1298 private transient ZoneItemInfo localZoneItemInfo; 1299 1300 1303 private transient ZoneItem lastZoneItem; 1304 1305 1309 private ZoneItemInfo getDefaultZoneItemInfo() { 1310 ZoneItemInfo zii = (ZoneItemInfo)zoneItemInfoCache.get(requestedLocale); 1311 if (zii != null) { 1312 return zii; 1313 } 1314 zii = getZoneItemInfo(getDefaultZoneStrings(requestedLocale)); 1315 String [] zoneIDs = TimeZone.getAvailableIDs(); 1317 for (int i = 0; i < zoneIDs.length; i++) { 1318 Object o = zii.tzidMap.get(zoneIDs[i]); 1319 if (o != null) { 1320 continue; 1322 } 1323 String value = ZoneMeta.displayFallback(zoneIDs[i], null, requestedLocale); 1324 if (value != null) { 1325 String [] strings = new String [8]; 1326 strings[5] = value; 1327 zii.tzidMap.put(zoneIDs[i], strings); 1328 1329 ZoneItem item = new ZoneItem(); 1330 item.zid = zoneIDs[i]; 1331 item.value = value; 1332 item.type = TIMEZONE_EXEMPLAR_CITY; 1333 zii.tzStringMap.put(item.value, item); 1334 } 1335 } 1336 zoneItemInfoCache.put(requestedLocale, zii); 1337 return zii; 1338 } 1339 1340 1343 private static String [][] getDefaultZoneStrings(ULocale locale) { 1344 ArrayList tmpList = new ArrayList (); 1345 HashSet tmpSet = new HashSet (); 1346 for (ULocale tempLocale = locale; tempLocale != null; tempLocale = tempLocale.getFallback()) { 1347 ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, tempLocale); 1348 ICUResourceBundle zoneStringsBundle = bundle.getWithFallback("zoneStrings"); 1349 for(int i = 0; i < zoneStringsBundle.getSize(); i++){ 1350 ICUResourceBundle zoneTable = zoneStringsBundle.get(i); 1351 String key = Utility.replaceAll(zoneTable.getKey(), ":", "/"); 1352 if(key.length() == 0|| zoneTable.getType() != ICUResourceBundle.TABLE){ 1354 continue; 1355 } 1356 if (tmpSet.contains(key)) { 1357 continue; 1359 } 1360 String [] strings = new String [8]; 1361 strings[0] = key; 1362 try { 1363 strings[1] = zoneTable.getStringWithFallback(LONG_STANDARD); 1364 } catch (MissingResourceException ex) { 1365 } 1367 try { 1368 strings[2] = zoneTable.getStringWithFallback(SHORT_STANDARD); 1369 } catch (MissingResourceException ex) { 1370 } 1372 try { 1373 strings[3] = zoneTable.getStringWithFallback(LONG_DAYLIGHT); 1374 } catch (MissingResourceException ex) { 1375 } 1377 try { 1378 strings[4] = zoneTable.getStringWithFallback(SHORT_DAYLIGHT); 1379 } catch (MissingResourceException ex) { 1380 } 1382 try { 1383 String city = zoneTable.getStringWithFallback(EXEMPLAR_CITY); 1384 strings[5] = ZoneMeta.displayFallback(key, city, tempLocale); 1385 } catch (MissingResourceException ex) { 1386 } 1388 try { 1389 strings[6] = zoneTable.getStringWithFallback(LONG_GENERIC); 1390 } catch (MissingResourceException ex) { 1391 } 1393 try { 1394 strings[7] = zoneTable.getStringWithFallback(SHORT_GENERIC); 1395 } catch (MissingResourceException ex) { 1396 } 1398 tmpList.add(strings); 1399 } 1400 } 1401 String [][] array = new String [tmpList.size()][8]; 1402 tmpList.toArray(array); 1403 return array; 1404 } 1405 1406 1409 private ZoneItemInfo getLocalZoneItemInfo() { 1410 if (localZoneItemInfo == null && zoneStrings != null) { 1411 localZoneItemInfo = getZoneItemInfo(zoneStrings); 1412 } 1413 return localZoneItemInfo; 1414 } 1415 1416 1420 private ZoneItemInfo getZoneItemInfo(String [][] strings) { 1421 ZoneItemInfo zii = new ZoneItemInfo(); 1422 zii.tzStrings = strings; 1423 zii.tzidMap = new HashMap (); 1424 zii.tzStringMap = new TextTrieMap(true); 1425 for (int i = 0; i < strings.length; i++) { 1426 String zid = strings[i][0]; 1427 if (zid != null && zid.length() > 0) { 1428 zii.tzidMap.put(zid, strings[i]); 1429 int nameCount = strings[i].length < 8 ? strings[i].length : 8; 1430 for (int j = 1; j < nameCount; j++) { 1431 if (strings[i][j] != null) { 1432 int type = -1; 1434 switch (j) { 1435 case 1: 1436 type = TIMEZONE_LONG_STANDARD; 1437 break; 1438 case 2: 1439 type = TIMEZONE_SHORT_STANDARD; 1440 break; 1441 case 3: 1442 type = TIMEZONE_LONG_DAYLIGHT; 1443 break; 1444 case 4: 1445 type = TIMEZONE_SHORT_DAYLIGHT; 1446 break; 1447 case 5: 1448 if (nameCount == 6 || nameCount == 8) { 1449 type = TIMEZONE_EXEMPLAR_CITY; 1450 } else { 1451 type = TIMEZONE_LONG_GENERIC; 1452 } 1453 break; 1454 case 6: 1455 if (nameCount == 8) { 1456 type = TIMEZONE_LONG_GENERIC; 1457 } else { 1458 type = TIMEZONE_SHORT_GENERIC; 1459 } 1460 break; 1461 case 7: 1462 type = TIMEZONE_SHORT_GENERIC; 1463 break; 1464 default: 1465 continue; 1467 } 1468 ZoneItem item = new ZoneItem(); 1469 item.zid = zid; 1470 item.value = strings[i][j]; 1471 item.type = type; 1472 zii.tzStringMap.put(strings[i][j], item); 1473 } 1474 } 1475 } 1476 } 1477 return zii; 1478 } 1479 1480 1483 private ULocale requestedLocale; 1484 1485 1490 private static final String SHORT_GENERIC = "sg", 1491 SHORT_STANDARD = "ss", 1492 SHORT_DAYLIGHT = "sd", 1493 LONG_GENERIC = "lg", 1494 LONG_STANDARD = "ls", 1495 LONG_DAYLIGHT = "ld", 1496 EXEMPLAR_CITY = "ec"; 1497 1502 static final int TIMEZONE_SHORT_GENERIC = 0, 1503 TIMEZONE_SHORT_STANDARD = 1, 1504 TIMEZONE_SHORT_DAYLIGHT = 2, 1505 TIMEZONE_LONG_GENERIC = 3, 1506 TIMEZONE_LONG_STANDARD = 4, 1507 TIMEZONE_LONG_DAYLIGHT = 5, 1508 TIMEZONE_EXEMPLAR_CITY = 6, 1509 TIMEZONE_COUNT = 7; 1510 1511 1521 1541 1542 1545 1552 1553 1558 private final String [] duplicate(String [] srcArray) 1559 { 1560 return (String [])srcArray.clone(); 1561 } 1562 1563 private final String [][] duplicate(String [][] srcArray) 1564 { 1565 String [][] aCopy = new String [srcArray.length][]; 1566 for (int i = 0; i < srcArray.length; ++i) 1567 aCopy[i] = duplicate(srcArray[i]); 1568 return aCopy; 1569 } 1570 1571 1585 1586 1588 1648 public DateFormatSymbols(Calendar cal, Locale locale) { 1649 initializeData(ULocale.forLocale(locale), cal.getType()); 1650 } 1651 1652 1713 public DateFormatSymbols(Calendar cal, ULocale locale) { 1714 initializeData(locale, cal.getType()); 1715 } 1716 1717 1723 public DateFormatSymbols(Class calendarClass, Locale locale) { 1724 this(calendarClass, ULocale.forLocale(locale)); 1725 } 1726 1727 1734 public DateFormatSymbols(Class calendarClass, ULocale locale) { 1735 String fullName = calendarClass.getName(); 1736 int lastDot = fullName.lastIndexOf('.'); 1737 String className = fullName.substring(lastDot+1); 1738 String calType = Utility.replaceAll(className, "Calendar", "").toLowerCase(); 1739 1740 initializeData(locale, calType); 1741 } 1742 1743 1750 public DateFormatSymbols(ResourceBundle bundle, Locale locale) { 1751 this(bundle, ULocale.forLocale(locale)); 1752 } 1753 1754 1762 public DateFormatSymbols(ResourceBundle bundle, ULocale locale) { 1763 initializeData(locale, 1764 new CalendarData((ICUResourceBundle)bundle, null)); 1765 } 1766 1767 1778 static public ResourceBundle getDateFormatBundle(Class calendarClass, Locale locale) 1779 throws MissingResourceException { 1780 return getDateFormatBundle(calendarClass, ULocale.forLocale(locale)); 1781 } 1782 1783 1795 static public ResourceBundle getDateFormatBundle(Class calendarClass, ULocale locale) 1796 throws MissingResourceException { 1797 1798 String fullName = calendarClass.getName(); 1801 int lastDot = fullName.lastIndexOf('.'); 1802 String className = fullName.substring(lastDot+1); 1803 1804 String bundleName = className + "Symbols"; 1805 1806 UResourceBundle result = null; 1807 try { 1808 result = UResourceBundle.getBundleInstance(bundleName, locale); 1809 } 1810 catch (MissingResourceException e) { 1811 if (!(GregorianCalendar.class.isAssignableFrom(calendarClass))) { 1815 throw e; 1818 } 1819 } 1821 return result; 1822 } 1823 1824 1830 public static ResourceBundle getDateFormatBundle(Calendar cal, Locale locale) 1831 throws MissingResourceException { 1832 return getDateFormatBundle(cal.getClass(), ULocale.forLocale(locale)); 1833 } 1834 1835 1842 public static ResourceBundle getDateFormatBundle(Calendar cal, ULocale locale) 1843 throws MissingResourceException { 1844 return getDateFormatBundle(cal.getClass(), locale); 1845 } 1846 1847 1849 1873 public final ULocale getLocale(ULocale.Type type) { 1874 return type == ULocale.ACTUAL_LOCALE ? 1875 this.actualLocale : this.validLocale; 1876 } 1877 1878 1896 final void setLocale(ULocale valid, ULocale actual) { 1897 if ((valid == null) != (actual == null)) { 1899 throw new IllegalArgumentException (); 1901 } 1903 this.validLocale = valid; 1906 this.actualLocale = actual; 1907 } 1908 1909 1915 private ULocale validLocale; 1916 1917 1924 private ULocale actualLocale; 1925 1926 } 1928 | Popular Tags |