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