1 /*
   2  * Copyright (c) 2012, 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 sun.util.locale.provider;
  27 
  28 import java.util.LinkedHashSet;
  29 import java.util.Locale;
  30 import java.util.Map;
  31 import java.util.Set;
  32 import java.util.TimeZone;
  33 import java.util.spi.TimeZoneNameProvider;
  34 import sun.util.calendar.ZoneInfo;
  35 import sun.util.resources.TimeZoneNamesBundle;
  36 
  37 /**
  38  * Concrete implementation of the
  39  * {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider} class
  40  * for the JRE LocaleProviderAdapter.
  41  *
  42  * @author Naoto Sato
  43  * @author Masayoshi Okutsu
  44  */
  45 public class TimeZoneNameProviderImpl extends TimeZoneNameProvider {
  46     private final LocaleProviderAdapter.Type type;
  47     private final Set<String> langtags;
  48 
  49     TimeZoneNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  50         this.type = type;
  51         this.langtags = langtags;
  52     }
  53 
  54     /**
  55      * Returns an array of all locales for which this locale service provider
  56      * can provide localized objects or names.
  57      *
  58      * @return An array of all locales for which this locale service provider
  59      * can provide localized objects or names.
  60      */
  61     @Override
  62     public Locale[] getAvailableLocales() {
  63         return LocaleProviderAdapter.toLocaleArray(langtags);
  64     }
  65 
  66     @Override
  67     public boolean isSupportedLocale(Locale locale) {
  68         return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
  69     }
  70 
  71     /**
  72      * Returns a name for the given time zone ID that's suitable for
  73      * presentation to the user in the specified locale. The given time
  74      * zone ID is "GMT" or one of the names defined using "Zone" entries
  75      * in the "tz database", a public domain time zone database at
  76      * <a href="ftp://elsie.nci.nih.gov/pub/">ftp://elsie.nci.nih.gov/pub/</a>.
  77      * The data of this database is contained in a file whose name starts with
  78      * "tzdata", and the specification of the data format is part of the zic.8
  79      * man page, which is contained in a file whose name starts with "tzcode".
  80      * <p>
  81      * If <code>daylight</code> is true, the method should return a name
  82      * appropriate for daylight saving time even if the specified time zone
  83      * has not observed daylight saving time in the past.
  84      *
  85      * @param ID a time zone ID string
  86      * @param daylight if true, return the daylight saving name.
  87      * @param style either {@link java.util.TimeZone#LONG TimeZone.LONG} or
  88      *    {@link java.util.TimeZone#SHORT TimeZone.SHORT}
  89      * @param locale the desired locale
  90      * @return the human-readable name of the given time zone in the
  91      *     given locale, or null if it's not available.
  92      * @exception IllegalArgumentException if <code>style</code> is invalid,
  93      *     or <code>locale</code> isn't one of the locales returned from
  94      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
  95      *     getAvailableLocales()}.
  96      * @exception NullPointerException if <code>ID</code> or <code>locale</code>
  97      *     is null
  98      * @see java.util.TimeZone#getDisplayName(boolean, int, java.util.Locale)
  99      */
 100     @Override
 101     public String getDisplayName(String id, boolean daylight, int style, Locale locale) {
 102         String[] names = getDisplayNameArray(id, 5, locale);
 103         if (names != null) {
 104             int index = daylight ? 3 : 1;
 105             if (style == TimeZone.SHORT) {
 106                 index++;
 107             }
 108             return names[index];
 109         }
 110         return null;
 111     }
 112 
 113     @Override
 114     public String getGenericDisplayName(String id, int style, Locale locale) {
 115         String[] names = getDisplayNameArray(id, 7, locale);
 116         if (names != null && names.length >= 7) {
 117             return names[(style == TimeZone.LONG) ? 5 : 6];
 118         }
 119         return null;
 120     }
 121 
 122     private String[] getDisplayNameArray(String id, int n, Locale locale) {
 123         if (id == null || locale == null) {
 124             throw new NullPointerException();
 125         }
 126         LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
 127         TimeZoneNamesBundle rb = adapter.getLocaleResources(locale).getTimeZoneNames();
 128         return rb.containsKey(id) ? rb.getStringArray(id, n) : null;
 129     }
 130 
 131     /**
 132      * Returns a String[][] as the DateFormatSymbols.getZoneStrings() value for
 133      * the given locale. This method is package private.
 134      *
 135      * @param locale a Locale for time zone names
 136      * @return an array of time zone names arrays
 137      */
 138     String[][] getZoneStrings(Locale locale) {
 139         LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
 140         TimeZoneNamesBundle rb = adapter.getLocaleResources(locale).getTimeZoneNames();
 141         Set<String> keyset = rb.keySet();
 142         // Use a LinkedHashSet to preseve the order
 143         Set<String[]> value = new LinkedHashSet<>();
 144         for (String key : keyset) {
 145             value.add(rb.getStringArray(key));
 146         }
 147 
 148         // Add aliases data for CLDR
 149         if (type == LocaleProviderAdapter.Type.CLDR) {
 150             // Note: TimeZoneNamesBundle creates a String[] on each getStringArray call.
 151             Map<String, String> aliases = ZoneInfo.getAliasTable();
 152             for (String alias : aliases.keySet()) {
 153                 if (!keyset.contains(alias)) {
 154                     String tzid = aliases.get(alias);
 155                     if (keyset.contains(tzid)) {
 156                         String[] val = rb.getStringArray(tzid);
 157                         val[0] = alias;
 158                         value.add(val);
 159                     }
 160                 }
 161             }
 162         }
 163         return value.toArray(new String[0][]);
 164     }
 165 }