1 /*
   2  * Copyright (c) 2019, 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.Arrays;
  31 import java.util.HashMap;
  32 import java.util.Map;
  33 import java.util.stream.Stream;
  34 import org.xml.sax.Attributes;
  35 import org.xml.sax.InputSource;
  36 import org.xml.sax.SAXException;
  37 
  38 /**
  39  * Handles parsing of files in Locale Data Markup Language for
  40  * plurals.xml
  41  */
  42 
  43 class PluralsParseHandler extends AbstractLDMLHandler<Object> {
  44     @Override
  45     public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException {
  46         // avoid HTTP traffic to unicode.org
  47         if (systemID.startsWith(CLDRConverter.SPPL_LDML_DTD_SYSTEM_ID)) {
  48             return new InputSource((new File(CLDRConverter.LOCAL_SPPL_LDML_DTD)).toURI().toString());
  49         }
  50         return null;
  51     }
  52 
  53     @Override
  54     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  55         switch (qName) {
  56         case "plurals":
  57             // Only deal with "cardinal" type for now.
  58             if (attributes.getValue("type").equals("cardinal")) {
  59                 pushContainer(qName, attributes);
  60             } else {
  61                 // ignore
  62                 pushIgnoredContainer(qName);
  63             }
  64             break;
  65         case "pluralRules":
  66             // key: locales
  67             pushKeyContainer(qName, attributes, attributes.getValue("locales"));
  68             break;
  69         case "pluralRule":
  70             pushStringEntry(qName, attributes, attributes.getValue("count"));
  71             break;
  72         default:
  73             // treat anything else as a container
  74             pushContainer(qName, attributes);
  75             break;
  76         }
  77     }
  78 
  79     @Override
  80     public void endElement(String uri, String localName, String qName) throws SAXException {
  81         assert qName.equals(currentContainer.getqName()) : "current=" + currentContainer.getqName() + ", param=" + qName;
  82         switch (qName) {
  83             case "pluralRule":
  84                 assert !(currentContainer instanceof Entry);
  85                 Entry entry = (Entry)currentContainer;
  86                 final String count = entry.getKey();
  87                 final String rule = (String)entry.getValue();
  88                 String locales = ((KeyContainer)(currentContainer.getParent())).getKey();
  89                 Arrays.stream(locales.split("\\s"))
  90                         .forEach(loc -> {
  91                             Map<String, String> rules = (Map<String, String>)get(loc);
  92                             if (rules == null) {
  93                                 rules = new HashMap<>();
  94                                 put(loc, rules);
  95                             }
  96                             if (!count.equals("other")) {
  97                                 rules.put(count, rule);
  98                             }
  99                         });
 100                 break;
 101         }
 102 
 103         currentContainer = currentContainer.getParent();
 104     }
 105 }