• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package com.android.timezone.version.tools;
17 
18 import com.android.i18n.timezone.TzDataSetVersion;
19 
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.OutputStream;
26 import java.io.Reader;
27 import java.util.Properties;
28 
29 /**
30  * A command-line tool for creating a time zone data set version file.
31  *
32  * <p>Args:
33  * <dl>
34  *     <dt>input properties file</dt>
35  * </dl>
36  *
37  * <p>The input properties file must have the entries:
38  * <dl>
39  *     <dt>rules.version</dt>
40  *     <dd>The IANA rules version.</dd>
41  *     <dt>revision</dt>
42  *     <dd>IANA data revision (typically 1).</dd>
43  *     <dt>output.version.file</dt>
44  *     <dd>The location to write the version file to.</dd>
45  * </dl>
46  *
47  * <p>The output consists of:
48  * <ul>
49  *     <li>A version file.</li>
50  * </ul>
51  */
52 public class CreateTzVersion {
53 
CreateTzVersion()54     private CreateTzVersion() {}
55 
main(String[] args)56     public static void main(String[] args) throws Exception {
57         if (args.length != 1) {
58             printUsage();
59             System.exit(1);
60         }
61         File f = new File(args[0]);
62         if (!f.exists()) {
63             System.err.println("Properties file " + f + " not found");
64             printUsage();
65             System.exit(2);
66         }
67         Properties properties = loadProperties(f);
68         String ianaRulesVersion = getMandatoryProperty(properties, "rules.version");
69         int revision = Integer.parseInt(getMandatoryProperty(properties, "revision"));
70 
71         // Create an object to hold version metadata for the tz data.
72         TzDataSetVersion tzDataSetVersion = new TzDataSetVersion(
73                 TzDataSetVersion.currentFormatMajorVersion(),
74                 TzDataSetVersion.currentFormatMinorVersion(),
75                 ianaRulesVersion,
76                 revision);
77         byte[] tzDataSetVersionBytes = tzDataSetVersion.toBytes();
78 
79         File outputVersionFile = new File(getMandatoryProperty(properties, "output.version.file"));
80 
81         // Write the tz data set version file.
82         try (OutputStream os = new FileOutputStream(outputVersionFile)) {
83             os.write(tzDataSetVersionBytes);
84         }
85         System.out.println("Wrote " + outputVersionFile);
86     }
87 
getMandatoryProperty(Properties p, String propertyName)88     private static String getMandatoryProperty(Properties p, String propertyName) {
89         String value = p.getProperty(propertyName);
90         if (value == null) {
91             System.out.println("Missing property: " + propertyName);
92             printUsage();
93             System.exit(3);
94         }
95         return value;
96     }
97 
loadProperties(File f)98     private static Properties loadProperties(File f) throws IOException {
99         Properties p = new Properties();
100         try (Reader reader = new InputStreamReader(new FileInputStream(f))) {
101             p.load(reader);
102         }
103         return p;
104     }
105 
printUsage()106     private static void printUsage() {
107         System.out.println("Usage:");
108         System.out.println("\t" + CreateTzVersion.class.getName() +
109                 " <tzupdate.properties file>");
110     }
111 }
112