1 /*
   2  * Copyright (c) 2000, 2018, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import  java.io.BufferedWriter;
  25 import  java.io.File;
  26 import  java.io.FileWriter;
  27 import  java.io.IOException;
  28 import  java.util.HashMap;
  29 import  java.util.List;
  30 import  java.util.Map;
  31 import  java.util.Set;
  32 import  java.util.SortedMap;
  33 import  java.util.TreeMap;
  34 import  java.util.TreeSet;
  35 
  36 /**
  37  * <code>Simple</code> generates TimeZoneData, which had been used as internal
  38  * data of TimeZone before J2SDK1.3.
  39  * Since J2SDK1.4 doesn't need TimeZoneData, this class is for maintenance
  40  * of old JDK release.
  41  */
  42 class Simple extends BackEnd {
  43 
  44     /**
  45      * Zone records which are applied for given year.
  46      */
  47     private static Map<String,ZoneRec> lastZoneRecs = new HashMap<>();
  48 
  49     /**
  50      * Rule records which are applied for given year.
  51      */
  52     private static Map<String,List<RuleRec>> lastRules = new TreeMap<>();
  53 
  54     /**
  55      * zone IDs sorted by their GMT offsets. If zone's GMT
  56      * offset will change in the future, its last known offset is
  57      * used.
  58      */
  59     private SortedMap<Integer, Set<String>> zonesByOffset = new TreeMap<>();
  60 
  61     /**
  62      * Sets last Rule records and Zone records for given timezone to
  63      * each Map.
  64      *
  65      * @param tz Timezone object for each zone
  66      * @return always 0
  67      */
  68     int processZoneinfo(Timezone tz) {
  69         String zonename = tz.getName();
  70 
  71         lastRules.put(zonename, tz.getLastRules());
  72         lastZoneRecs.put(zonename, tz.getLastZoneRec());
  73 
  74         // Populate zonesByOffset. (Zones that will change their
  75         // GMT offsets are also added to zonesByOffset here.)
  76         int lastKnownOffset = tz.getRawOffset();
  77         Set<String> set = zonesByOffset.get(lastKnownOffset);
  78         if (set == null) {
  79             set = new TreeSet<>();
  80             zonesByOffset.put(lastKnownOffset, set);
  81         }
  82         set.add(zonename);
  83 
  84         return 0;
  85     }
  86 
  87     /**
  88      * Generates TimeZoneData to output SimpleTimeZone data.
  89      * @param map Mappings object which is generated by {@link Main#compile}.
  90      * @return 0 if no error occurred, otherwise 1.
  91      */
  92     int generateSrc(Mappings map) {
  93         try {
  94             File outD = new File(Main.getOutputDir());
  95             outD.mkdirs();
  96 
  97             FileWriter fw =
  98                 new FileWriter(new File(outD, "TimeZoneData.java"), false);
  99             BufferedWriter out = new BufferedWriter(fw);
 100 
 101             out.write("import java.util.SimpleTimeZone;\n\n");
 102             out.write("    static SimpleTimeZone zones[] = {\n");
 103 
 104             Map<String,String> a = map.getAliases();
 105             List<Integer> roi = map.getRawOffsetsIndex();
 106             List<Set<String>> roit = map.getRawOffsetsIndexTable();
 107 
 108             int index = 0;
 109             for (int offset : zonesByOffset.keySet()) {
 110                 int o = roi.get(index);
 111                 Set<String> set = zonesByOffset.get(offset);
 112                 if (offset == o) {
 113                     // Merge aliases into zonesByOffset
 114                     set.addAll(roit.get(index));
 115                 }
 116                 index++;
 117 
 118                 for (String key : set) {
 119                     ZoneRec zrec;
 120                     String realname;
 121                     List<RuleRec> stz;
 122                     if ((realname = a.get(key)) != null) {
 123                         // if this alias is not targeted, ignore it.
 124                         if (!Zone.isTargetZone(key)) {
 125                             continue;
 126                         }
 127                         stz = lastRules.get(realname);
 128                         zrec = lastZoneRecs.get(realname);
 129                     } else {
 130                         stz = lastRules.get(key);
 131                         zrec = lastZoneRecs.get(key);
 132                     }
 133 
 134                     out.write("\t//--------------------------------------------------------------------\n");
 135                     String s = Time.toFormedString(offset);
 136                     out.write("\tnew SimpleTimeZone(" +
 137                         Time.toFormedString(offset) + ", \"" + key + "\"");
 138                     if (realname != null) {
 139                         out.write(" /* " + realname + " */");
 140                     }
 141 
 142                     if (stz == null) {
 143                         out.write("),\n");
 144                     } else {
 145                         RuleRec rr0 = stz.get(0);
 146                         RuleRec rr1 = stz.get(1);
 147 
 148                         out.write(",\n\t  " + Month.toString(rr0.getMonthNum()) +
 149                                   ", " + rr0.getDay().getDayForSimpleTimeZone() + ", " +
 150                                   rr0.getDay().getDayOfWeekForSimpleTimeZone() + ", " +
 151                                   Time.toFormedString((int)rr0.getTime().getTime()) + ", " +
 152                                   rr0.getTime().getTypeForSimpleTimeZone() + ",\n" +
 153 
 154                                   "\t  " + Month.toString(rr1.getMonthNum()) + ", " +
 155                                   rr1.getDay().getDayForSimpleTimeZone() + ", " +
 156                                   rr1.getDay().getDayOfWeekForSimpleTimeZone() + ", " +
 157                                   Time.toFormedString((int)rr1.getTime().getTime())+ ", " +
 158                                   rr1.getTime().getTypeForSimpleTimeZone() + ",\n" +
 159 
 160                                   "\t  " + Time.toFormedString(rr0.getSave()) + "),\n");
 161 
 162                         out.write("\t// " + rr0.getLine() + "\n");
 163                         out.write("\t// " + rr1.getLine() + "\n");
 164                     }
 165 
 166                     String zline = zrec.getLine();
 167                     if (zline.indexOf("Zone") == -1) {
 168                         zline = "Zone " + key + "\t" + zline.trim();
 169                     }
 170                     out.write("\t// " + zline + "\n");
 171                 }
 172             }
 173             out.write("    };\n");
 174 
 175             out.close();
 176             fw.close();
 177         } catch(IOException e) {
 178             Main.panic("IO error: "+e.getMessage());
 179             return 1;
 180         }
 181 
 182         return 0;
 183     }
 184 }