34 import org.xml.sax.SAXException; 35 36 /** 37 * Handles parsing of files in Locale Data Markup Language for SupplementData.xml 38 * and produces a map that uses the keys and values of JRE locale data. 39 */ 40 41 class SupplementDataParseHandler extends AbstractLDMLHandler<Object> { 42 //UNM49 region and composition code used in supplementalData.xml 43 private static final String WORLD = "001"; 44 45 private static final String JAVA_FIRSTDAY = "firstDayOfWeek"; 46 private static final String JAVA_MINDAY = "minimalDaysInFirstWeek"; 47 48 // The weekData is now in supplementalData.xml, 49 // which is not a locale specific file. 50 // Map for JRE is created locale specific way. 51 // When parsing the locale neutral file (supplementalData.xml), 52 // we need to rely on the country code because 53 // the weekData is listed using country code. 54 private final Map<String, Object> firstDayMap; 55 private final Map<String, Object> minDaysMap; 56 57 SupplementDataParseHandler() { 58 firstDayMap = new HashMap<>(); 59 minDaysMap = new HashMap<>(); 60 } 61 62 /** 63 * It returns Map that contains the firstDay and minDays information for 64 * the country. The Map is created in JRE format after obtaining the data 65 * from two Maps, firstDayMap and minDaysMap. 66 * 67 * It returns null when there is no firstDay and minDays for the country 68 * although this should not happen because supplementalData.xml includes 69 * default value for the world ("001") for firstDay and minDays. 70 */ 71 Map<String, Object> getData(String country) { 72 Map<String, Object> values = new HashMap<>(); 73 String countryData = getWeekData(country, JAVA_FIRSTDAY, firstDayMap); 74 if (countryData != null) { 75 values.put(JAVA_FIRSTDAY, countryData); 76 } 77 String minDaysData = getWeekData(country, JAVA_MINDAY, minDaysMap); 78 if (minDaysData != null) { 79 values.put(JAVA_MINDAY, minDaysData); 80 } 81 return values.isEmpty() ? null : values; 82 } 83 84 /** 85 * It returns either firstDay or minDays in the JRE format for the country. 86 * 87 * @param country territory code of the requested data 88 * @param jreDataName JAVA_FIRSTDAY or JAVA_MINDAY 89 * @param dataMap firstDayMap or minDaysMap 90 * @return the value for the given jreDataName, or null if requested value 91 * (firstDay/minDays) is not available although that is highly unlikely 92 * because of the default value for the world (001). 93 */ 94 String getWeekData(String country, final String jreDataName, final Map<String, Object> dataMap) { 95 String countryValue = null; 96 String defaultWorldValue = null; 97 for (String key : dataMap.keySet()) { 98 if (key.contains(country)) { 99 if (jreDataName.equals(JAVA_FIRSTDAY)) { 100 countryValue = DAY_OF_WEEK_MAP.get((String) dataMap.get(key)); 127 /** 128 * JRE requires all the data to be organized by the locale while CLDR 1.4 list 129 * Calendar related data (weekData)in SupplementalData.xml. 130 * startElement stores JRE required data into two Maps, 131 * firstDayMap and minDaysMap. 132 */ 133 @Override 134 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 135 // elements we need to actively ignore 136 switch (qName) { 137 case "firstDay": 138 if (!isIgnored(attributes)) { 139 firstDayMap.put(attributes.getValue("territories"), attributes.getValue("day")); 140 } 141 break; 142 case "minDays": 143 if (!isIgnored(attributes)) { 144 minDaysMap.put(attributes.getValue("territories"), attributes.getValue("count")); 145 } 146 break; 147 default: 148 // treat anything else as a container 149 pushContainer(qName, attributes); 150 break; 151 } 152 } 153 154 } | 34 import org.xml.sax.SAXException; 35 36 /** 37 * Handles parsing of files in Locale Data Markup Language for SupplementData.xml 38 * and produces a map that uses the keys and values of JRE locale data. 39 */ 40 41 class SupplementDataParseHandler extends AbstractLDMLHandler<Object> { 42 //UNM49 region and composition code used in supplementalData.xml 43 private static final String WORLD = "001"; 44 45 private static final String JAVA_FIRSTDAY = "firstDayOfWeek"; 46 private static final String JAVA_MINDAY = "minimalDaysInFirstWeek"; 47 48 // The weekData is now in supplementalData.xml, 49 // which is not a locale specific file. 50 // Map for JRE is created locale specific way. 51 // When parsing the locale neutral file (supplementalData.xml), 52 // we need to rely on the country code because 53 // the weekData is listed using country code. 54 // 55 // weekData are generated per each country 56 private final Map<String, Object> firstDayMap; 57 private final Map<String, Object> minDaysMap; 58 59 // Parent locales. These information will only be 60 // generated towards the base meta info, with the format of 61 // 62 // parentLocale.<parent_locale_id>=<child_locale_id>(" "<child_locale_id>)+ 63 private final Map<String, String> parentLocalesMap; 64 65 SupplementDataParseHandler() { 66 firstDayMap = new HashMap<>(); 67 minDaysMap = new HashMap<>(); 68 parentLocalesMap = new HashMap<>(); 69 } 70 71 /** 72 * It returns Map that contains the firstDay and minDays information for 73 * the country. The Map is created in JRE format after obtaining the data 74 * from two Maps, firstDayMap and minDaysMap. 75 * 76 * It returns null when there is no firstDay and minDays for the country 77 * although this should not happen because supplementalData.xml includes 78 * default value for the world ("001") for firstDay and minDays. 79 */ 80 Map<String, Object> getData(String id) { 81 Map<String, Object> values = new HashMap<>(); 82 if ("root".equals(id)) { 83 parentLocalesMap.keySet().forEach(key -> { 84 values.put(CLDRConverter.PARENT_LOCALE_PREFIX+key, 85 parentLocalesMap.get(key)); 86 }); 87 } else { 88 String countryData = getWeekData(id, JAVA_FIRSTDAY, firstDayMap); 89 if (countryData != null) { 90 values.put(JAVA_FIRSTDAY, countryData); 91 } 92 String minDaysData = getWeekData(id, JAVA_MINDAY, minDaysMap); 93 if (minDaysData != null) { 94 values.put(JAVA_MINDAY, minDaysData); 95 } 96 } 97 return values.isEmpty() ? null : values; 98 } 99 100 /** 101 * It returns either firstDay or minDays in the JRE format for the country. 102 * 103 * @param country territory code of the requested data 104 * @param jreDataName JAVA_FIRSTDAY or JAVA_MINDAY 105 * @param dataMap firstDayMap or minDaysMap 106 * @return the value for the given jreDataName, or null if requested value 107 * (firstDay/minDays) is not available although that is highly unlikely 108 * because of the default value for the world (001). 109 */ 110 String getWeekData(String country, final String jreDataName, final Map<String, Object> dataMap) { 111 String countryValue = null; 112 String defaultWorldValue = null; 113 for (String key : dataMap.keySet()) { 114 if (key.contains(country)) { 115 if (jreDataName.equals(JAVA_FIRSTDAY)) { 116 countryValue = DAY_OF_WEEK_MAP.get((String) dataMap.get(key)); 143 /** 144 * JRE requires all the data to be organized by the locale while CLDR 1.4 list 145 * Calendar related data (weekData)in SupplementalData.xml. 146 * startElement stores JRE required data into two Maps, 147 * firstDayMap and minDaysMap. 148 */ 149 @Override 150 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 151 // elements we need to actively ignore 152 switch (qName) { 153 case "firstDay": 154 if (!isIgnored(attributes)) { 155 firstDayMap.put(attributes.getValue("territories"), attributes.getValue("day")); 156 } 157 break; 158 case "minDays": 159 if (!isIgnored(attributes)) { 160 minDaysMap.put(attributes.getValue("territories"), attributes.getValue("count")); 161 } 162 break; 163 case "parentLocale": 164 if (!isIgnored(attributes)) { 165 parentLocalesMap.put( 166 attributes.getValue("parent").replaceAll("_", "-"), 167 attributes.getValue("locales").replaceAll("_", "-")); 168 } 169 break; 170 default: 171 // treat anything else as a container 172 pushContainer(qName, attributes); 173 break; 174 } 175 } 176 177 } |