001: /*
002: * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.javazic;
027:
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: /**
033: * Main class for the javazic time zone data compiler.
034: *
035: * @since 1.4
036: */
037: public class Main {
038:
039: private static boolean verbose = false;
040: static boolean outputDoc = false;
041:
042: private List<String> ziFiles = new ArrayList<String>();
043: private static String zoneNamesFile = null;
044: private static String versionName = "unknown";
045: private static String outputDir = "zoneinfo";
046: private static String mapFile = null;
047:
048: /**
049: * Parses the specified arguments and sets up the variables.
050: * @param argv the arguments
051: */
052: void processArgs(String[] argv) {
053: for (int i = 0; i < argv.length; i++) {
054: String arg = argv[i];
055: if (arg.startsWith("-h")) {
056: usage();
057: System.exit(0);
058: } else if (arg.equals("-d")) {
059: outputDir = argv[++i];
060: } else if (arg.equals("-v")) {
061: verbose = true;
062: } else if (arg.equals("-V")) {
063: versionName = argv[++i];
064: } else if (arg.equals("-doc")) {
065: outputDoc = true;
066: } else if (arg.equals("-map")) {
067: outputDoc = true;
068: mapFile = argv[++i];
069: } else if (arg.equals("-f")) {
070: zoneNamesFile = argv[++i];
071: } else if (arg.equals("-S")) {
072: try {
073: Zoneinfo.setYear(Integer.parseInt(argv[++i]));
074: } catch (Exception e) {
075: error("invalid year: " + argv[i]);
076: usage();
077: System.exit(1);
078: }
079: } else {
080: boolean isStartYear = arg.equals("-s");
081: if (isStartYear || arg.equals("-e")) {
082: try {
083: int year = Integer.parseInt(argv[++i]);
084: if (isStartYear) {
085: Zoneinfo.setStartYear(year);
086: } else {
087: Zoneinfo.setEndYear(year);
088: }
089: } catch (Exception e) {
090: error("invalid year: " + argv[i]);
091: usage();
092: System.exit(1);
093: }
094: } else {
095: // the rest of args are zoneinfo source files
096: while (i < argv.length) {
097: ziFiles.add(argv[i++]);
098: }
099: }
100: }
101: }
102: }
103:
104: /**
105: * Parses zoneinfo source files
106: */
107: int compile() {
108: int nFiles = ziFiles.size();
109: int status = 0;
110: Mappings maps = new Mappings();
111: BackEnd backend = BackEnd.getBackEnd();
112:
113: for (int i = 0; i < nFiles; i++) {
114: Zoneinfo frontend = Zoneinfo.parse(ziFiles.get(i));
115:
116: for (String key : frontend.getZones().keySet()) {
117: info(key);
118:
119: Timezone tz = frontend.phase2(key);
120: status |= backend.processZoneinfo(tz);
121: }
122:
123: maps.add(frontend);
124: }
125:
126: // special code for dealing with the conflicting name "MET"
127: Zone.addMET();
128:
129: maps.resolve();
130:
131: status |= backend.generateSrc(maps);
132:
133: return status;
134: }
135:
136: public static void main(String[] argv) {
137: Main zic = new Main();
138:
139: /*
140: * Parse args
141: */
142: zic.processArgs(argv);
143:
144: /*
145: * Read target zone names
146: */
147: if (zoneNamesFile != null) {
148: Zone.readZoneNames(zoneNamesFile);
149: }
150:
151: int status = zic.compile();
152:
153: System.exit(status);
154: }
155:
156: void usage() {
157: System.err
158: .println("Usage: javazic [options] file...\n"
159: + " -f namefile file containing zone names\n"
160: + " to be generated (ie, generating subset)\n"
161: + " -d dir output directory\n"
162: + " -v verbose\n"
163: + " -V datavers specifies the tzdata version string\n"
164: + " (eg, \"tzdata2000g\")"
165: + " -S year output only SimleTimeZone data of that year\n"
166: + " -s year start year (default: 1900)\n"
167: + " -e year end year (default: 2037)\n"
168: + " -doc generates HTML documents\n"
169: + " -map mapfile generates HTML documents with map information\n"
170: + " file... zoneinfo source file(s)");
171: }
172:
173: /**
174: * @return the output directory path name
175: */
176: static String getOutputDir() {
177: return outputDir;
178: }
179:
180: /**
181: * @return the map file's path and name
182: */
183: static String getMapFile() {
184: return mapFile;
185: }
186:
187: /**
188: * Returns the time zone data version string specified by the -V
189: * option. If it is not specified, "unknown" is returned.
190: * @return the time zone data version string
191: */
192: static String getVersionName() {
193: return versionName;
194: }
195:
196: /**
197: * Prints out the specified fatal error message and calls {@link
198: * java.lang.System#exit System.exit(1)}.
199: * @param msg the fatal error message
200: */
201: static void panic(String msg) {
202: printMessage("fatal error", msg);
203: System.exit(1);
204: }
205:
206: /**
207: * Prints out the specified error message.
208: * @param msg the error message
209: */
210: static void error(String msg) {
211: printMessage("error", msg);
212: }
213:
214: /**
215: * Prints out the specified warning message.
216: * @param msg the warning message
217: */
218: static void warning(String msg) {
219: printMessage("warning", msg);
220: }
221:
222: /**
223: * Prints out the informative message.
224: * @param msg the informative message
225: */
226: static void info(String msg) {
227: if (verbose) {
228: printMessage(null, msg);
229: }
230: }
231:
232: private static void printMessage(String type, String msg) {
233: if (type != null) {
234: type += ": ";
235: } else {
236: type = "";
237: }
238: System.err.println("javazic: " + type + msg);
239: }
240: }
|