1 /*
2 * Copyright (c) 2012, 2013, 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.io.PrintWriter;
31 import java.util.Formatter;
32 import java.util.HashSet;
33 import java.util.Map;
34 import java.util.Set;
35 import java.util.SortedSet;
36
37 class ResourceBundleGenerator implements BundleGenerator {
38 @Override
39 public void generateBundle(String packageName, String baseName, String localeID, boolean useJava,
40 Map<String, ?> map, BundleType type) throws IOException {
41 String suffix = useJava ? ".java" : ".properties";
42 String lang = CLDRConverter.getLanguageCode(localeID);
43 String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator
44 + packageName + File.separator + "resources" + File.separator + "cldr";
45 if (lang.length() > 0) {
46 dirName = dirName + File.separator + lang;
47 packageName = packageName + ".resources.cldr." + lang;
48 } else {
49 packageName = packageName + ".resources.cldr";
50 }
51 File dir = new File(dirName);
52 if (!dir.exists()) {
53 dir.mkdirs();
54 }
55 File file = new File(dir, baseName + ("root".equals(localeID) ? "" : "_" + localeID) + suffix);
56 if (!file.exists()) {
57 file.createNewFile();
72 Formatter fmt = null;
73 if (type == BundleType.TIMEZONE) {
74 fmt = new Formatter();
75 Set<String> metaKeys = new HashSet<>();
76 for (String key : map.keySet()) {
77 if (key.startsWith(CLDRConverter.METAZONE_ID_PREFIX)) {
78 String meta = key.substring(CLDRConverter.METAZONE_ID_PREFIX.length());
79 String[] value;
80 value = (String[]) map.get(key);
81 fmt.format(" final String[] %s = new String[] {\n", meta);
82 for (String s : value) {
83 fmt.format(" \"%s\",\n", CLDRConverter.saveConvert(s, useJava));
84 }
85 fmt.format(" };\n");
86 metaKeys.add(key);
87 }
88 }
89 for (String key : metaKeys) {
90 map.remove(key);
91 }
92 }
93
94 try (PrintWriter out = new PrintWriter(file, encoding)) {
95 // Output copyright headers
96 out.println(CopyrightHeaders.getOpenJDKCopyright());
97 out.println(CopyrightHeaders.getUnicodeCopyright());
98
99 if (useJava) {
100 out.println("package sun." + packageName + ";\n");
101 out.printf("import %s;\n\n", type.getPathName());
102 out.printf("public class %s%s extends %s {\n", baseName, "root".equals(localeID) ? "" : "_" + localeID, type.getClassName());
103
104 out.println(" @Override\n" +
105 " protected final Object[][] getContents() {");
106 if (fmt != null) {
107 out.print(fmt.toString());
108 }
109 out.println(" final Object[][] data = new Object[][] {");
110 }
111 for (String key : map.keySet()) {
|
1 /*
2 * Copyright (c) 2012, 2016, 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.io.PrintWriter;
31 import java.util.Formatter;
32 import java.util.HashSet;
33 import java.util.LinkedHashMap;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.SortedSet;
37
38 class ResourceBundleGenerator implements BundleGenerator {
39 // preferred timezones - keeping compatibility with JDK1.1 3 letter abbreviations
40 private static final String[] preferredTZIDs = {
41 "America/Los_Angeles",
42 "America/Denver",
43 "America/Phoenix",
44 "America/Chicago",
45 "America/New_York",
46 "America/Indianapolis",
47 "Pacific/Honolulu",
48 "America/Anchorage",
49 "America/Halifax",
50 "America/Sitka",
51 "America/St_Johns",
52 "Europe/Paris",
53 // Although CLDR does not support abbreviated zones, handle "GMT" as a
54 // special case here, as it is specified in the javadoc.
55 "GMT",
56 "Africa/Casablanca",
57 "Asia/Jerusalem",
58 "Asia/Tokyo",
59 "Europe/Bucharest",
60 "Asia/Shanghai",
61 "UTC",
62 };
63
64 @Override
65 public void generateBundle(String packageName, String baseName, String localeID, boolean useJava,
66 Map<String, ?> map, BundleType type) throws IOException {
67 String suffix = useJava ? ".java" : ".properties";
68 String lang = CLDRConverter.getLanguageCode(localeID);
69 String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator
70 + packageName + File.separator + "resources" + File.separator + "cldr";
71 if (lang.length() > 0) {
72 dirName = dirName + File.separator + lang;
73 packageName = packageName + ".resources.cldr." + lang;
74 } else {
75 packageName = packageName + ".resources.cldr";
76 }
77 File dir = new File(dirName);
78 if (!dir.exists()) {
79 dir.mkdirs();
80 }
81 File file = new File(dir, baseName + ("root".equals(localeID) ? "" : "_" + localeID) + suffix);
82 if (!file.exists()) {
83 file.createNewFile();
98 Formatter fmt = null;
99 if (type == BundleType.TIMEZONE) {
100 fmt = new Formatter();
101 Set<String> metaKeys = new HashSet<>();
102 for (String key : map.keySet()) {
103 if (key.startsWith(CLDRConverter.METAZONE_ID_PREFIX)) {
104 String meta = key.substring(CLDRConverter.METAZONE_ID_PREFIX.length());
105 String[] value;
106 value = (String[]) map.get(key);
107 fmt.format(" final String[] %s = new String[] {\n", meta);
108 for (String s : value) {
109 fmt.format(" \"%s\",\n", CLDRConverter.saveConvert(s, useJava));
110 }
111 fmt.format(" };\n");
112 metaKeys.add(key);
113 }
114 }
115 for (String key : metaKeys) {
116 map.remove(key);
117 }
118
119 // Make it preferred ordered
120 LinkedHashMap<String, Object> newMap = new LinkedHashMap<>();
121 for (String preferred : preferredTZIDs) {
122 if (map.containsKey(preferred)) {
123 newMap.put(preferred, map.remove(preferred));
124 } else if (("GMT".equals(preferred) || "UTC".equals(preferred)) &&
125 metaKeys.contains(CLDRConverter.METAZONE_ID_PREFIX+preferred)) {
126 newMap.put(preferred, preferred);
127 }
128 }
129 newMap.putAll(map);
130 map = newMap;
131 }
132
133 try (PrintWriter out = new PrintWriter(file, encoding)) {
134 // Output copyright headers
135 out.println(CopyrightHeaders.getOpenJDKCopyright());
136 out.println(CopyrightHeaders.getUnicodeCopyright());
137
138 if (useJava) {
139 out.println("package sun." + packageName + ";\n");
140 out.printf("import %s;\n\n", type.getPathName());
141 out.printf("public class %s%s extends %s {\n", baseName, "root".equals(localeID) ? "" : "_" + localeID, type.getClassName());
142
143 out.println(" @Override\n" +
144 " protected final Object[][] getContents() {");
145 if (fmt != null) {
146 out.print(fmt.toString());
147 }
148 out.println(" final Object[][] data = new Object[][] {");
149 }
150 for (String key : map.keySet()) {
|