001: /*
002: * @(#)Simple.java 1.6 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: package sun.tools.javazic;
028:
029: import java.io.BufferedWriter;
030: import java.io.File;
031: import java.io.FileWriter;
032: import java.io.IOException;
033: import java.util.ArrayList;
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.LinkedList;
037: import java.util.TreeMap;
038: import java.util.TreeSet;
039:
040: /**
041: * <code>Simple</code> generates TimeZoneData, which had been used as internal
042: * data of TimeZone before J2SDK1.3.
043: * Since J2SDK1.4 doesn't need TimeZoneData, this class is for maintenance
044: * of old JDK release.
045: */
046: class Simple extends BackEnd {
047:
048: /**
049: * Zone records which are applied for given year.
050: */
051: private static HashMap lastZoneRecs;
052:
053: /**
054: * Rule records which are applied for given year.
055: */
056: private static TreeMap lastRules;
057:
058: /**
059: * Sets last Rule records and Zone records for given timezone to each Map.
060: * @param tz Timezone object for each zone
061: * @return always 0
062: */
063: int processZoneinfo(Timezone tz) {
064: String zonename = tz.getName();
065:
066: if (lastRules == null) {
067: lastRules = new TreeMap();
068: lastZoneRecs = new HashMap();
069: }
070: lastRules.put(zonename, tz.getLastRules());
071: lastZoneRecs.put(zonename, tz.getLastZoneRec());
072:
073: return 0;
074: }
075:
076: /**
077: * Generates TimeZoneData to output SimpleTimeZone data.
078: * @param map Mappings object which is generated by {@link Main#compile Main.compile()}.
079: * @return 0 if no error occurred, otherwise 1.
080: */
081: int generateSrc(Mappings map) {
082: try {
083: String outputDir = Main.getOutputDir();
084: File outD = new File(outputDir);
085:
086: if (!outputDir.endsWith(File.separator)) {
087: outputDir += outD.separator;
088: }
089: outD.mkdirs();
090:
091: FileWriter fw = new FileWriter(outputDir
092: + "TimeZoneData.java", false);
093: BufferedWriter out = new BufferedWriter(fw);
094:
095: out.write("import java.util.SimpleTimeZone;\n\n");
096: out.write(" static SimpleTimeZone zones[] = {\n");
097:
098: TreeMap a = map.getAliases();
099: LinkedList roit = map.getRawOffsetsIndexTable();
100: int roit_size = roit.size();
101: for (int i = 0; i < roit_size; i++) {
102: TreeSet perRO = (TreeSet) roit.get(i);
103: Iterator keys = perRO.iterator();
104: String realname;
105: ArrayList stz;
106: ZoneRec zrec;
107:
108: while (keys.hasNext()) {
109: String key = (String) keys.next();
110:
111: if ((realname = (String) a.get(key)) != null) {
112: // if this alias is not targeted, ignore it.
113: if (!Zone.isTargetZone(key)) {
114: continue;
115: }
116: stz = (ArrayList) lastRules.get(realname);
117: zrec = (ZoneRec) lastZoneRecs.get(realname);
118: } else {
119: stz = (ArrayList) lastRules.get(key);
120: zrec = (ZoneRec) lastZoneRecs.get(key);
121: }
122:
123: out
124: .write("\t//--------------------------------------------------------------------\n");
125: int offset = ((Integer) map.getRawOffsetsIndex()
126: .get(i)).intValue();
127: String s = Time.toFormedString(offset);
128: out.write("\tnew SimpleTimeZone("
129: + Time.toFormedString(offset) + ", \""
130: + key + "\"");
131: if (realname != null) {
132: out.write(" /* " + realname + " */");
133: }
134:
135: if (stz == null) {
136: out.write("),\n");
137: } else {
138: RuleRec rr0 = (RuleRec) stz.get(0);
139: RuleRec rr1 = (RuleRec) stz.get(1);
140:
141: out
142: .write(",\n\t "
143: + Month.toString(rr0
144: .getMonthNum())
145: + ", "
146: + rr0
147: .getDay()
148: .getDayForSimpleTimeZone()
149: + ", "
150: + rr0
151: .getDay()
152: .getDayOfWeekForSimpleTimeZone()
153: + ", "
154: + Time.toFormedString((int) rr0
155: .getTime().getTime())
156: + ", "
157: + rr0
158: .getTime()
159: .getTypeForSimpleTimeZone()
160: + ",\n"
161: +
162:
163: "\t "
164: + Month.toString(rr1
165: .getMonthNum())
166: + ", "
167: + rr1
168: .getDay()
169: .getDayForSimpleTimeZone()
170: + ", "
171: + rr1
172: .getDay()
173: .getDayOfWeekForSimpleTimeZone()
174: + ", "
175: + Time.toFormedString((int) rr1
176: .getTime().getTime())
177: + ", "
178: + rr1
179: .getTime()
180: .getTypeForSimpleTimeZone()
181: + ",\n"
182: +
183:
184: "\t "
185: + Time.toFormedString(rr0
186: .getSave()) + "),\n");
187:
188: out.write("\t// " + rr0.getLine() + "\n");
189: out.write("\t// " + rr1.getLine() + "\n");
190: }
191:
192: String zline = zrec.getLine();
193: if (zline.indexOf("Zone") == -1) {
194: zline = "Zone " + key + "\t" + zline.trim();
195: }
196: out.write("\t// " + zline + "\n");
197: }
198: }
199: out.write(" };\n");
200:
201: out.close();
202: fw.close();
203: } catch (IOException e) {
204: Main.panic("IO error: " + e.getMessage());
205: return 1;
206: }
207:
208: return 0;
209: }
210: }
|