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