417 }
418
419 // Parsers for data in "supplemental" directory
420 //
421 private static void parseSupplemental() throws Exception {
422 // Parse SupplementalData file and store the information in the HashMap
423 // Calendar information such as firstDay and minDay are stored in
424 // supplementalData.xml as of CLDR1.4. Individual territory is listed
425 // with its ISO 3166 country code while default is listed using UNM49
426 // region and composition numerical code (001 for World.)
427 //
428 // SupplementalData file also provides the "parent" locales which
429 // are othrwise not to be fallen back. Process them here as well.
430 //
431 handlerSuppl = new SupplementDataParseHandler();
432 parseLDMLFile(new File(SPPL_SOURCE_FILE), handlerSuppl);
433 Map<String, Object> parentData = handlerSuppl.getData("root");
434 parentData.keySet().stream()
435 .filter(key -> key.startsWith(PARENT_LOCALE_PREFIX))
436 .forEach(key -> {
437 parentLocalesMap.put(key, new TreeSet(
438 Arrays.asList(((String)parentData.get(key)).split(" "))));
439 });
440
441 // Parse numberingSystems to get digit zero character information.
442 handlerNumbering = new NumberingSystemsParseHandler();
443 parseLDMLFile(new File(NUMBERING_SOURCE_FILE), handlerNumbering);
444
445 // Parse metaZones to create mappings between Olson tzids and CLDR meta zone names
446 handlerMetaZones = new MetaZonesParseHandler();
447 parseLDMLFile(new File(METAZONES_SOURCE_FILE), handlerMetaZones);
448
449 // Parse likelySubtags
450 handlerLikelySubtags = new LikelySubtagsParseHandler();
451 parseLDMLFile(new File(LIKELYSUBTAGS_SOURCE_FILE), handlerLikelySubtags);
452
453 // Parse supplementalMetadata
454 // Currently interested in deprecated time zone ids and language aliases.
455 handlerSupplMeta = new SupplementalMetadataParseHandler();
456 parseLDMLFile(new File(SPPL_META_SOURCE_FILE), handlerSupplMeta);
457
464 parseLDMLFile(new File(PLURALS_SOURCE_FILE), handlerPlurals);
465 }
466
467 // Parsers for data in "bcp47" directory
468 //
469 private static void parseBCP47() throws Exception {
470 // Parse timezone
471 handlerTimeZone = new TimeZoneParseHandler();
472 parseLDMLFile(new File(TIMEZONE_SOURCE_FILE), handlerTimeZone);
473
474 // canonical tz name map
475 // alias -> primary
476 handlerTimeZone.getData().forEach((k, v) -> {
477 String[] ids = ((String)v).split("\\s");
478 for (int i = 1; i < ids.length; i++) {
479 canonicalTZMap.put(ids[i], ids[0]);
480 }
481 });
482 }
483
484 private static void parseLDMLFile(File srcfile, AbstractLDMLHandler handler) throws Exception {
485 info("..... Parsing " + srcfile.getName() + " .....");
486 SAXParserFactory pf = SAXParserFactory.newInstance();
487 pf.setValidating(true);
488 SAXParser parser = pf.newSAXParser();
489 enableFileAccess(parser);
490 parser.parse(srcfile, handler);
491 }
492
493 private static StringBuilder getCandLocales(Locale cldrLoc) {
494 List<Locale> candList = getCandidateLocales(cldrLoc);
495 StringBuilder sb = new StringBuilder();
496 for (Locale loc : candList) {
497 if (!loc.equals(Locale.ROOT)) {
498 sb.append(toLocaleName(loc.toLanguageTag()));
499 sb.append(",");
500 }
501 }
502 return sb;
503 }
504
557 if (bundleTypes.contains(Bundle.Type.FORMATDATA)) {
558 Map<String, Object> formatDataMap = extractFormatData(targetMap, bundle.getID());
559 if (!formatDataMap.isEmpty() || bundle.isRoot()) {
560 bundleGenerator.generateBundle("text", "FormatData", bundle.getJavaID(), true, formatDataMap, BundleType.PLAIN);
561 }
562 }
563
564 // For AvailableLocales
565 metaInfo.get("AvailableLocales").add(toLanguageTag(bundle.getID()));
566 addLikelySubtags(metaInfo, "AvailableLocales", bundle.getID());
567 }
568 bundleGenerator.generateMetaInfo(metaInfo);
569 }
570
571 static final Map<String, String> aliases = new HashMap<>();
572
573 /**
574 * Translate the aliases into the real entries in the bundle map.
575 */
576 static void handleAliases(Map<String, Object> bundleMap) {
577 Set bundleKeys = bundleMap.keySet();
578 try {
579 for (String key : aliases.keySet()) {
580 String targetKey = aliases.get(key);
581 if (bundleKeys.contains(targetKey)) {
582 bundleMap.putIfAbsent(key, bundleMap.get(targetKey));
583 }
584 }
585 } catch (Exception ex) {
586 Logger.getLogger(CLDRConverter.class.getName()).log(Level.SEVERE, null, ex);
587 }
588 }
589
590 /*
591 * Returns the language portion of the given id.
592 * If id is "root", "" is returned.
593 */
594 static String getLanguageCode(String id) {
595 return "root".equals(id) ? "" : Locale.forLanguageTag(id.replaceAll("_", "-")).getLanguage();
596 }
597
1141 Stream.concat(
1142 Stream.concat(
1143 Stream.of(
1144 "package sun.text.resources;",
1145 "public final class PluralRules {",
1146 " public static final String[][] rulesArray = {"
1147 ),
1148 pluralRulesStream().sorted()
1149 ),
1150 Stream.of(
1151 " };",
1152 "}"
1153 )
1154 )
1155 .collect(Collectors.toList()),
1156 StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
1157 }
1158
1159 private static Stream<String> pluralRulesStream() {
1160 return handlerPlurals.getData().entrySet().stream()
1161 .filter(e -> !((Map<String, String>)e.getValue()).isEmpty())
1162 .map(e -> {
1163 String loc = e.getKey();
1164 Map<String, String> rules = (Map<String, String>)e.getValue();
1165 return " {\"" + loc + "\", \"" +
1166 rules.entrySet().stream()
1167 .map(rule -> rule.getKey() + ":" + rule.getValue().replaceFirst("@.*", ""))
1168 .map(String::trim)
1169 .collect(Collectors.joining(";")) + "\"},";
1170 });
1171 }
1172
1173 // for debug
1174 static void dumpMap(Map<String, Object> map) {
1175 map.entrySet().stream()
1176 .sorted(Map.Entry.comparingByKey())
1177 .map(e -> {
1178 Object val = e.getValue();
1179 String valStr = null;
1180
1181 if (val instanceof String[]) {
1182 valStr = Arrays.asList((String[])val).toString();
1183 } else if (val != null) {
1184 valStr = val.toString();
|
417 }
418
419 // Parsers for data in "supplemental" directory
420 //
421 private static void parseSupplemental() throws Exception {
422 // Parse SupplementalData file and store the information in the HashMap
423 // Calendar information such as firstDay and minDay are stored in
424 // supplementalData.xml as of CLDR1.4. Individual territory is listed
425 // with its ISO 3166 country code while default is listed using UNM49
426 // region and composition numerical code (001 for World.)
427 //
428 // SupplementalData file also provides the "parent" locales which
429 // are othrwise not to be fallen back. Process them here as well.
430 //
431 handlerSuppl = new SupplementDataParseHandler();
432 parseLDMLFile(new File(SPPL_SOURCE_FILE), handlerSuppl);
433 Map<String, Object> parentData = handlerSuppl.getData("root");
434 parentData.keySet().stream()
435 .filter(key -> key.startsWith(PARENT_LOCALE_PREFIX))
436 .forEach(key -> {
437 parentLocalesMap.put(key, new TreeSet<String>(
438 Arrays.asList(((String)parentData.get(key)).split(" "))));
439 });
440
441 // Parse numberingSystems to get digit zero character information.
442 handlerNumbering = new NumberingSystemsParseHandler();
443 parseLDMLFile(new File(NUMBERING_SOURCE_FILE), handlerNumbering);
444
445 // Parse metaZones to create mappings between Olson tzids and CLDR meta zone names
446 handlerMetaZones = new MetaZonesParseHandler();
447 parseLDMLFile(new File(METAZONES_SOURCE_FILE), handlerMetaZones);
448
449 // Parse likelySubtags
450 handlerLikelySubtags = new LikelySubtagsParseHandler();
451 parseLDMLFile(new File(LIKELYSUBTAGS_SOURCE_FILE), handlerLikelySubtags);
452
453 // Parse supplementalMetadata
454 // Currently interested in deprecated time zone ids and language aliases.
455 handlerSupplMeta = new SupplementalMetadataParseHandler();
456 parseLDMLFile(new File(SPPL_META_SOURCE_FILE), handlerSupplMeta);
457
464 parseLDMLFile(new File(PLURALS_SOURCE_FILE), handlerPlurals);
465 }
466
467 // Parsers for data in "bcp47" directory
468 //
469 private static void parseBCP47() throws Exception {
470 // Parse timezone
471 handlerTimeZone = new TimeZoneParseHandler();
472 parseLDMLFile(new File(TIMEZONE_SOURCE_FILE), handlerTimeZone);
473
474 // canonical tz name map
475 // alias -> primary
476 handlerTimeZone.getData().forEach((k, v) -> {
477 String[] ids = ((String)v).split("\\s");
478 for (int i = 1; i < ids.length; i++) {
479 canonicalTZMap.put(ids[i], ids[0]);
480 }
481 });
482 }
483
484 private static void parseLDMLFile(File srcfile, AbstractLDMLHandler<?> handler) throws Exception {
485 info("..... Parsing " + srcfile.getName() + " .....");
486 SAXParserFactory pf = SAXParserFactory.newInstance();
487 pf.setValidating(true);
488 SAXParser parser = pf.newSAXParser();
489 enableFileAccess(parser);
490 parser.parse(srcfile, handler);
491 }
492
493 private static StringBuilder getCandLocales(Locale cldrLoc) {
494 List<Locale> candList = getCandidateLocales(cldrLoc);
495 StringBuilder sb = new StringBuilder();
496 for (Locale loc : candList) {
497 if (!loc.equals(Locale.ROOT)) {
498 sb.append(toLocaleName(loc.toLanguageTag()));
499 sb.append(",");
500 }
501 }
502 return sb;
503 }
504
557 if (bundleTypes.contains(Bundle.Type.FORMATDATA)) {
558 Map<String, Object> formatDataMap = extractFormatData(targetMap, bundle.getID());
559 if (!formatDataMap.isEmpty() || bundle.isRoot()) {
560 bundleGenerator.generateBundle("text", "FormatData", bundle.getJavaID(), true, formatDataMap, BundleType.PLAIN);
561 }
562 }
563
564 // For AvailableLocales
565 metaInfo.get("AvailableLocales").add(toLanguageTag(bundle.getID()));
566 addLikelySubtags(metaInfo, "AvailableLocales", bundle.getID());
567 }
568 bundleGenerator.generateMetaInfo(metaInfo);
569 }
570
571 static final Map<String, String> aliases = new HashMap<>();
572
573 /**
574 * Translate the aliases into the real entries in the bundle map.
575 */
576 static void handleAliases(Map<String, Object> bundleMap) {
577 Set<String> bundleKeys = bundleMap.keySet();
578 try {
579 for (String key : aliases.keySet()) {
580 String targetKey = aliases.get(key);
581 if (bundleKeys.contains(targetKey)) {
582 bundleMap.putIfAbsent(key, bundleMap.get(targetKey));
583 }
584 }
585 } catch (Exception ex) {
586 Logger.getLogger(CLDRConverter.class.getName()).log(Level.SEVERE, null, ex);
587 }
588 }
589
590 /*
591 * Returns the language portion of the given id.
592 * If id is "root", "" is returned.
593 */
594 static String getLanguageCode(String id) {
595 return "root".equals(id) ? "" : Locale.forLanguageTag(id.replaceAll("_", "-")).getLanguage();
596 }
597
1141 Stream.concat(
1142 Stream.concat(
1143 Stream.of(
1144 "package sun.text.resources;",
1145 "public final class PluralRules {",
1146 " public static final String[][] rulesArray = {"
1147 ),
1148 pluralRulesStream().sorted()
1149 ),
1150 Stream.of(
1151 " };",
1152 "}"
1153 )
1154 )
1155 .collect(Collectors.toList()),
1156 StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
1157 }
1158
1159 private static Stream<String> pluralRulesStream() {
1160 return handlerPlurals.getData().entrySet().stream()
1161 .filter(e -> !(e.getValue()).isEmpty())
1162 .map(e -> {
1163 String loc = e.getKey();
1164 Map<String, String> rules = e.getValue();
1165 return " {\"" + loc + "\", \"" +
1166 rules.entrySet().stream()
1167 .map(rule -> rule.getKey() + ":" + rule.getValue().replaceFirst("@.*", ""))
1168 .map(String::trim)
1169 .collect(Collectors.joining(";")) + "\"},";
1170 });
1171 }
1172
1173 // for debug
1174 static void dumpMap(Map<String, Object> map) {
1175 map.entrySet().stream()
1176 .sorted(Map.Entry.comparingByKey())
1177 .map(e -> {
1178 Object val = e.getValue();
1179 String valStr = null;
1180
1181 if (val instanceof String[]) {
1182 valStr = Arrays.asList((String[])val).toString();
1183 } else if (val != null) {
1184 valStr = val.toString();
|