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.io.BufferedReader;
029: import java.io.FileReader;
030: import java.io.FileNotFoundException;
031: import java.io.IOException;
032: import java.util.ArrayList;
033: import java.util.HashMap;
034: import java.util.HashSet;
035: import java.util.List;
036: import java.util.Map;
037: import java.util.Set;
038: import java.util.StringTokenizer;
039:
040: /**
041: * Zone holds information corresponding to a "Zone" part of a time
042: * zone definition file.
043: *
044: * @since 1.4
045: */
046: class Zone {
047: // zone name (e.g., "America/Los_Angeles")
048: private String name;
049:
050: // zone records
051: private List<ZoneRec> list;
052:
053: // target zone names for this compilation
054: private static Set<String> targetZones;
055:
056: /**
057: * Constructs a Zone with the specified zone name.
058: * @param name the zone name
059: */
060: Zone(String name) {
061: this .name = name;
062: list = new ArrayList<ZoneRec>();
063: }
064:
065: /**
066: * Reads time zone names to be generated, called "target zone
067: * name", from the specified text file and creats an internal hash
068: * table to keep those names. It's assumed that one text line
069: * contains a zone name or comments if it starts with
070: * '#'. Comments can't follow a zone name in a single line.
071: * @param fileName the text file name
072: */
073: static void readZoneNames(String fileName) {
074: if (fileName == null) {
075: return;
076: }
077: BufferedReader in = null;
078: try {
079: FileReader fr = new FileReader(fileName);
080: in = new BufferedReader(fr);
081: } catch (FileNotFoundException e) {
082: Main.panic("can't open file: " + fileName);
083: }
084: targetZones = new HashSet<String>();
085: String line;
086:
087: try {
088: while ((line = in.readLine()) != null) {
089: line = line.trim();
090: if (line.length() == 0 || line.charAt(0) == '#') {
091: continue;
092: }
093: if (!targetZones.add(line)) {
094: Main
095: .warning("duplicated target zone name: "
096: + line);
097: }
098: }
099: in.close();
100: } catch (IOException e) {
101: Main.panic("IO error: " + e.getMessage());
102: }
103: }
104:
105: /**
106: * Determines whether the specified zone is one of the target zones.
107: * If no target zones are specified, this method always returns
108: * true for any zone name.
109: * @param zoneName the zone name
110: * @return true if the specified name is a target zone.
111: */
112: static boolean isTargetZone(String zoneName) {
113: if (targetZones == null) {
114: return true;
115: }
116: return targetZones.contains(zoneName);
117: }
118:
119: /**
120: * Forces to add "MET" to the target zone table. This is because
121: * there is a conflict between Java zone name "WET" and Olson zone
122: * name.
123: */
124: static void addMET() {
125: if (targetZones != null) {
126: targetZones.add("MET");
127: }
128: }
129:
130: /**
131: * @return the zone name
132: */
133: String getName() {
134: return name;
135: }
136:
137: /**
138: * Adds the specified zone record to the zone record list.
139: */
140: void add(ZoneRec rec) {
141: list.add(rec);
142: }
143:
144: /**
145: * @param index the index at which the zone record in the list is returned.
146: * @return the zone record specified by the index.
147: */
148: ZoneRec get(int index) {
149: return list.get(index);
150: }
151:
152: /**
153: * @return the size of the zone record list
154: */
155: int size() {
156: return list.size();
157: }
158:
159: /**
160: * Resolves the reference to a rule in each zone record.
161: * @param zi the Zoneinfo object with which the rule reference is
162: * resolved.
163: */
164: void resolve(Zoneinfo zi) {
165: for (int i = 0; i < list.size(); i++) {
166: ZoneRec rec = list.get(i);
167: rec.resolve(zi);
168: }
169: }
170: }
|