Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/cldrconverter/ResourceBundleGenerator.java
32287 views
/*1* Copyright (c) 2012, 2016, 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.io.PrintWriter;30import java.util.Formatter;31import java.util.HashSet;32import java.util.LinkedHashMap;33import java.util.Map;34import java.util.Set;35import java.util.SortedSet;3637class ResourceBundleGenerator implements BundleGenerator {38// preferred timezones - keeping compatibility with JDK1.1 3 letter abbreviations39private static final String[] preferredTZIDs = {40"America/Los_Angeles",41"America/Denver",42"America/Phoenix",43"America/Chicago",44"America/New_York",45"America/Indianapolis",46"Pacific/Honolulu",47"America/Anchorage",48"America/Halifax",49"America/Sitka",50"America/St_Johns",51"Europe/Paris",52// Although CLDR does not support abbreviated zones, handle "GMT" as a53// special case here, as it is specified in the javadoc.54"GMT",55"Africa/Casablanca",56"Asia/Jerusalem",57"Asia/Tokyo",58"Europe/Bucharest",59"Asia/Shanghai",60"UTC",61};6263@Override64public void generateBundle(String packageName, String baseName, String localeID, boolean useJava,65Map<String, ?> map, BundleType type) throws IOException {66String suffix = useJava ? ".java" : ".properties";67String lang = CLDRConverter.getLanguageCode(localeID);68String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator69+ packageName + File.separator + "resources" + File.separator + "cldr";70if (lang.length() > 0) {71dirName = dirName + File.separator + lang;72packageName = packageName + ".resources.cldr." + lang;73} else {74packageName = packageName + ".resources.cldr";75}76File dir = new File(dirName);77if (!dir.exists()) {78dir.mkdirs();79}80File file = new File(dir, baseName + ("root".equals(localeID) ? "" : "_" + localeID) + suffix);81if (!file.exists()) {82file.createNewFile();83}84CLDRConverter.info("\tWriting file " + file);8586String encoding;87if (useJava) {88if (CLDRConverter.USE_UTF8) {89encoding = "utf-8";90} else {91encoding = "us-ascii";92}93} else {94encoding = "iso-8859-1";95}9697Formatter fmt = null;98if (type == BundleType.TIMEZONE) {99fmt = new Formatter();100Set<String> metaKeys = new HashSet<>();101for (String key : map.keySet()) {102if (key.startsWith(CLDRConverter.METAZONE_ID_PREFIX)) {103String meta = key.substring(CLDRConverter.METAZONE_ID_PREFIX.length());104String[] value;105value = (String[]) map.get(key);106fmt.format(" final String[] %s = new String[] {\n", meta);107for (String s : value) {108fmt.format(" \"%s\",\n", CLDRConverter.saveConvert(s, useJava));109}110fmt.format(" };\n");111metaKeys.add(key);112}113}114for (String key : metaKeys) {115map.remove(key);116}117118// Make it preferred ordered119LinkedHashMap<String, Object> newMap = new LinkedHashMap<>();120for (String preferred : preferredTZIDs) {121if (map.containsKey(preferred)) {122newMap.put(preferred, map.remove(preferred));123} else if (("GMT".equals(preferred) || "UTC".equals(preferred)) &&124metaKeys.contains(CLDRConverter.METAZONE_ID_PREFIX+preferred)) {125newMap.put(preferred, preferred);126}127}128newMap.putAll(map);129map = newMap;130}131132try (PrintWriter out = new PrintWriter(file, encoding)) {133// Output copyright headers134out.println(CopyrightHeaders.getOpenJDKCopyright());135out.println(CopyrightHeaders.getUnicodeCopyright());136137if (useJava) {138out.println("package sun." + packageName + ";\n");139out.printf("import %s;\n\n", type.getPathName());140out.printf("public class %s%s extends %s {\n", baseName, "root".equals(localeID) ? "" : "_" + localeID, type.getClassName());141142out.println(" @Override\n" +143" protected final Object[][] getContents() {");144if (fmt != null) {145out.print(fmt.toString());146}147out.println(" final Object[][] data = new Object[][] {");148}149for (String key : map.keySet()) {150if (useJava) {151Object value = map.get(key);152if (value == null) {153CLDRConverter.warning("null value for " + key);154} else if (value instanceof String) {155if (type == BundleType.TIMEZONE) {156out.printf(" { \"%s\", %s },\n", key, CLDRConverter.saveConvert((String) value, useJava));157} else {158out.printf(" { \"%s\", \"%s\" },\n", key, CLDRConverter.saveConvert((String) value, useJava));159}160} else if (value instanceof String[]) {161String[] values = (String[]) value;162out.println(" { \"" + key + "\",\n new String[] {");163for (String s : values) {164out.println(" \"" + CLDRConverter.saveConvert(s, useJava) + "\",");165}166out.println(" }\n },");167} else {168throw new RuntimeException("unknown value type: " + value.getClass().getName());169}170} else {171out.println(key + "=" + CLDRConverter.saveConvert((String) map.get(key), useJava));172}173}174if (useJava) {175out.println(" };\n return data;\n }\n}");176}177}178}179180private static final String METAINFO_CLASS = "CLDRLocaleDataMetaInfo";181182@Override183public void generateMetaInfo(Map<String, SortedSet<String>> metaInfo) throws IOException {184String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator + "util" + File.separator185+ "cldr" + File.separator;186File dir = new File(dirName);187if (!dir.exists()) {188dir.mkdirs();189}190File file = new File(dir, METAINFO_CLASS + ".java");191if (!file.exists()) {192file.createNewFile();193}194CLDRConverter.info("Generating file " + file);195196try (PrintWriter out = new PrintWriter(file, "us-ascii")) {197out.println(CopyrightHeaders.getOpenJDKCopyright());198199out.println("package sun.util.cldr;\n\n"200+ "import java.util.ListResourceBundle;\n");201out.printf("public class %s extends ListResourceBundle {\n", METAINFO_CLASS);202out.println(" @Override\n" +203" protected final Object[][] getContents() {\n" +204" final Object[][] data = new Object[][] {");205for (String key : metaInfo.keySet()) {206out.printf(" { \"%s\",\n", key);207out.printf(" \"%s\" },\n", toLocaleList(metaInfo.get(key)));208}209out.println(" };\n return data;\n }\n}");210}211}212213private static String toLocaleList(SortedSet<String> set) {214StringBuilder sb = new StringBuilder(set.size() * 6);215for (String id : set) {216if (!"root".equals(id)) {217if (sb.length() > 0) {218sb.append(' ');219}220sb.append(id);221}222}223return sb.toString();224}225}226227228