Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/cldrconverter/LDMLParseHandler.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.ArrayList;30import java.util.HashMap;31import java.util.List;32import java.util.Locale;33import java.util.Map;34import org.xml.sax.Attributes;35import org.xml.sax.InputSource;36import org.xml.sax.SAXException;3738/**39* Handles parsing of files in Locale Data Markup Language and produces a map40* that uses the keys and values of JRE locale data.41*/42class LDMLParseHandler extends AbstractLDMLHandler<Object> {43private String defaultNumberingSystem;44private String currentNumberingSystem = "";45private CalendarType currentCalendarType;46private String zoneNameStyle; // "long" or "short" for time zone names47private String zonePrefix;48private final String id;4950LDMLParseHandler(String id) {51this.id = id;52}5354@Override55public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException {56// avoid HTTP traffic to unicode.org57if (systemID.startsWith(CLDRConverter.LDML_DTD_SYSTEM_ID)) {58return new InputSource((new File(CLDRConverter.LOCAL_LDML_DTD)).toURI().toString());59}60return null;61}6263@Override64public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {65switch (qName) {66//67// Generic information68//69case "identity":70// ignore this element - it has language and territory elements that aren't locale data71pushIgnoredContainer(qName);72break;73case "type":74if ("calendar".equals(attributes.getValue("key"))) {75pushStringEntry(qName, attributes, CLDRConverter.CALENDAR_NAME_PREFIX + attributes.getValue("type"));76} else {77pushIgnoredContainer(qName);78}79break;80case "language":81// for LocaleNames82// copy string83pushStringEntry(qName, attributes, CLDRConverter.LOCALE_NAME_PREFIX + attributes.getValue("type"));84break;85case "script":86// for LocaleNames87// copy string88pushStringEntry(qName, attributes, CLDRConverter.LOCALE_NAME_PREFIX + attributes.getValue("type"));89break;90case "territory":91// for LocaleNames92// copy string93pushStringEntry(qName, attributes, CLDRConverter.LOCALE_NAME_PREFIX + attributes.getValue("type"));94break;9596//97// Currency information98//99case "currency":100// for CurrencyNames101// stash away "type" value for nested <symbol>102pushKeyContainer(qName, attributes, attributes.getValue("type"));103break;104case "symbol":105// for CurrencyNames106// need to get the key from the containing <currency> element107pushStringEntry(qName, attributes, CLDRConverter.CURRENCY_SYMBOL_PREFIX108+ getContainerKey());109break;110111// Calendar or currency112case "displayName":113{114if (currentCalendarType != null) {115pushStringEntry(qName, attributes,116currentCalendarType.keyElementName() + "field." + getContainerKey());117} else {118// for CurrencyNames119// need to get the key from the containing <currency> element120// ignore if is has "count" attribute121String containerKey = getContainerKey();122if (containerKey != null && attributes.getValue("count") == null) {123pushStringEntry(qName, attributes,124CLDRConverter.CURRENCY_NAME_PREFIX125+ containerKey.toLowerCase(Locale.ROOT),126attributes.getValue("type"));127} else {128pushIgnoredContainer(qName);129}130}131}132break;133134//135// Calendar information136//137case "calendar":138{139// mostly for FormatData (CalendarData items firstDay and minDays are also nested)140// use only if it's supported by java.util.Calendar.141String calendarName = attributes.getValue("type");142currentCalendarType = CalendarType.forName(calendarName);143if (currentCalendarType != null) {144pushContainer(qName, attributes);145} else {146pushIgnoredContainer(qName);147}148}149break;150case "fields":151if (currentCalendarType != null) {152pushContainer(qName, attributes);153} else {154pushIgnoredContainer(qName);155}156break;157case "field":158{159String type = attributes.getValue("type");160switch (type) {161case "era":162case "year":163case "month":164case "week":165case "weekday":166case "dayperiod":167case "hour":168case "minute":169case "second":170case "zone":171pushKeyContainer(qName, attributes, type);172break;173default:174pushIgnoredContainer(qName);175break;176}177}178break;179case "monthContext":180{181// for FormatData182// need to keep stand-alone and format, to allow for inheritance in CLDR183String type = attributes.getValue("type");184if ("stand-alone".equals(type) || "format".equals(type)) {185pushKeyContainer(qName, attributes, type);186} else {187pushIgnoredContainer(qName);188}189}190break;191case "monthWidth":192{193// for FormatData194// create string array for the two types that the JRE knows195// keep info about the context type so we can sort out inheritance later196String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName();197switch (attributes.getValue("type")) {198case "wide":199pushStringArrayEntry(qName, attributes, prefix + "MonthNames/" + getContainerKey(), 13);200break;201case "abbreviated":202pushStringArrayEntry(qName, attributes, prefix + "MonthAbbreviations/" + getContainerKey(), 13);203break;204case "narrow":205pushStringArrayEntry(qName, attributes, prefix + "MonthNarrows/" + getContainerKey(), 13);206break;207default:208pushIgnoredContainer(qName);209break;210}211}212break;213case "month":214// for FormatData215// add to string array entry of monthWidth element216pushStringArrayElement(qName, attributes, Integer.parseInt(attributes.getValue("type")) - 1);217break;218case "dayContext":219{220// for FormatData221// need to keep stand-alone and format, to allow for multiple inheritance in CLDR222String type = attributes.getValue("type");223if ("stand-alone".equals(type) || "format".equals(type)) {224pushKeyContainer(qName, attributes, type);225} else {226pushIgnoredContainer(qName);227}228}229break;230case "dayWidth":231{232// for FormatData233// create string array for the two types that the JRE knows234// keep info about the context type so we can sort out inheritance later235String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName();236switch (attributes.getValue("type")) {237case "wide":238pushStringArrayEntry(qName, attributes, prefix + "DayNames/" + getContainerKey(), 7);239break;240case "abbreviated":241pushStringArrayEntry(qName, attributes, prefix + "DayAbbreviations/" + getContainerKey(), 7);242break;243case "narrow":244pushStringArrayEntry(qName, attributes, prefix + "DayNarrows/" + getContainerKey(), 7);245break;246default:247pushIgnoredContainer(qName);248break;249}250}251break;252case "day":253// for FormatData254// add to string array entry of monthWidth element255pushStringArrayElement(qName, attributes, Integer.parseInt(DAY_OF_WEEK_MAP.get(attributes.getValue("type"))) - 1);256break;257case "dayPeriodContext":258// for FormatData259// need to keep stand-alone and format, to allow for multiple inheritance in CLDR260// for FormatData261// need to keep stand-alone and format, to allow for multiple inheritance in CLDR262{263String type = attributes.getValue("type");264if ("stand-alone".equals(type) || "format".equals(type)) {265pushKeyContainer(qName, attributes, type);266} else {267pushIgnoredContainer(qName);268}269}270break;271case "dayPeriodWidth":272// for FormatData273// create string array entry for am/pm. only keeping wide274switch (attributes.getValue("type")) {275case "wide":276pushStringArrayEntry(qName, attributes, "AmPmMarkers/" + getContainerKey(), 2);277break;278case "narrow":279pushStringArrayEntry(qName, attributes, "narrow.AmPmMarkers/" + getContainerKey(), 2);280break;281default:282pushIgnoredContainer(qName);283break;284}285break;286case "dayPeriod":287// for FormatData288// add to string array entry of AmPmMarkers element289if (attributes.getValue("alt") == null) {290switch (attributes.getValue("type")) {291case "am":292pushStringArrayElement(qName, attributes, 0);293break;294case "pm":295pushStringArrayElement(qName, attributes, 1);296break;297default:298pushIgnoredContainer(qName);299break;300}301} else {302// discard alt values303pushIgnoredContainer(qName);304}305break;306case "eraNames":307// CLDR era names are inconsistent in terms of their lengths. For example,308// the full names of Japanese imperial eras are eraAbbr, while the full names309// of the Julian eras are eraNames.310if (currentCalendarType == null) {311assert currentContainer instanceof IgnoredContainer;312pushIgnoredContainer(qName);313} else {314String key = currentCalendarType.keyElementName() + "long.Eras"; // for now315pushStringArrayEntry(qName, attributes, key, currentCalendarType.getEraLength(qName));316}317break;318case "eraAbbr":319// for FormatData320// create string array entry321if (currentCalendarType == null) {322assert currentContainer instanceof IgnoredContainer;323pushIgnoredContainer(qName);324} else {325String key = currentCalendarType.keyElementName() + "Eras";326pushStringArrayEntry(qName, attributes, key, currentCalendarType.getEraLength(qName));327}328break;329case "eraNarrow":330// mainly used for the Japanese imperial calendar331if (currentCalendarType == null) {332assert currentContainer instanceof IgnoredContainer;333pushIgnoredContainer(qName);334} else {335String key = currentCalendarType.keyElementName() + "narrow.Eras";336pushStringArrayEntry(qName, attributes, key, currentCalendarType.getEraLength(qName));337}338break;339case "era":340// for FormatData341// add to string array entry of eraAbbr element342if (currentCalendarType == null) {343assert currentContainer instanceof IgnoredContainer;344pushIgnoredContainer(qName);345} else {346int index = Integer.parseInt(attributes.getValue("type"));347index = currentCalendarType.normalizeEraIndex(index);348if (index >= 0) {349pushStringArrayElement(qName, attributes, index);350} else {351pushIgnoredContainer(qName);352}353if (currentContainer.getParent() == null) {354throw new InternalError("currentContainer: null parent");355}356}357break;358case "quarterContext":359{360// for FormatData361// need to keep stand-alone and format, to allow for inheritance in CLDR362String type = attributes.getValue("type");363if ("stand-alone".equals(type) || "format".equals(type)) {364pushKeyContainer(qName, attributes, type);365} else {366pushIgnoredContainer(qName);367}368}369break;370case "quarterWidth":371{372// for FormatData373// keep info about the context type so we can sort out inheritance later374String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName();375switch (attributes.getValue("type")) {376case "wide":377pushStringArrayEntry(qName, attributes, prefix + "QuarterNames/" + getContainerKey(), 4);378break;379case "abbreviated":380pushStringArrayEntry(qName, attributes, prefix + "QuarterAbbreviations/" + getContainerKey(), 4);381break;382case "narrow":383pushStringArrayEntry(qName, attributes, prefix + "QuarterNarrows/" + getContainerKey(), 4);384break;385default:386pushIgnoredContainer(qName);387break;388}389}390break;391case "quarter":392// for FormatData393// add to string array entry of quarterWidth element394pushStringArrayElement(qName, attributes, Integer.parseInt(attributes.getValue("type")) - 1);395break;396397//398// Time zone names399//400case "timeZoneNames":401pushContainer(qName, attributes);402break;403case "zone":404{405String tzid = attributes.getValue("type"); // Olson tz id406zonePrefix = CLDRConverter.TIMEZONE_ID_PREFIX;407put(zonePrefix + tzid, new HashMap<String, String>());408pushKeyContainer(qName, attributes, tzid);409}410break;411case "metazone":412{413String zone = attributes.getValue("type"); // LDML meta zone id414zonePrefix = CLDRConverter.METAZONE_ID_PREFIX;415put(zonePrefix + zone, new HashMap<String, String>());416pushKeyContainer(qName, attributes, zone);417}418break;419case "long":420zoneNameStyle = "long";421pushContainer(qName, attributes);422break;423case "short":424zoneNameStyle = "short";425pushContainer(qName, attributes);426break;427case "generic": // generic name428case "standard": // standard time name429case "daylight": // daylight saving (summer) time name430pushStringEntry(qName, attributes, CLDRConverter.ZONE_NAME_PREFIX + qName + "." + zoneNameStyle);431break;432case "exemplarCity": // not used in JDK433pushIgnoredContainer(qName);434break;435436//437// Number format information438//439case "decimalFormatLength":440if (attributes.getValue("type") == null) {441// skipping type="short" data442// for FormatData443// copy string for later assembly into NumberPatterns444pushStringEntry(qName, attributes, "NumberPatterns/decimal");445} else {446pushIgnoredContainer(qName);447}448break;449case "currencyFormat":450// for FormatData451// copy string for later assembly into NumberPatterns452pushStringEntry(qName, attributes, "NumberPatterns/currency");453break;454case "percentFormat":455// for FormatData456// copy string for later assembly into NumberPatterns457pushStringEntry(qName, attributes, "NumberPatterns/percent");458break;459case "defaultNumberingSystem":460// default numbering system if multiple numbering systems are used.461pushStringEntry(qName, attributes, "DefaultNumberingSystem");462break;463case "symbols":464// for FormatData465// look up numberingSystems466symbols: {467String script = attributes.getValue("numberSystem");468if (script == null) {469// Has no script. Just ignore.470pushIgnoredContainer(qName);471break;472}473474// Use keys as <script>."NumberElements/<symbol>"475currentNumberingSystem = script + ".";476String digits = CLDRConverter.handlerNumbering.get(script);477if (digits == null) {478throw new InternalError("null digits for " + script);479}480if (Character.isSurrogate(digits.charAt(0))) {481// DecimalFormatSymbols doesn't support supplementary characters as digit zero.482pushIgnoredContainer(qName);483break;484}485// in case digits are in the reversed order, reverse back the order.486if (digits.charAt(0) > digits.charAt(digits.length() - 1)) {487StringBuilder sb = new StringBuilder(digits);488digits = sb.reverse().toString();489}490// Check if the order is sequential.491char c0 = digits.charAt(0);492for (int i = 1; i < digits.length(); i++) {493if (digits.charAt(i) != c0 + i) {494pushIgnoredContainer(qName);495break symbols;496}497}498@SuppressWarnings("unchecked")499List<String> numberingScripts = (List<String>) get("numberingScripts");500if (numberingScripts == null) {501numberingScripts = new ArrayList<>();502put("numberingScripts", numberingScripts);503}504numberingScripts.add(script);505put(currentNumberingSystem + "NumberElements/zero", digits.substring(0, 1));506pushContainer(qName, attributes);507}508break;509case "decimal":510// for FormatData511// copy string for later assembly into NumberElements512pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/decimal");513break;514case "group":515// for FormatData516// copy string for later assembly into NumberElements517pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/group");518break;519case "list":520// for FormatData521// copy string for later assembly into NumberElements522pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/list");523break;524case "percentSign":525// for FormatData526// copy string for later assembly into NumberElements527pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/percent");528break;529case "nativeZeroDigit":530// for FormatData531// copy string for later assembly into NumberElements532pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/zero");533break;534case "patternDigit":535// for FormatData536// copy string for later assembly into NumberElements537pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/pattern");538break;539case "plusSign":540// TODO: DecimalFormatSymbols doesn't support plusSign541pushIgnoredContainer(qName);542break;543case "minusSign":544// for FormatData545// copy string for later assembly into NumberElements546pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/minus");547break;548case "exponential":549// for FormatData550// copy string for later assembly into NumberElements551pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/exponential");552break;553case "perMille":554// for FormatData555// copy string for later assembly into NumberElements556pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/permille");557break;558case "infinity":559// for FormatData560// copy string for later assembly into NumberElements561pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/infinity");562break;563case "nan":564// for FormatData565// copy string for later assembly into NumberElements566pushStringEntry(qName, attributes, currentNumberingSystem + "NumberElements/nan");567break;568case "timeFormatLength":569{570// for FormatData571// copy string for later assembly into DateTimePatterns572String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName();573pushStringEntry(qName, attributes, prefix + "DateTimePatterns/" + attributes.getValue("type") + "-time");574}575break;576case "dateFormatLength":577{578// for FormatData579// copy string for later assembly into DateTimePatterns580String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName();581pushStringEntry(qName, attributes, prefix + "DateTimePatterns/" + attributes.getValue("type") + "-date");582}583break;584case "dateTimeFormat":585{586// for FormatData587// copy string for later assembly into DateTimePatterns588String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName();589pushStringEntry(qName, attributes, prefix + "DateTimePatterns/date-time");590}591break;592case "localizedPatternChars":593{594// for FormatData595// copy string for later adaptation to JRE use596String prefix = (currentCalendarType == null) ? "" : currentCalendarType.keyElementName();597pushStringEntry(qName, attributes, prefix + "DateTimePatternChars");598}599break;600601default:602// treat anything else as a container603pushContainer(qName, attributes);604break;605}606}607608@Override609public void endElement(String uri, String localName, String qName) throws SAXException {610assert qName.equals(currentContainer.getqName()) : "current=" + currentContainer.getqName() + ", param=" + qName;611switch (qName) {612case "calendar":613assert !(currentContainer instanceof Entry);614currentCalendarType = null;615break;616617case "defaultNumberingSystem":618if (currentContainer instanceof StringEntry) {619defaultNumberingSystem = ((StringEntry) currentContainer).getValue();620assert defaultNumberingSystem != null;621put(((StringEntry) currentContainer).getKey(), defaultNumberingSystem);622} else {623defaultNumberingSystem = null;624}625break;626627case "timeZoneNames":628zonePrefix = null;629break;630case "generic":631case "standard":632case "daylight":633if (zonePrefix != null && (currentContainer instanceof Entry)) {634@SuppressWarnings("unchecked")635Map<String, String> valmap = (Map<String, String>) get(zonePrefix + getContainerKey());636Entry<?> entry = (Entry<?>) currentContainer;637valmap.put(entry.getKey(), (String) entry.getValue());638}639break;640default:641if (currentContainer instanceof Entry) {642Entry<?> entry = (Entry<?>) currentContainer;643Object value = entry.getValue();644if (value != null) {645put(entry.getKey(), value);646}647}648}649currentContainer = currentContainer.getParent();650}651}652653654