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