1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.timezone.location.tools; 18 19 import com.android.timezone.location.common.LicenseSupport; 20 import com.android.timezone.location.storage.tzs2range.TzS2Range; 21 import com.android.timezone.location.storage.tzs2range.TzS2RangeFileFormat; 22 import com.android.timezone.location.storage.tzs2range.write.TzS2RangeFileWriter; 23 import com.android.timezone.location.tools.proto.GeotzProtos; 24 25 import com.beust.jcommander.JCommander; 26 import com.beust.jcommander.Parameter; 27 import com.beust.jcommander.converters.FileConverter; 28 import com.google.protobuf.TextFormat; 29 30 import java.io.File; 31 import java.io.FileReader; 32 import java.util.Iterator; 33 import java.util.List; 34 35 /** Creates a TZ S2 file from a text proto file. */ 36 public final class CreateTzS2File { 37 38 39 private static class Arguments { 40 41 @Parameter(names = "--input-file", 42 description = "Proto file", 43 required = true, 44 converter = FileConverter.class) 45 public File inputFile; 46 47 @Parameter(names = "--s2-level", 48 description = "s2 level of input data", 49 required = true) 50 public int s2Level; 51 52 @Parameter(names = "--output-file", 53 description = "tz s2 file", 54 required = true, 55 converter = FileConverter.class) 56 public File outputFile; 57 58 } 59 60 /** 61 * Usage: 62 * CreateTzS2File <[input] proto file> <[input] s2 level of input data> <[output] tz s2 file> 63 * 64 * The proto file is defined in geotz_protos.proto. The data must be ordered correctly. 65 */ main(String[] args)66 public static void main(String[] args) throws Exception { 67 Arguments arguments = new Arguments(); 68 JCommander.newBuilder() 69 .addObject(arguments) 70 .build() 71 .parse(args); 72 File inputFile = arguments.inputFile; 73 int s2Level = arguments.s2Level; 74 File outputFile = arguments.outputFile; 75 76 // The input file is expected to be associated with a LICENSE file. Copy it to the output 77 // directory. 78 LicenseSupport.copyLicenseFile(inputFile.getParentFile(), outputFile.getParentFile()); 79 80 GeotzProtos.TimeZones timeZonesInput; 81 try (FileReader reader = new FileReader(inputFile)) { 82 GeotzProtos.TimeZones.Builder builder = GeotzProtos.TimeZones.newBuilder(); 83 TextFormat.getParser().merge(reader, builder); 84 timeZonesInput = builder.build(); 85 } 86 87 TzS2RangeFileFormat fileFormat = FileFormats.getFileFormatForLevel(s2Level); 88 try (TzS2RangeFileWriter writer = TzS2RangeFileWriter.open(outputFile, fileFormat)) { 89 List<GeotzProtos.TimeZoneIdSet> timeZoneIdSets = timeZonesInput.getTimeZoneIdSetsList(); 90 Iterator<TzS2Range> tzS2RangeIterator = timeZonesInput.getRangesList() 91 .stream() 92 .map(x -> createTzS2Range(timeZoneIdSets, x)) 93 .iterator(); 94 writer.processRanges(tzS2RangeIterator); 95 } 96 } 97 createTzS2Range(List<GeotzProtos.TimeZoneIdSet> tzIdStrings, GeotzProtos.S2Range inputRange)98 private static TzS2Range createTzS2Range(List<GeotzProtos.TimeZoneIdSet> tzIdStrings, 99 GeotzProtos.S2Range inputRange) { 100 GeotzProtos.TimeZoneIdSet timeZoneIdSet = 101 tzIdStrings.get(inputRange.getTimeZoneIdSetIndex()); 102 List<String> rangeStrings = timeZoneIdSet.getTimeZoneIdsList(); 103 return new TzS2Range(inputRange.getStartCellId(), inputRange.getEndCellId(), rangeStrings); 104 } 105 } 106