1 /*
   2  * Copyright (c) 2000, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.util.ArrayList;
  25 import java.util.List;
  26 
  27 /**
  28  * Main class for the javazic time zone data compiler.
  29  *
  30  * @since 1.4
  31  */
  32 public class Main {
  33 
  34     private static boolean verbose = false;
  35     static boolean outputDoc = false;
  36 
  37     private List<String> ziFiles = new ArrayList<String>();
  38     private static String zoneNamesFile = null;
  39     private static String versionName = "unknown";
  40     private static String outputDir = "zoneinfo";
  41     private static String mapFile = null;
  42 
  43     /**
  44      * Parses the specified arguments and sets up the variables.
  45      * @param argv the arguments
  46      */
  47     void processArgs(String[] argv) {
  48         for (int i = 0; i < argv.length; i++) {
  49             String arg = argv[i];
  50             if (arg.startsWith("-h")) {
  51                 usage();
  52                 System.exit(0);
  53             } else if (arg.equals("-d")) {
  54                 outputDir = argv[++i];
  55             } else if (arg.equals("-v")) {
  56                 verbose = true;
  57             } else if (arg.equals("-V")) {
  58                 versionName = argv[++i];
  59             } else if (arg.equals("-doc")) {
  60                 outputDoc = true;
  61             } else if (arg.equals("-map")) {
  62                 outputDoc = true;
  63                 mapFile = argv[++i];
  64             } else if (arg.equals("-f")) {
  65                 zoneNamesFile = argv[++i];
  66             } else if (arg.equals("-S")) {
  67                 try {
  68                     Zoneinfo.setYear(Integer.parseInt(argv[++i]));
  69                 } catch (Exception e) {
  70                     error("invalid year: " + argv[i]);
  71                     usage();
  72                     System.exit(1);
  73                 }
  74             } else {
  75                 boolean isStartYear = arg.equals("-s");
  76                 if (isStartYear || arg.equals("-e")) {
  77                     try {
  78                         int year = Integer.parseInt(argv[++i]);
  79                         if (isStartYear) {
  80                             Zoneinfo.setStartYear(year);
  81                         } else {
  82                             Zoneinfo.setEndYear(year);
  83                         }
  84                     } catch (Exception e) {
  85                         error("invalid year: " + argv[i]);
  86                         usage();
  87                         System.exit(1);
  88                     }
  89                 } else {
  90                     // the rest of args are zoneinfo source files
  91                     while (i < argv.length) {
  92                         ziFiles.add(argv[i++]);
  93                     }
  94                 }
  95             }
  96         }
  97     }
  98 
  99     /**
 100      * Parses zoneinfo source files
 101      */
 102     int compile() {
 103         int nFiles = ziFiles.size();
 104         int status = 0;
 105         Mappings maps = new Mappings();
 106         BackEnd backend = BackEnd.getBackEnd();
 107 
 108         for (int i = 0; i < nFiles; i++) {
 109             Zoneinfo frontend = Zoneinfo.parse(ziFiles.get(i));
 110 
 111             for (String key : frontend.getZones().keySet()) {
 112                 info(key);
 113 
 114                 Timezone tz = frontend.phase2(key);
 115                 status |= backend.processZoneinfo(tz);
 116             }
 117 
 118             maps.add(frontend);
 119         }
 120 
 121         // special code for dealing with the conflicting name "MET"
 122         Zone.addMET();
 123 
 124         maps.resolve();
 125 
 126         status |= backend.generateSrc(maps);
 127 
 128         return status;
 129     }
 130 
 131     public static void main(String[] argv) {
 132         Main zic = new Main();
 133 
 134         /*
 135          * Parse args
 136          */
 137         zic.processArgs(argv);
 138 
 139         /*
 140          * Read target zone names
 141          */
 142         if (zoneNamesFile != null) {
 143             Zone.readZoneNames(zoneNamesFile);
 144         }
 145 
 146         zic.compile();
 147     }
 148 
 149     void usage() {
 150         System.err.println("Usage: javazic [options] file...\n"+
 151                            "         -f namefile  file containing zone names\n"+
 152                            "                      to be generated (ie, generating subset)\n"+
 153                            "         -d dir       output directory\n"+
 154                            "         -v           verbose\n"+
 155                            "         -V datavers  specifies the tzdata version string\n"+
 156                            "                      (eg, \"tzdata2000g\")"+
 157                            "         -S year      output only SimleTimeZone data of that year\n"+
 158                            "         -s year      start year (default: 1900)\n"+
 159                            "         -e year      end year (default: 2037)\n"+
 160                            "         -doc         generates HTML documents\n"+
 161                            "         -map mapfile generates HTML documents with map information\n"+
 162                            "         file...      zoneinfo source file(s)");
 163     }
 164 
 165     /**
 166      * @return the output directory path name
 167      */
 168     static String getOutputDir() {
 169         return outputDir;
 170     }
 171 
 172     /**
 173      * @return the map file's path and name
 174      */
 175     static String getMapFile() {
 176         return mapFile;
 177     }
 178 
 179     /**
 180      * Returns the time zone data version string specified by the -V
 181      * option. If it is not specified, "unknown" is returned.
 182      * @return the time zone data version string
 183      */
 184     static String getVersionName() {
 185         return versionName;
 186     }
 187 
 188     /**
 189      * Prints out the specified fatal error message and calls {@link
 190      * java.lang.System#exit System.exit(1)}.
 191      * @param msg the fatal error message
 192      */
 193     static void panic(String msg) {
 194         printMessage("fatal error", msg);
 195         System.exit(1);
 196     }
 197 
 198     /**
 199      * Prints out the specified error message.
 200      * @param msg the error message
 201      */
 202     static void error(String msg) {
 203         printMessage("error", msg);
 204     }
 205 
 206     /**
 207      * Prints out the specified warning message.
 208      * @param msg the warning message
 209      */
 210     static void warning(String msg) {
 211         printMessage("warning", msg);
 212     }
 213 
 214     /**
 215      * Prints out the informative message.
 216      * @param msg the informative message
 217      */
 218     static void info(String msg) {
 219         if (verbose) {
 220             printMessage(null, msg);
 221         }
 222     }
 223 
 224     private static void printMessage(String type, String msg) {
 225         if (type != null) {
 226             type += ": ";
 227         } else {
 228             type = "";
 229         }
 230         System.err.println("javazic: " + type + msg);
 231     }
 232 }