1 /*
   2  * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 import java.io.BufferedReader;
  27 import java.io.FileReader;
  28 import java.io.FileNotFoundException;
  29 import java.io.IOException;
  30 import java.util.ArrayList;
  31 import java.util.HashMap;
  32 import java.util.HashSet;
  33 import java.util.List;
  34 import java.util.Map;
  35 import java.util.Set;
  36 import java.util.StringTokenizer;
  37 
  38 /**
  39  * Zone holds information corresponding to a "Zone" part of a time
  40  * zone definition file.
  41  *
  42  * @since 1.4
  43  */
  44 class Zone {
  45     // zone name (e.g., "America/Los_Angeles")
  46     private String name;
  47 
  48     // zone records
  49     private List<ZoneRec> list;
  50 
  51     // target zone names for this compilation
  52     private static Set<String> targetZones;
  53 
  54     /**
  55      * Constructs a Zone with the specified zone name.
  56      * @param name the zone name
  57      */
  58     Zone(String name) {
  59         this.name = name;
  60         list = new ArrayList<ZoneRec>();
  61     }
  62 
  63     /**
  64      * Reads time zone names to be generated, called "target zone
  65      * name", from the specified text file and creats an internal hash
  66      * table to keep those names. It's assumed that one text line
  67      * contains a zone name or comments if it starts with
  68      * '#'. Comments can't follow a zone name in a single line.
  69      * @param fileName the text file name
  70      */
  71     static void readZoneNames(String fileName) {
  72         if (fileName == null) {
  73             return;
  74         }
  75         BufferedReader in = null;
  76         try {
  77             FileReader fr = new FileReader(fileName);
  78             in = new BufferedReader(fr);
  79         } catch (FileNotFoundException e) {
  80             Main.panic("can't open file: " + fileName);
  81         }
  82         targetZones = new HashSet<String>();
  83         String line;
  84 
  85         try {
  86             while ((line = in.readLine()) != null) {
  87                 line = line.trim();
  88                 if (line.length() == 0 || line.charAt(0) == '#') {
  89                     continue;
  90                 }
  91                 if (!targetZones.add(line)) {
  92                     Main.warning("duplicated target zone name: " + line);
  93                 }
  94             }
  95             in.close();
  96         } catch (IOException e) {
  97             Main.panic("IO error: "+e.getMessage());
  98         }
  99     }
 100 
 101     /**
 102      * Determines whether the specified zone is one of the target zones.
 103      * If no target zones are specified, this method always returns
 104      * true for any zone name.
 105      * @param zoneName the zone name
 106      * @return true if the specified name is a target zone.
 107      */
 108     static boolean isTargetZone(String zoneName) {
 109         if (targetZones == null) {
 110             return true;
 111         }
 112         return targetZones.contains(zoneName);
 113     }
 114 
 115     /**
 116      * Forces to add "MET" to the target zone table. This is because
 117      * there is a conflict between Java zone name "WET" and Olson zone
 118      * name.
 119      */
 120     static void addMET() {
 121         if (targetZones != null) {
 122             targetZones.add("MET");
 123         }
 124     }
 125 
 126     /**
 127      * @return the zone name
 128      */
 129     String getName() {
 130         return name;
 131     }
 132 
 133     /**
 134      * Adds the specified zone record to the zone record list.
 135      */
 136     void add(ZoneRec rec) {
 137         list.add(rec);
 138     }
 139 
 140     /**
 141      * @param index the index at which the zone record in the list is returned.
 142      * @return the zone record specified by the index.
 143      */
 144     ZoneRec get(int index) {
 145         return list.get(index);
 146     }
 147 
 148     /**
 149      * @return the size of the zone record list
 150      */
 151     int size() {
 152         return list.size();
 153     }
 154 
 155     /**
 156      * Resolves the reference to a rule in each zone record.
 157      * @param zi the Zoneinfo object with which the rule reference is
 158      * resolved.
 159      */
 160     void resolve(Zoneinfo zi) {
 161         for (int i = 0; i < list.size(); i++) {
 162             ZoneRec rec = list.get(i);
 163             rec.resolve(zi);
 164         }
 165     }
 166 }