1 /*
2 * Copyright (c) 2012, 2015, 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
239
240 @Override
241 public void generateMetaInfo(Map<String, SortedSet<String>> metaInfo) throws IOException {
242 String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator + "util" +
243 File.separator +
244 (CLDRConverter.isBaseModule ? "cldr" + File.separator + File.separator :
245 "resources" + File.separator + "cldr" + File.separator + "provider" + File.separator);
246 File dir = new File(dirName);
247 if (!dir.exists()) {
248 dir.mkdirs();
249 }
250 String className =
251 (CLDRConverter.isBaseModule ? "CLDRBaseLocaleDataMetaInfo" : "CLDRLocaleDataMetaInfo");
252 File file = new File(dir, className + ".java");
253 if (!file.exists()) {
254 file.createNewFile();
255 }
256 CLDRConverter.info("Generating file " + file);
257
258 try (PrintWriter out = new PrintWriter(file, "us-ascii")) {
259 out.println(CopyrightHeaders.getOpenJDKCopyright());
260
261 out.println((CLDRConverter.isBaseModule ? "package sun.util.cldr;\n\n" :
262 "package sun.util.resources.cldr.provider;\n\n")
263 + "import java.util.HashMap;\n"
264 + "import java.util.Locale;\n"
265 + "import java.util.Map;\n"
266 + "import sun.util.locale.provider.LocaleProviderAdapter;\n"
267 + "import sun.util.locale.provider.LocaleDataMetaInfo;\n");
268 out.printf("public class %s implements LocaleDataMetaInfo {\n", className);
269 out.println(" private static final Map<String, String> resourceNameToLocales = new HashMap<>();\n" +
270 (CLDRConverter.isBaseModule ?
271 " private static final Map<Locale, String[]> parentLocalesMap = new HashMap<>();\n\n" : "\n") +
272 " static {\n");
273
274 for (String key : metaInfo.keySet()) {
275 if (key.startsWith(CLDRConverter.PARENT_LOCALE_PREFIX)) {
276 String parentTag = key.substring(CLDRConverter.PARENT_LOCALE_PREFIX.length());
277 if ("root".equals(parentTag)) {
278 out.printf(" parentLocalesMap.put(Locale.ROOT,\n");
279 } else {
280 out.printf(" parentLocalesMap.put(Locale.forLanguageTag(\"%s\"),\n",
281 parentTag);
282 }
283 String[] children = toLocaleList(metaInfo.get(key), true).split(" ");
284 Arrays.sort(children);
285 out.printf(" new String[] {\n" +
286 " ");
287 int count = 0;
288 for (int i = 0; i < children.length; i++) {
289 String child = children[i];
290 out.printf("\"%s\", ", child);
291 count += child.length() + 4;
292 if (i != children.length - 1 && count > 64) {
293 out.printf("\n ");
294 count = 0;
295 }
296 }
297 out.printf("\n });\n");
298 } else {
299 out.printf(" resourceNameToLocales.put(\"%s\",\n", key);
300 out.printf(" \"%s\");\n",
301 toLocaleList(key.equals("FormatData") ? metaInfo.get("AvailableLocales") :
302 metaInfo.get(key), false));
303 }
304 }
305 out.println(" }\n\n");
306
307 out.println(" @Override\n" +
308 " public LocaleProviderAdapter.Type getType() {\n" +
309 " return LocaleProviderAdapter.Type.CLDR;\n" +
310 " }\n\n");
311
312 out.println(" @Override\n" +
313 " public String availableLanguageTags(String category) {\n" +
314 " return resourceNameToLocales.getOrDefault(category, \"\");\n" +
315 " }\n\n");
316
317 if (CLDRConverter.isBaseModule) {
318 out.printf(" public Map<Locale, String[]> parentLocales() {\n" +
319 " return parentLocalesMap;\n" +
320 " }\n}");
321 } else {
322 out.println("}");
323 }
324 }
325 }
326
327 private static final Locale.Builder LOCALE_BUILDER = new Locale.Builder();
328 private static boolean isBaseLocale(String localeID) {
329 localeID = localeID.replaceAll("-", "_");
330 // ignore script here
331 Locale locale = LOCALE_BUILDER
332 .clear()
333 .setLanguage(CLDRConverter.getLanguageCode(localeID))
334 .setRegion(CLDRConverter.getRegionCode(localeID))
335 .build();
336 return CLDRConverter.BASE_LOCALES.contains(locale);
337 }
338
339 private static String toLocaleList(SortedSet<String> set, boolean all) {
340 StringBuilder sb = new StringBuilder(set.size() * 6);
341 for (String id : set) {
342 if (!"root".equals(id)) {
|
1 /*
2 * Copyright (c) 2012, 2017, 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
239
240 @Override
241 public void generateMetaInfo(Map<String, SortedSet<String>> metaInfo) throws IOException {
242 String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator + "util" +
243 File.separator +
244 (CLDRConverter.isBaseModule ? "cldr" + File.separator + File.separator :
245 "resources" + File.separator + "cldr" + File.separator + "provider" + File.separator);
246 File dir = new File(dirName);
247 if (!dir.exists()) {
248 dir.mkdirs();
249 }
250 String className =
251 (CLDRConverter.isBaseModule ? "CLDRBaseLocaleDataMetaInfo" : "CLDRLocaleDataMetaInfo");
252 File file = new File(dir, className + ".java");
253 if (!file.exists()) {
254 file.createNewFile();
255 }
256 CLDRConverter.info("Generating file " + file);
257
258 try (PrintWriter out = new PrintWriter(file, "us-ascii")) {
259 out.printf(CopyrightHeaders.getOpenJDKCopyright());
260
261 out.printf((CLDRConverter.isBaseModule ? "package sun.util.cldr;\n\n" :
262 "package sun.util.resources.cldr.provider;\n\n")
263 + "import java.util.HashMap;\n"
264 + "import java.util.Locale;\n"
265 + "import java.util.Map;\n"
266 + "import sun.util.locale.provider.LocaleDataMetaInfo;\n"
267 + "import sun.util.locale.provider.LocaleProviderAdapter;\n\n");
268 out.printf("public class %s implements LocaleDataMetaInfo {\n", className);
269 out.printf(" private static final Map<String, String> resourceNameToLocales = new HashMap<>();\n" +
270 (CLDRConverter.isBaseModule ?
271 " private static final Map<Locale, String[]> parentLocalesMap = new HashMap<>();\n\n" :
272 "\n") +
273 " static {\n");
274
275 for (String key : metaInfo.keySet()) {
276 if (key.startsWith(CLDRConverter.PARENT_LOCALE_PREFIX)) {
277 String parentTag = key.substring(CLDRConverter.PARENT_LOCALE_PREFIX.length());
278 if ("root".equals(parentTag)) {
279 out.printf(" parentLocalesMap.put(Locale.ROOT,\n");
280 } else {
281 out.printf(" parentLocalesMap.put(Locale.forLanguageTag(\"%s\"),\n",
282 parentTag);
283 }
284 String[] children = toLocaleList(metaInfo.get(key), true).split(" ");
285 Arrays.sort(children);
286 out.printf(" new String[] {\n" +
287 " ");
288 int count = 0;
289 for (int i = 0; i < children.length; i++) {
290 String child = children[i];
291 out.printf("\"%s\", ", child);
292 count += child.length() + 4;
293 if (i != children.length - 1 && count > 64) {
294 out.printf("\n ");
295 count = 0;
296 }
297 }
298 out.printf("\n });\n");
299 } else {
300 if ("AvailableLocales".equals(key)) {
301 out.printf(" resourceNameToLocales.put(\"%s\",\n", key);
302 out.printf(" \"%s\");\n", toLocaleList(metaInfo.get(key), false));
303 }
304 }
305 }
306
307 out.printf(" }\n\n");
308
309 // end of static initializer block.
310
311 // Short TZ names for delayed initialization
312 if (CLDRConverter.isBaseModule) {
313 out.printf(" private static class TZShortIDMapHolder {\n");
314 out.printf(" static final Map<String, String> tzShortIDMap = new HashMap<>();\n");
315 out.printf(" static {\n");
316 CLDRConverter.handlerTimeZone.getData().entrySet().stream()
317 .forEach(e -> {
318 out.printf(" tzShortIDMap.put(\"%s\", \"%s\");\n", e.getKey(),
319 ((String)e.getValue()));
320 });
321 out.printf(" }\n }\n\n");
322 }
323
324 out.printf(" @Override\n" +
325 " public LocaleProviderAdapter.Type getType() {\n" +
326 " return LocaleProviderAdapter.Type.CLDR;\n" +
327 " }\n\n");
328
329 out.printf(" @Override\n" +
330 " public String availableLanguageTags(String category) {\n" +
331 " return resourceNameToLocales.getOrDefault(category, \"\");\n" +
332 " }\n\n");
333
334 if (CLDRConverter.isBaseModule) {
335 out.printf(" @Override\n" +
336 " public Map<String, String> tzShortIDs() {\n" +
337 " return TZShortIDMapHolder.tzShortIDMap;\n" +
338 " }\n\n");
339 out.printf(" public Map<Locale, String[]> parentLocales() {\n" +
340 " return parentLocalesMap;\n" +
341 " }\n}");
342 } else {
343 out.printf("}");
344 }
345 }
346 }
347
348 private static final Locale.Builder LOCALE_BUILDER = new Locale.Builder();
349 private static boolean isBaseLocale(String localeID) {
350 localeID = localeID.replaceAll("-", "_");
351 // ignore script here
352 Locale locale = LOCALE_BUILDER
353 .clear()
354 .setLanguage(CLDRConverter.getLanguageCode(localeID))
355 .setRegion(CLDRConverter.getRegionCode(localeID))
356 .build();
357 return CLDRConverter.BASE_LOCALES.contains(locale);
358 }
359
360 private static String toLocaleList(SortedSet<String> set, boolean all) {
361 StringBuilder sb = new StringBuilder(set.size() * 6);
362 for (String id : set) {
363 if (!"root".equals(id)) {
|