2626
2727import com .ibm .icu .impl .CacheBase ;
2828import com .ibm .icu .impl .CalendarUtil ;
29+ import com .ibm .icu .impl .EraRules ;
2930import com .ibm .icu .impl .ICUData ;
3031import com .ibm .icu .impl .ICUResourceBundle ;
3132import com .ibm .icu .impl .SoftCache ;
@@ -1806,37 +1807,8 @@ protected void processResource(String path, UResource.Key key, UResource.Value v
18061807 String [] dataArray = value .getStringArray ();
18071808 arrays .put (currentPath , dataArray );
18081809 } else if (value .getType () == ICUResourceBundle .TABLE ) {
1809- // We might have an eras table that is replacing an eras leaf array
1810- if (currentPath .startsWith ("eras" )) {
1811- // path is one of eras/wide, eras/abbreviated, eras/narrow
1812- UResource .Table rDataTable = value .getTable ();
1813- int dataTableSize = rDataTable .getSize ();
1814- ArrayList <String > dataList = new ArrayList <>(dataTableSize );
1815- // Expand the ArrayList as necessary to have index from 0 up to the max
1816- // eraCode, and fill in the slots for the eras defined in the resource data
1817- // (other slots get nulls).
1818- for (int dataTableIndex = 0 ; dataTableIndex < dataTableSize ; dataTableIndex ++) {
1819- rDataTable .getKeyAndValue (dataTableIndex , key , value );
1820- int listIndex = Integer .parseInt (key .toString ());
1821- if (listIndex + 1 > dataList .size ()) {
1822- dataList .ensureCapacity (listIndex + 1 ); // needed only to minimize expansions
1823- // Fill in empty strings for all added slots
1824- while (dataList .size () < listIndex + 1 ) {
1825- dataList .add ("" );
1826- }
1827- }
1828- // Now set the eraName that we just read
1829- String eraName = (value .getType () == ICUResourceBundle .STRING ) ? value .getString () : "" ;
1830- dataList .set (listIndex , eraName );
1831- }
1832- // Now convert to array
1833- String [] dataArray = dataList .toArray (new String [dataList .size ()]);
1834- // Save the array
1835- arrays .put (currentPath , dataArray );
1836- } else {
1837- // We are not on a leaf, recursively process the subtable.
1838- processResource (currentPath , key , value );
1839- }
1810+ // We are not on a leaf, recursively process the subtable.
1811+ processResource (currentPath , key , value );
18401812 }
18411813 }
18421814 }
@@ -1898,6 +1870,46 @@ private DateFormatSymbols(ULocale desiredLocale, ICUResourceBundle b, String cal
18981870 initializeData (desiredLocale , b , calendarType );
18991871 }
19001872
1873+ /**
1874+ * Convert era names map from CalendarSink to array, filling in missing values from fallback.
1875+ * @internal
1876+ * @deprecated This API is ICU internal only.
1877+ */
1878+ protected String [] initEras (String erasKey , Map <String , Map <String , String >> maps ,
1879+ ICUResourceBundle calBundle , int maxEra ) {
1880+ Map <String , String > eraNamesTable = maps .get (erasKey );
1881+ if (eraNamesTable == null ) {
1882+ return null ;
1883+ }
1884+ ICUResourceBundle calErasWidthBundle = calBundle .findWithFallback (erasKey );
1885+ String [] eraArray = new String [maxEra + 1 ];
1886+ if (eraArray != null ) {
1887+ for (int eraCode = 0 ; eraCode <= maxEra ; eraCode ++) {
1888+ String eraKey = Integer .toString (eraCode );
1889+ String eraName = eraNamesTable .get (eraKey );
1890+ if (eraName != null ) {
1891+ eraArray [eraCode ] = eraName ;
1892+ } else {
1893+ // For a map, the sink does not seem to fill in parent entries for keys
1894+ // that do not exist in the current bundle, that is why we need to explicitly
1895+ // fill these in. Also true in ICU4C. Also pre-set to empty string in case
1896+ // there is no parent entry.
1897+ eraArray [eraCode ] = "" ;
1898+ if (calErasWidthBundle != null ) {
1899+ ICUResourceBundle calErasWidthKeyBundle = calErasWidthBundle .findWithFallback (eraKey );
1900+ if (calErasWidthKeyBundle != null ) {
1901+ eraName = calErasWidthKeyBundle .getString ();
1902+ if (eraName != null ) {
1903+ eraArray [eraCode ] = eraName ;
1904+ }
1905+ }
1906+ }
1907+ }
1908+ }
1909+ }
1910+ return eraArray ;
1911+ }
1912+
19011913 /**
19021914 * Initializes format symbols for the locale and calendar type
19031915 * @param desiredLocale The locale whose symbols are desired.
@@ -1916,6 +1928,8 @@ protected void initializeData(ULocale desiredLocale, ICUResourceBundle b, String
19161928 b = (ICUResourceBundle ) UResourceBundle
19171929 .getBundleInstance (ICUData .ICU_BASE_NAME , desiredLocale );
19181930 }
1931+ // Save the calendarType (with fallback) for later use with initEras:
1932+ String calTypeForEras = ((calendarType !=null )? calendarType : "gregorian" );
19191933
19201934 // Iterate over the resource bundle data following the fallbacks through different calendar types
19211935 while (calendarType != null ) {
@@ -1952,9 +1966,23 @@ protected void initializeData(ULocale desiredLocale, ICUResourceBundle b, String
19521966 Map <String , String []> arrays = calendarSink .arrays ;
19531967 Map <String , Map <String , String >> maps = calendarSink .maps ;
19541968
1955- eras = arrays .get ("eras/abbreviated" );
1956- eraNames = arrays .get ("eras/wide" );
1957- narrowEras = arrays .get ("eras/narrow" );
1969+ // Era setup: Get maxEra from EraRules, get the calendar's era bundle:
1970+ EraRules eraRules = null ;
1971+ try {
1972+ eraRules = EraRules .getInstance (calTypeForEras , false );
1973+ } catch (MissingResourceException e ) {
1974+ // call IDs unsupported in supplmental era rules such as
1975+ // "iso8601" or bogus "unknown"; fix for here and for
1976+ // calBundle:
1977+ calTypeForEras = "gregorian" ;
1978+ eraRules = EraRules .getInstance (calTypeForEras , false );
1979+ }
1980+ int maxEra = (eraRules != null )? eraRules .getMaxEraCode () : 0 ;
1981+ ICUResourceBundle calBundle = b .findWithFallback ("calendar/" + calTypeForEras );
1982+
1983+ eras = initEras ("eras/abbreviated" , maps , calBundle , maxEra );
1984+ eraNames = initEras ("eras/wide" , maps , calBundle , maxEra );
1985+ narrowEras = initEras ("eras/narrow" , maps , calBundle , maxEra );
19581986
19591987 months = arrays .get ("monthNames/format/wide" );
19601988 shortMonths = arrays .get ("monthNames/format/abbreviated" );
0 commit comments