Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/cldrconverter/AbstractLDMLHandler.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 build.tools.cldrconverter.CLDRConverter.DraftType;28import java.util.HashMap;29import java.util.Map;30import java.util.Set;31import org.xml.sax.Attributes;32import org.xml.sax.SAXException;33import org.xml.sax.SAXParseException;34import org.xml.sax.helpers.DefaultHandler;3536/**37* This is an abstract class for general LDML parsing purpose.38* LDMLParseHandler, SupplementLDMLParseHandler, and NumberingLDMLParseHandler39* are the subclasses of this class.40*/4142abstract class AbstractLDMLHandler<V> extends DefaultHandler {43static final Map<String, String> DAY_OF_WEEK_MAP = new HashMap<>();44static {45DAY_OF_WEEK_MAP.put("sun", "1");46DAY_OF_WEEK_MAP.put("mon", "2");47DAY_OF_WEEK_MAP.put("tue", "3");48DAY_OF_WEEK_MAP.put("wed", "4");49DAY_OF_WEEK_MAP.put("thu", "5");50DAY_OF_WEEK_MAP.put("fri", "6");51DAY_OF_WEEK_MAP.put("sat", "7");52}53// Collected data in JRE locale data format.54private Map<String, V> data = new HashMap<>();5556// The root Container57Container currentContainer = new Container("$ROOT", null);5859AbstractLDMLHandler() {60}6162Map<String, V> getData() {63return data;64}6566V put(String key, V value) {67return data.put(key, value);68}6970V get(String key) {71return data.get(key);72}7374Set<String> keySet() {75return data.keySet();76}7778/*79* It returns true if the data should be ignored based on the user80* defined acceptance level, which is listed with draft attribute in81* the cldr locale xml files.82* When the alt attribute is present, the data is always ignored since83* we always use the primary data84*/85boolean isIgnored(Attributes attributes) {86if (attributes.getValue("alt") != null) {87return true;88}89String draftValue = attributes.getValue("draft");90if (draftValue != null) {91return DraftType.getDefault().ordinal() > DraftType.forKeyword(draftValue).ordinal();92}93return false;94}9596void pushContainer(String qName, Attributes attributes) {97if (isIgnored(attributes) || currentContainer instanceof IgnoredContainer) {98currentContainer = new IgnoredContainer(qName, currentContainer);99} else {100currentContainer = new Container(qName, currentContainer);101}102}103104void pushIgnoredContainer(String qName) {105currentContainer = new IgnoredContainer(qName, currentContainer);106}107108void pushKeyContainer(String qName, Attributes attributes, String key) {109if (!pushIfIgnored(qName, attributes)) {110currentContainer = new KeyContainer(qName, currentContainer, key);111}112}113114/**115* start an element that defines a string entry, with the value provided by the element's text.116*/117void pushStringEntry(String qName, Attributes attributes, String key) {118if (!pushIfIgnored(qName, attributes)) {119currentContainer = new StringEntry(qName, currentContainer, key);120}121}122123/**124* start an element that defines a string entry, with the value provided by an attribute value.125*/126void pushStringEntry(String qName, Attributes attributes, String key, String value) {127if (!pushIfIgnored(qName, attributes)) {128currentContainer = new StringEntry(qName, currentContainer, key, value);129}130}131132void pushStringArrayEntry(String qName, Attributes attributes, String key, int length) {133if (!pushIfIgnored(qName, attributes)) {134currentContainer = new StringArrayEntry(qName, currentContainer, key, length);135}136}137138void pushStringArrayElement(String qName, Attributes attributes, int index) {139if (!pushIfIgnored(qName, attributes)) {140currentContainer = new StringArrayElement(qName, currentContainer, index);141}142}143144private boolean pushIfIgnored(String qName, Attributes attributes) {145if (isIgnored(attributes) || currentContainer instanceof IgnoredContainer) {146pushIgnoredContainer(qName);147return true;148}149return false;150}151152/**153* Obtains the key from the innermost containing container that provides one.154*/155String getContainerKey() {156Container current = currentContainer;157while (current != null) {158if (current instanceof KeyContainer) {159return ((KeyContainer) current).getKey();160}161current = current.getParent();162}163return null;164}165166@Override167public void characters(char[] ch, int start, int length) throws SAXException {168currentContainer.addCharacters(ch, start, length);169}170171@SuppressWarnings(value = "CallToThreadDumpStack")172@Override173public void warning(SAXParseException e) throws SAXException {174e.printStackTrace();175}176177@SuppressWarnings(value = "CallToThreadDumpStack")178@Override179public void error(SAXParseException e) throws SAXException {180e.printStackTrace();181}182183@SuppressWarnings(value = "CallToThreadDumpStack")184@Override185public void fatalError(SAXParseException e) throws SAXException {186e.printStackTrace();187super.fatalError(e);188}189}190191192