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