< prev index next >

make/jdk/src/classes/build/tools/cldrconverter/CLDRConverter.java

Print this page
rev 49555 : [mq]: 8189784

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -26,20 +26,19 @@
 package build.tools.cldrconverter;
 
 import static build.tools.cldrconverter.Bundle.jreTimeZoneNames;
 import build.tools.cldrconverter.BundleGenerator.BundleType;
 import java.io.File;
-import java.nio.file.DirectoryStream;
-import java.nio.file.FileSystems;
-import java.nio.file.Files;
-import java.nio.file.Path;
+import java.nio.file.*;
+import java.time.*;
 import java.util.*;
 import java.util.ResourceBundle.Control;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
+import java.util.stream.Stream;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 import org.xml.sax.SAXNotRecognizedException;
 import org.xml.sax.SAXNotSupportedException;
 

@@ -54,16 +53,17 @@
     static final String LDML_DTD_SYSTEM_ID = "http://www.unicode.org/cldr/dtd/2.0/ldml.dtd";
     static final String SPPL_LDML_DTD_SYSTEM_ID = "http://www.unicode.org/cldr/dtd/2.0/ldmlSupplemental.dtd";
     static final String BCP47_LDML_DTD_SYSTEM_ID = "http://www.unicode.org/cldr/dtd/2.0/ldmlBCP47.dtd";
 
 
-    private static String CLDR_BASE = "../CLDR/21.0.1/";
+    private static String CLDR_BASE;
     static String LOCAL_LDML_DTD;
     static String LOCAL_SPPL_LDML_DTD;
     static String LOCAL_BCP47_LDML_DTD;
     private static String SOURCE_FILE_DIR;
     private static String SPPL_SOURCE_FILE;
+    private static String SPPL_META_SOURCE_FILE;
     private static String NUMBERING_SOURCE_FILE;
     private static String METAZONES_SOURCE_FILE;
     private static String LIKELYSUBTAGS_SOURCE_FILE;
     private static String TIMEZONE_SOURCE_FILE;
     static String DESTINATION_DIR = "build/gensrc";

@@ -83,10 +83,11 @@
     static final String ZONE_NAME_PREFIX = "timezone.displayname.";
     static final String METAZONE_ID_PREFIX = "metazone.id.";
     static final String PARENT_LOCALE_PREFIX = "parentLocale.";
 
     private static SupplementDataParseHandler handlerSuppl;
+    private static SupplementalMetadataParseHandler handlerSupplMeta;
     private static LikelySubtagsParseHandler handlerLikelySubtags;
     static NumberingSystemsParseHandler handlerNumbering;
     static MetaZonesParseHandler handlerMetaZones;
     static TimeZoneParseHandler handlerTimeZone;
     private static BundleGenerator bundleGenerator;

@@ -98,10 +99,13 @@
     // "parentLocales" map
     private static final Map<String, SortedSet<String>> parentLocalesMap = new HashMap<>();
     private static final ResourceBundle.Control defCon =
         ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_DEFAULT);
 
+    private static final String[] AVAILABLE_TZIDS = TimeZone.getAvailableIDs();
+    private static String zoneNameTempFile;
+
     static enum DraftType {
         UNCONFIRMED,
         PROVISIONAL,
         CONTRIBUTED,
         APPROVED;

@@ -193,10 +197,14 @@
 
                     case "-verbose":
                         verbose = true;
                         break;
 
+                    case "-zntempfile":
+                        zoneNameTempFile = args[++i];
+                        break;
+
                     case "-help":
                         usage();
                         System.exit(0);
                         break;
 

@@ -219,10 +227,11 @@
         SPPL_SOURCE_FILE = CLDR_BASE + "/supplemental/supplementalData.xml";
         LIKELYSUBTAGS_SOURCE_FILE = CLDR_BASE + "/supplemental/likelySubtags.xml";
         NUMBERING_SOURCE_FILE = CLDR_BASE + "/supplemental/numberingSystems.xml";
         METAZONES_SOURCE_FILE = CLDR_BASE + "/supplemental/metaZones.xml";
         TIMEZONE_SOURCE_FILE = CLDR_BASE + "/bcp47/timezone.xml";
+        SPPL_META_SOURCE_FILE = CLDR_BASE + "/supplemental/supplementalMetadata.xml";
 
         if (BASE_LOCALES.isEmpty()) {
             setupBaseLocales("en-US");
         }
 

@@ -232,10 +241,15 @@
         parseSupplemental();
         parseBCP47();
 
         List<Bundle> bundles = readBundleList();
         convertBundles(bundles);
+
+        // Generate java.time.format.ZoneName.java
+        if (isBaseModule) {
+            generateZoneName();
+        }
     }
 
     private static void usage() {
         errout("Usage: java CLDRConverter [options]%n"
                 + "\t-help          output this usage message and exit%n"

@@ -244,11 +258,11 @@
                 + "\t\t       draft level for using data (default: contributed)%n"
                 + "\t-base dir      base directory for CLDR input files%n"
                 + "\t-basemodule    generates bundles that go into java.base module%n"
                 + "\t-baselocales loc(,loc)*      locales that go into the base module%n"
                 + "\t-o dir         output directory (default: ./build/gensrc)%n"
-                + "\t-o dir         output directory (defaut: ./build/gensrc)%n"
+                + "\t-zntempfile    template file for java.time.format.ZoneName.java%n"
                 + "\t-utf8          use UTF-8 rather than \\uxxxx (for debug)%n");
     }
 
     static void info(String fmt, Object... args) {
         if (verbose) {

@@ -399,10 +413,15 @@
         parseLDMLFile(new File(METAZONES_SOURCE_FILE), handlerMetaZones);
 
         // Parse likelySubtags
         handlerLikelySubtags = new LikelySubtagsParseHandler();
         parseLDMLFile(new File(LIKELYSUBTAGS_SOURCE_FILE), handlerLikelySubtags);
+
+        // Parse supplementalMetadata
+        // Currently only interested in deprecated time zone ids.
+        handlerSupplMeta = new SupplementalMetadataParseHandler();
+        parseLDMLFile(new File(SPPL_META_SOURCE_FILE), handlerSupplMeta);
     }
 
     // Parsers for data in "bcp47" directory
     //
     private static void parseBCP47() throws Exception {

@@ -654,28 +673,32 @@
                     }
                 }
             });
         }
 
-        for (String tzid : handlerMetaZones.keySet()) {
-            String tzKey = TIMEZONE_ID_PREFIX + tzid;
-            Object data = map.get(tzKey);
+        Arrays.stream(AVAILABLE_TZIDS).forEach(tzid -> {
+            // If the tzid is deprecated, get the data for the replacement id
+            String tzKey = Optional.ofNullable((String)handlerSupplMeta.get(tzid))
+                                   .orElse(tzid);
+            Object data = map.get(TIMEZONE_ID_PREFIX + tzKey);   
+
             if (data instanceof String[]) {
                 names.put(tzid, data);
             } else {
-                String meta = handlerMetaZones.get(tzid);
+                String meta = handlerMetaZones.get(tzKey);
                 if (meta != null) {
                     String metaKey = METAZONE_ID_PREFIX + meta;
                     data = map.get(metaKey);
                     if (data instanceof String[]) {
                         // Keep the metazone prefix here.
                         names.put(metaKey, data);
                         names.put(tzid, meta);
                     }
                 }
             }
-        }
+        });
+
         return names;
     }
 
     /**
      * Extracts the language independent calendar data. Each of the two keys,

@@ -946,6 +969,46 @@
             }
         }
 
         return candidates;
     }
+
+    private static void generateZoneName() throws Exception {
+        Files.createDirectories(Paths.get(DESTINATION_DIR, "java", "time", "format"));
+        Files.write(Paths.get(DESTINATION_DIR, "java", "time", "format", "ZoneName.java"),
+            Files.lines(Paths.get(zoneNameTempFile))
+                .flatMap(l -> {
+                    if (l.equals("%%%%ZIDMAP%%%%")) {
+                        return zidMapEntry();
+                    } else if (l.equals("%%%%MZONEMAP%%%%")) {
+                        return handlerMetaZones.mzoneMapEntry();
+                    } else if (l.equals("%%%%DEPRECATED%%%%")) {
+                        return handlerSupplMeta.deprecatedMap();
+                    } else {
+                        return Stream.of(l);
+                    }
+                })
+                .collect(Collectors.toList()),
+            StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
+    }
+
+    private static Stream<String> zidMapEntry() {
+        Map<String, String> canonMap = new HashMap<>();
+        handlerTimeZone.getData().entrySet().stream()
+            .forEach(e -> {
+                String[] ids = ((String)e.getValue()).split("\\s");
+                for (int i = 1; i < ids.length; i++) {
+                    canonMap.put(ids[i], ids[0]);
+                }});
+        return ZoneId.getAvailableZoneIds().stream()
+                .map(id -> {
+                    String canonId = canonMap.getOrDefault(id, id);
+                    String meta = handlerMetaZones.get(canonId);
+                    String zone001 = handlerMetaZones.zidMap().get(meta);
+                    return zone001 == null ? "" :
+                            String.format("        \"%s\", \"%s\", \"%s\",",
+                                            id, meta, zone001);
+                })
+                .filter(s -> !s.isEmpty())
+                .sorted();
+    }
 }
< prev index next >