Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/cldrconverter/SupplementDataParseHandler.java
32287 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.cldrconverter;2627import java.io.File;28import java.io.IOException;29import java.util.HashMap;30import java.util.Map;31import org.xml.sax.Attributes;32import org.xml.sax.InputSource;33import org.xml.sax.SAXException;3435/**36* Handles parsing of files in Locale Data Markup Language for SupplementData.xml37* and produces a map that uses the keys and values of JRE locale data.38*/3940class SupplementDataParseHandler extends AbstractLDMLHandler<Object> {41//UNM49 region and composition code used in supplementalData.xml42private static final String WORLD = "001";4344private static final String JAVA_FIRSTDAY = "firstDayOfWeek";45private static final String JAVA_MINDAY = "minimalDaysInFirstWeek";4647// The weekData is now in supplementalData.xml,48// which is not a locale specific file.49// Map for JRE is created locale specific way.50// When parsing the locale neutral file (supplementalData.xml),51// we need to rely on the country code because52// the weekData is listed using country code.53private final Map<String, Object> firstDayMap;54private final Map<String, Object> minDaysMap;5556SupplementDataParseHandler() {57firstDayMap = new HashMap<>();58minDaysMap = new HashMap<>();59}6061/**62* It returns Map that contains the firstDay and minDays information for63* the country. The Map is created in JRE format after obtaining the data64* from two Maps, firstDayMap and minDaysMap.65*66* It returns null when there is no firstDay and minDays for the country67* although this should not happen because supplementalData.xml includes68* default value for the world ("001") for firstDay and minDays.69*/70Map<String, Object> getData(String country) {71Map<String, Object> values = new HashMap<>();72String countryData = getWeekData(country, JAVA_FIRSTDAY, firstDayMap);73if (countryData != null) {74values.put(JAVA_FIRSTDAY, countryData);75}76String minDaysData = getWeekData(country, JAVA_MINDAY, minDaysMap);77if (minDaysData != null) {78values.put(JAVA_MINDAY, minDaysData);79}80return values.isEmpty() ? null : values;81}8283/**84* It returns either firstDay or minDays in the JRE format for the country.85*86* @param country territory code of the requested data87* @param jreDataName JAVA_FIRSTDAY or JAVA_MINDAY88* @param dataMap firstDayMap or minDaysMap89* @return the value for the given jreDataName, or null if requested value90* (firstDay/minDays) is not available although that is highly unlikely91* because of the default value for the world (001).92*/93String getWeekData(String country, final String jreDataName, final Map<String, Object> dataMap) {94String countryValue = null;95String defaultWorldValue = null;96for (String key : dataMap.keySet()) {97if (key.contains(country)) {98if (jreDataName.equals(JAVA_FIRSTDAY)) {99countryValue = DAY_OF_WEEK_MAP.get((String) dataMap.get(key));100} else if (jreDataName.equals(JAVA_MINDAY)) {101countryValue = (String) dataMap.get(key);102}103if (countryValue != null) {104return countryValue;105}106} else if (key.contains(WORLD)) {107if (jreDataName.equals(JAVA_FIRSTDAY)) {108defaultWorldValue = DAY_OF_WEEK_MAP.get((String) dataMap.get(key));109} else if (jreDataName.equals(JAVA_MINDAY)) {110defaultWorldValue = (String) dataMap.get(key);111}112}113}114return defaultWorldValue;115}116117@Override118public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException {119// avoid HTTP traffic to unicode.org120if (systemID.startsWith(CLDRConverter.SPPL_LDML_DTD_SYSTEM_ID)) {121return new InputSource((new File(CLDRConverter.LOCAL_SPPL_LDML_DTD)).toURI().toString());122}123return null;124}125126/**127* JRE requires all the data to be organized by the locale while CLDR 1.4 list128* Calendar related data (weekData)in SupplementalData.xml.129* startElement stores JRE required data into two Maps,130* firstDayMap and minDaysMap.131*/132@Override133public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {134// elements we need to actively ignore135switch (qName) {136case "firstDay":137if (!isIgnored(attributes)) {138firstDayMap.put(attributes.getValue("territories"), attributes.getValue("day"));139}140break;141case "minDays":142if (!isIgnored(attributes)) {143minDaysMap.put(attributes.getValue("territories"), attributes.getValue("count"));144}145break;146default:147// treat anything else as a container148pushContainer(qName, attributes);149break;150}151}152153}154155156