< prev index next >

make/jdk/src/classes/build/tools/tzdb/TzdbZoneRulesCompiler.java

Print this page
rev 51769 : imported patch 8209880
   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


  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package build.tools.tzdb;
  58 
  59 import java.io.ByteArrayOutputStream;
  60 import java.io.DataOutputStream;
  61 import java.nio.charset.StandardCharsets;
  62 import java.nio.file.Files;
  63 import java.nio.file.Path;
  64 import java.nio.file.Paths;
  65 import java.text.ParsePosition;
  66 import java.util.ArrayList;
  67 import java.util.Arrays;
  68 import java.util.HashMap;
  69 import java.util.HashSet;
  70 import java.util.List;
  71 import java.util.Map;
  72 import java.util.NoSuchElementException;
  73 import java.util.Scanner;
  74 import java.util.SortedMap;
  75 import java.util.TreeMap;
  76 import java.util.regex.Matcher;
  77 import java.util.regex.MatchResult;
  78 import java.util.regex.Pattern;

  79 
  80 /**
  81  * A compiler that reads a set of TZDB time-zone files and builds a single
  82  * combined TZDB data file.
  83  *
  84  * @since 1.8
  85  */
  86 public final class TzdbZoneRulesCompiler {
  87 
  88     public static void main(String[] args) {
  89         new TzdbZoneRulesCompiler().compile(args);
  90     }
  91 
  92     private void compile(String[] args) {
  93         if (args.length < 2) {
  94             outputHelp();
  95             return;
  96         }
  97         Path srcDir = null;
  98         Path dstFile = null;


 239     /**
 240      * Outputs the file.
 241      */
 242     private void outputFile(Path dstFile, String version,
 243                             SortedMap<String, ZoneRules> builtZones,
 244                             Map<String, String> links) {
 245         try (DataOutputStream out = new DataOutputStream(Files.newOutputStream(dstFile))) {
 246             // file version
 247             out.writeByte(1);
 248             // group
 249             out.writeUTF("TZDB");
 250             // versions
 251             out.writeShort(1);
 252             out.writeUTF(version);
 253             // regions
 254             String[] regionArray = builtZones.keySet().toArray(new String[builtZones.size()]);
 255             out.writeShort(regionArray.length);
 256             for (String regionId : regionArray) {
 257                 out.writeUTF(regionId);
 258             }
 259             // rules  -- hashset -> remove the dup
 260             List<ZoneRules> rulesList = new ArrayList<>(new HashSet<>(builtZones.values()));


 261             out.writeShort(rulesList.size());
 262             ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
 263             for (ZoneRules rules : rulesList) {
 264                 baos.reset();
 265                 DataOutputStream dataos = new DataOutputStream(baos);
 266                 rules.writeExternal(dataos);
 267                 dataos.close();
 268                 byte[] bytes = baos.toByteArray();
 269                 out.writeShort(bytes.length);
 270                 out.write(bytes);
 271             }
 272             // link version-region-rules
 273             out.writeShort(builtZones.size());
 274             for (Map.Entry<String, ZoneRules> entry : builtZones.entrySet()) {
 275                  int regionIndex = Arrays.binarySearch(regionArray, entry.getKey());
 276                  int rulesIndex = rulesList.indexOf(entry.getValue());
 277                  out.writeShort(regionIndex);
 278                  out.writeShort(rulesIndex);
 279             }
 280             // alias-region


   1 /*
   2  * Copyright (c) 2012, 2018, 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


  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package build.tools.tzdb;
  58 
  59 import java.io.ByteArrayOutputStream;
  60 import java.io.DataOutputStream;
  61 import java.nio.charset.StandardCharsets;
  62 import java.nio.file.Files;
  63 import java.nio.file.Path;
  64 import java.nio.file.Paths;
  65 import java.text.ParsePosition;
  66 import java.util.ArrayList;
  67 import java.util.Arrays;


  68 import java.util.List;
  69 import java.util.Map;
  70 import java.util.NoSuchElementException;
  71 import java.util.Scanner;
  72 import java.util.SortedMap;
  73 import java.util.TreeMap;
  74 import java.util.regex.Matcher;
  75 import java.util.regex.MatchResult;
  76 import java.util.regex.Pattern;
  77 import java.util.stream.Collectors;
  78 
  79 /**
  80  * A compiler that reads a set of TZDB time-zone files and builds a single
  81  * combined TZDB data file.
  82  *
  83  * @since 1.8
  84  */
  85 public final class TzdbZoneRulesCompiler {
  86 
  87     public static void main(String[] args) {
  88         new TzdbZoneRulesCompiler().compile(args);
  89     }
  90 
  91     private void compile(String[] args) {
  92         if (args.length < 2) {
  93             outputHelp();
  94             return;
  95         }
  96         Path srcDir = null;
  97         Path dstFile = null;


 238     /**
 239      * Outputs the file.
 240      */
 241     private void outputFile(Path dstFile, String version,
 242                             SortedMap<String, ZoneRules> builtZones,
 243                             Map<String, String> links) {
 244         try (DataOutputStream out = new DataOutputStream(Files.newOutputStream(dstFile))) {
 245             // file version
 246             out.writeByte(1);
 247             // group
 248             out.writeUTF("TZDB");
 249             // versions
 250             out.writeShort(1);
 251             out.writeUTF(version);
 252             // regions
 253             String[] regionArray = builtZones.keySet().toArray(new String[builtZones.size()]);
 254             out.writeShort(regionArray.length);
 255             for (String regionId : regionArray) {
 256                 out.writeUTF(regionId);
 257             }
 258             // rules  -- remove the dup
 259             List<ZoneRules> rulesList = builtZones.values().stream()
 260                 .distinct()
 261                 .collect(Collectors.toList());
 262             out.writeShort(rulesList.size());
 263             ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
 264             for (ZoneRules rules : rulesList) {
 265                 baos.reset();
 266                 DataOutputStream dataos = new DataOutputStream(baos);
 267                 rules.writeExternal(dataos);
 268                 dataos.close();
 269                 byte[] bytes = baos.toByteArray();
 270                 out.writeShort(bytes.length);
 271                 out.write(bytes);
 272             }
 273             // link version-region-rules
 274             out.writeShort(builtZones.size());
 275             for (Map.Entry<String, ZoneRules> entry : builtZones.entrySet()) {
 276                  int regionIndex = Arrays.binarySearch(regionArray, entry.getKey());
 277                  int rulesIndex = rulesList.indexOf(entry.getValue());
 278                  out.writeShort(regionIndex);
 279                  out.writeShort(rulesIndex);
 280             }
 281             // alias-region


< prev index next >