jdk/make/tools/src/build/tools/javazic/Zone.java
changeset 16068 b2b3f6569eae
parent 15995 7313fd7a4823
parent 16067 36055e4b5305
child 16069 469ad49d6185
equal deleted inserted replaced
15995:7313fd7a4823 16068:b2b3f6569eae
     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 package build.tools.javazic;
       
    27 
       
    28 import java.io.BufferedReader;
       
    29 import java.io.FileReader;
       
    30 import java.io.FileNotFoundException;
       
    31 import java.io.IOException;
       
    32 import java.util.ArrayList;
       
    33 import java.util.HashMap;
       
    34 import java.util.HashSet;
       
    35 import java.util.List;
       
    36 import java.util.Map;
       
    37 import java.util.Set;
       
    38 import java.util.StringTokenizer;
       
    39 
       
    40 /**
       
    41  * Zone holds information corresponding to a "Zone" part of a time
       
    42  * zone definition file.
       
    43  *
       
    44  * @since 1.4
       
    45  */
       
    46 class Zone {
       
    47     // zone name (e.g., "America/Los_Angeles")
       
    48     private String name;
       
    49 
       
    50     // zone records
       
    51     private List<ZoneRec> list;
       
    52 
       
    53     // target zone names for this compilation
       
    54     private static Set<String> targetZones;
       
    55 
       
    56     /**
       
    57      * Constructs a Zone with the specified zone name.
       
    58      * @param name the zone name
       
    59      */
       
    60     Zone(String name) {
       
    61         this.name = name;
       
    62         list = new ArrayList<ZoneRec>();
       
    63     }
       
    64 
       
    65     /**
       
    66      * Reads time zone names to be generated, called "target zone
       
    67      * name", from the specified text file and creats an internal hash
       
    68      * table to keep those names. It's assumed that one text line
       
    69      * contains a zone name or comments if it starts with
       
    70      * '#'. Comments can't follow a zone name in a single line.
       
    71      * @param fileName the text file name
       
    72      */
       
    73     static void readZoneNames(String fileName) {
       
    74         if (fileName == null) {
       
    75             return;
       
    76         }
       
    77         BufferedReader in = null;
       
    78         try {
       
    79             FileReader fr = new FileReader(fileName);
       
    80             in = new BufferedReader(fr);
       
    81         } catch (FileNotFoundException e) {
       
    82             Main.panic("can't open file: " + fileName);
       
    83         }
       
    84         targetZones = new HashSet<String>();
       
    85         String line;
       
    86 
       
    87         try {
       
    88             while ((line = in.readLine()) != null) {
       
    89                 line = line.trim();
       
    90                 if (line.length() == 0 || line.charAt(0) == '#') {
       
    91                     continue;
       
    92                 }
       
    93                 if (!targetZones.add(line)) {
       
    94                     Main.warning("duplicated target zone name: " + line);
       
    95                 }
       
    96             }
       
    97             in.close();
       
    98         } catch (IOException e) {
       
    99             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 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 = list.get(i);
       
   165             rec.resolve(zi);
       
   166         }
       
   167     }
       
   168 }