1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package build.tools.cldrconverter; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.util.HashMap; 31 import java.util.Map; 32 import org.xml.sax.Attributes; 33 import org.xml.sax.InputSource; 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 SupplementDataParseHandler()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 */ getData(String country)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 */ getWeekData(String country, final String jreDataName, final Map<String, Object> dataMap)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)); 101 } else if (jreDataName.equals(JAVA_MINDAY)) { 102 countryValue = (String) dataMap.get(key); 103 } 104 if (countryValue != null) { 105 return countryValue; 106 } 107 } else if (key.contains(WORLD)) { 108 if (jreDataName.equals(JAVA_FIRSTDAY)) { 109 defaultWorldValue = DAY_OF_WEEK_MAP.get((String) dataMap.get(key)); 110 } else if (jreDataName.equals(JAVA_MINDAY)) { 111 defaultWorldValue = (String) dataMap.get(key); 112 } 113 } 114 } 115 return defaultWorldValue; 116 } 117 118 @Override resolveEntity(String publicID, String systemID)119 public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException { 120 // avoid HTTP traffic to unicode.org 121 if (systemID.startsWith(CLDRConverter.SPPL_LDML_DTD_SYSTEM_ID)) { 122 return new InputSource((new File(CLDRConverter.LOCAL_SPPL_LDML_DTD)).toURI().toString()); 123 } 124 return null; 125 } 126 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 startElement(String uri, String localName, String qName, Attributes attributes)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 } 155