make/jdk/src/classes/build/tools/tzdb/TzdbZoneRulesCompiler.java
author herrick
Thu, 28 Mar 2019 13:49:38 -0400
branchJDK-8200758-branch
changeset 57292 7a683c461b80
parent 51789 0535b5a54b83
child 57544 99d2dd7b84a8
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
51789
0535b5a54b83 8209880: tzdb.dat is not reproducibly built
naoto
parents: 47216
diff changeset
     2
 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * Copyright (c) 2009-2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
package build.tools.tzdb;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
import java.io.ByteArrayOutputStream;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
import java.io.DataOutputStream;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    61
import java.nio.charset.StandardCharsets;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    62
import java.nio.file.Files;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    63
import java.nio.file.Path;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    64
import java.nio.file.Paths;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import java.text.ParsePosition;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import java.util.ArrayList;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
import java.util.Arrays;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import java.util.List;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import java.util.Map;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    70
import java.util.NoSuchElementException;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    71
import java.util.Scanner;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
import java.util.SortedMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import java.util.TreeMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
import java.util.regex.Matcher;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    75
import java.util.regex.MatchResult;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.util.regex.Pattern;
51789
0535b5a54b83 8209880: tzdb.dat is not reproducibly built
naoto
parents: 47216
diff changeset
    77
import java.util.stream.Collectors;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
/**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    80
 * A compiler that reads a set of TZDB time-zone files and builds a single
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    81
 * combined TZDB data file.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
public final class TzdbZoneRulesCompiler {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    87
    public static void main(String[] args) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    88
        new TzdbZoneRulesCompiler().compile(args);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    89
    }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    91
    private void compile(String[] args) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
        if (args.length < 2) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
            outputHelp();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
            return;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    96
        Path srcDir = null;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    97
        Path dstFile = null;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
        String version = null;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    99
        // parse args/options
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
        int i;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
        for (i = 0; i < args.length; i++) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
            String arg = args[i];
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   103
            if (!arg.startsWith("-")) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
                break;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
            if ("-srcdir".equals(arg)) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   107
                if (srcDir == null && ++i < args.length) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   108
                    srcDir = Paths.get(args[i]);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
                    continue;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
                }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   111
            } else if ("-dstfile".equals(arg)) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   112
                if (dstFile == null && ++i < args.length) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   113
                    dstFile = Paths.get(args[i]);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
                    continue;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
            } else if ("-verbose".equals(arg)) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   117
                if (!verbose) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
                    verbose = true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
                    continue;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
                }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   121
            } else if (!"-help".equals(arg)) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
                System.out.println("Unrecognised option: " + arg);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
            outputHelp();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
            return;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
        // check source directory
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   128
        if (srcDir == null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   129
            System.err.println("Source directory must be specified using -srcdir");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   130
            System.exit(1);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   132
        if (!Files.isDirectory(srcDir)) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   133
            System.err.println("Source does not exist or is not a directory: " + srcDir);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   134
            System.exit(1);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
        // parse source file names
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   137
        if (i == args.length) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   138
            i = 0;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   139
            args = new String[] {"africa", "antarctica", "asia", "australasia", "europe",
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   140
                                 "northamerica","southamerica", "backward", "etcetera" };
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   141
            System.out.println("Source filenames not specified, using default set ( ");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   142
            for (String name : args) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   143
                System.out.printf(name + " ");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   144
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   145
            System.out.println(")");
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   147
        // source files in this directory
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   148
        List<Path> srcFiles = new ArrayList<>();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   149
        for (; i < args.length; i++) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   150
            Path file = srcDir.resolve(args[i]);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   151
            if (Files.exists(file)) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   152
                srcFiles.add(file);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   153
            } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   154
                System.err.println("Source directory does not contain source file: " + args[i]);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   155
                System.exit(1);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   158
        // check destination file
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   159
        if (dstFile == null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   160
            dstFile = srcDir.resolve("tzdb.dat");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   161
        } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   162
            Path parent = dstFile.getParent();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   163
            if (parent != null && !Files.exists(parent)) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   164
                System.err.println("Destination directory does not exist: " + parent);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   165
                System.exit(1);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   166
            }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   168
        try {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   169
            // get tzdb source version
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   170
            Matcher m = Pattern.compile("tzdata(?<ver>[0-9]{4}[A-z])")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   171
                               .matcher(new String(Files.readAllBytes(srcDir.resolve("VERSION")),
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   172
                                                   "ISO-8859-1"));
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   173
            if (m.find()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   174
                version = m.group("ver");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   175
            } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   176
                System.exit(1);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   177
                System.err.println("Source directory does not contain file: VERSION");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   178
            }
25158
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   179
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   180
            // load source files
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   181
            printVerbose("Compiling TZDB version " + version);
25158
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   182
            TzdbZoneRulesProvider provider = new TzdbZoneRulesProvider(srcFiles);
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   183
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   184
            // build zone rules
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   185
            printVerbose("Building rules");
25158
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   186
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   187
            // Build the rules, zones and links into real zones.
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   188
            SortedMap<String, ZoneRules> builtZones = new TreeMap<>();
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   189
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   190
            // build zones
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   191
            for (String zoneId : provider.getZoneIds()) {
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   192
                printVerbose("Building zone " + zoneId);
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   193
                builtZones.put(zoneId, provider.getZoneRules(zoneId));
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   194
            }
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   195
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   196
            // build aliases
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   197
            Map<String, String> links = provider.getAliasMap();
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   198
            for (String aliasId : links.keySet()) {
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   199
                String realId = links.get(aliasId);
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   200
                printVerbose("Linking alias " + aliasId + " to " + realId);
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   201
                ZoneRules realRules = builtZones.get(realId);
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   202
                if (realRules == null) {
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   203
                    realId = links.get(realId);  // try again (handle alias liked to alias)
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   204
                    printVerbose("Relinking alias " + aliasId + " to " + realId);
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   205
                    realRules = builtZones.get(realId);
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   206
                    if (realRules == null) {
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   207
                        throw new IllegalArgumentException("Alias '" + aliasId + "' links to invalid zone '" + realId);
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   208
                    }
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   209
                    links.put(aliasId, realId);
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   210
                }
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   211
                builtZones.put(aliasId, realRules);
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   212
            }
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   213
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   214
            // output to file
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   215
            printVerbose("Outputting tzdb file: " + dstFile);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   216
            outputFile(dstFile, version, builtZones, links);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   217
        } catch (Exception ex) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   218
            System.out.println("Failed: " + ex.toString());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   219
            ex.printStackTrace();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   220
            System.exit(1);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
        System.exit(0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
     * Output usage text for the command line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
    private static void outputHelp() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
        System.out.println("Usage: TzdbZoneRulesCompiler <options> <tzdb source filenames>");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
        System.out.println("where options include:");
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   231
        System.out.println("   -srcdir  <directory>  Where to find tzdb source directory (required)");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   232
        System.out.println("   -dstfile <file>       Where to output generated file (default srcdir/tzdb.dat)");
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
        System.out.println("   -help                 Print this usage message");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
        System.out.println("   -verbose              Output verbose information during compilation");
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   235
        System.out.println(" The source directory must contain the unpacked tzdb files, such as asia or europe");
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
     * Outputs the file.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
     */
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   241
    private void outputFile(Path dstFile, String version,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   242
                            SortedMap<String, ZoneRules> builtZones,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   243
                            Map<String, String> links) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   244
        try (DataOutputStream out = new DataOutputStream(Files.newOutputStream(dstFile))) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
            // file version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
            out.writeByte(1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
            // group
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
            out.writeUTF("TZDB");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
            // versions
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   250
            out.writeShort(1);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   251
            out.writeUTF(version);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
            // regions
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   253
            String[] regionArray = builtZones.keySet().toArray(new String[builtZones.size()]);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
            out.writeShort(regionArray.length);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
            for (String regionId : regionArray) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
                out.writeUTF(regionId);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
            }
51789
0535b5a54b83 8209880: tzdb.dat is not reproducibly built
naoto
parents: 47216
diff changeset
   258
            // rules  -- remove the dup
0535b5a54b83 8209880: tzdb.dat is not reproducibly built
naoto
parents: 47216
diff changeset
   259
            List<ZoneRules> rulesList = builtZones.values().stream()
0535b5a54b83 8209880: tzdb.dat is not reproducibly built
naoto
parents: 47216
diff changeset
   260
                .distinct()
0535b5a54b83 8209880: tzdb.dat is not reproducibly built
naoto
parents: 47216
diff changeset
   261
                .collect(Collectors.toList());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
            out.writeShort(rulesList.size());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
            ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
            for (ZoneRules rules : rulesList) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
                baos.reset();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
                DataOutputStream dataos = new DataOutputStream(baos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
                rules.writeExternal(dataos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
                dataos.close();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
                byte[] bytes = baos.toByteArray();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
                out.writeShort(bytes.length);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
                out.write(bytes);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
            // link version-region-rules
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   274
            out.writeShort(builtZones.size());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   275
            for (Map.Entry<String, ZoneRules> entry : builtZones.entrySet()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   276
                 int regionIndex = Arrays.binarySearch(regionArray, entry.getKey());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   277
                 int rulesIndex = rulesList.indexOf(entry.getValue());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   278
                 out.writeShort(regionIndex);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   279
                 out.writeShort(rulesIndex);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   281
            // alias-region
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   282
            out.writeShort(links.size());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   283
            for (Map.Entry<String, String> entry : links.entrySet()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   284
                 int aliasIndex = Arrays.binarySearch(regionArray, entry.getKey());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   285
                 int regionIndex = Arrays.binarySearch(regionArray, entry.getValue());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   286
                 out.writeShort(aliasIndex);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   287
                 out.writeShort(regionIndex);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   288
            }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
            out.flush();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
        } catch (Exception ex) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
            System.out.println("Failed: " + ex.toString());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
            ex.printStackTrace();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
            System.exit(1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   297
    /** Whether to output verbose messages. */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   298
    private boolean verbose;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   301
     * private contructor
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
     */
25158
e76154c2c239 8042369: Remove duplicated java.time classes in build.tools.tzdb
sherman
parents: 21805
diff changeset
   303
    private TzdbZoneRulesCompiler() {}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
     * Prints a verbose message.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
     * @param message  the message, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
    private void printVerbose(String message) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
        if (verbose) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
            System.out.println(message);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
}