0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.util;
0N/A
0N/Aimport java.lang.ref.SoftReference;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.LinkedList;
0N/Aimport java.util.List;
0N/Aimport java.util.Locale;
0N/Aimport java.util.Map;
0N/Aimport java.util.MissingResourceException;
0N/Aimport java.util.Set;
0N/Aimport java.util.concurrent.ConcurrentHashMap;
0N/Aimport java.util.spi.TimeZoneNameProvider;
0N/Aimport sun.util.calendar.ZoneInfo;
0N/Aimport sun.util.resources.LocaleData;
0N/Aimport sun.util.resources.OpenListResourceBundle;
0N/A
0N/A/**
0N/A * Utility class that deals with the localized time zone names
0N/A */
0N/Apublic final class TimeZoneNameUtility {
0N/A
0N/A /**
0N/A * cache to hold time zone resource bundles. Keyed by Locale
0N/A */
0N/A private static ConcurrentHashMap<Locale, SoftReference<OpenListResourceBundle>> cachedBundles =
0N/A new ConcurrentHashMap<Locale, SoftReference<OpenListResourceBundle>>();
0N/A
0N/A /**
0N/A * cache to hold time zone localized strings. Keyed by Locale
0N/A */
0N/A private static ConcurrentHashMap<Locale, SoftReference<String[][]>> cachedZoneData =
0N/A new ConcurrentHashMap<Locale, SoftReference<String[][]>>();
0N/A
0N/A /**
0N/A * get time zone localized strings. Enumerate all keys.
0N/A */
0N/A public static final String[][] getZoneStrings(Locale locale) {
0N/A String[][] zones;
0N/A SoftReference<String[][]> data = cachedZoneData.get(locale);
0N/A
0N/A if (data == null || ((zones = data.get()) == null)) {
0N/A zones = loadZoneStrings(locale);
0N/A data = new SoftReference<String[][]>(zones);
0N/A cachedZoneData.put(locale, data);
0N/A }
0N/A
0N/A return zones;
0N/A }
0N/A
0N/A private static final String[][] loadZoneStrings(Locale locale) {
0N/A List<String[]> zones = new LinkedList<String[]>();
0N/A OpenListResourceBundle rb = getBundle(locale);
0N/A Enumeration<String> keys = rb.getKeys();
0N/A String[] names = null;
0N/A
0N/A while(keys.hasMoreElements()) {
0N/A String key = keys.nextElement();
0N/A
0N/A names = retrieveDisplayNames(rb, key, locale);
0N/A if (names != null) {
0N/A zones.add(names);
0N/A }
0N/A }
0N/A
0N/A String[][] zonesArray = new String[zones.size()][];
0N/A return zones.toArray(zonesArray);
0N/A }
0N/A
0N/A /**
0N/A * Retrieve display names for a time zone ID.
0N/A */
0N/A public static final String[] retrieveDisplayNames(String id, Locale locale) {
0N/A OpenListResourceBundle rb = getBundle(locale);
0N/A return retrieveDisplayNames(rb, id, locale);
0N/A }
0N/A
0N/A private static final String[] retrieveDisplayNames(OpenListResourceBundle rb,
0N/A String id, Locale locale) {
0N/A LocaleServiceProviderPool pool =
0N/A LocaleServiceProviderPool.getPool(TimeZoneNameProvider.class);
0N/A String[] names = null;
0N/A
0N/A // Check whether a provider can provide an implementation that's closer
0N/A // to the requested locale than what the Java runtime itself can provide.
0N/A if (pool.hasProviders()) {
0N/A names = pool.getLocalizedObject(
0N/A TimeZoneNameGetter.INSTANCE,
0N/A locale, rb, id);
0N/A }
0N/A
0N/A if (names == null) {
0N/A try {
0N/A names = rb.getStringArray(id);
0N/A } catch (MissingResourceException mre) {
0N/A // fall through
0N/A }
0N/A }
0N/A
0N/A return names;
0N/A }
0N/A
0N/A private static final OpenListResourceBundle getBundle(Locale locale) {
0N/A OpenListResourceBundle rb;
0N/A SoftReference<OpenListResourceBundle> data = cachedBundles.get(locale);
0N/A
0N/A if (data == null || ((rb = data.get()) == null)) {
0N/A rb = LocaleData.getTimeZoneNames(locale);
0N/A data = new SoftReference<OpenListResourceBundle>(rb);
0N/A cachedBundles.put(locale, data);
0N/A }
0N/A
0N/A return rb;
0N/A }
0N/A
0N/A /**
0N/A * Obtains a localized time zone strings from a TimeZoneNameProvider
0N/A * implementation.
0N/A */
0N/A private static class TimeZoneNameGetter
0N/A implements LocaleServiceProviderPool.LocalizedObjectGetter<TimeZoneNameProvider,
0N/A String[]>{
0N/A private static final TimeZoneNameGetter INSTANCE =
0N/A new TimeZoneNameGetter();
0N/A
0N/A public String[] getObject(TimeZoneNameProvider timeZoneNameProvider,
0N/A Locale locale,
0N/A String requestID,
0N/A Object... params) {
0N/A assert params.length == 0;
0N/A String[] names = null;
0N/A String queryID = requestID;
0N/A
0N/A if (queryID.equals("GMT")) {
0N/A names = buildZoneStrings(timeZoneNameProvider, locale, queryID);
0N/A } else {
0N/A Map<String, String> aliases = ZoneInfo.getAliasTable();
0N/A
0N/A if (aliases != null) {
0N/A // Check whether this id is an alias, if so,
0N/A // look for the standard id.
0N/A if (aliases.containsKey(queryID)) {
0N/A String prevID = queryID;
0N/A while ((queryID = aliases.get(queryID)) != null) {
0N/A prevID = queryID;
0N/A }
0N/A queryID = prevID;
0N/A }
0N/A
0N/A names = buildZoneStrings(timeZoneNameProvider, locale, queryID);
0N/A
0N/A if (names == null) {
0N/A // There may be a case that a standard id has become an
0N/A // alias. so, check the aliases backward.
0N/A names = examineAliases(timeZoneNameProvider, locale,
0N/A queryID, aliases, aliases.entrySet());
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (names != null) {
0N/A names[0] = requestID;
0N/A }
0N/A
0N/A return names;
0N/A }
0N/A
0N/A private static String[] examineAliases(TimeZoneNameProvider tznp, Locale locale,
0N/A String id,
0N/A Map<String, String> aliases,
0N/A Set<Map.Entry<String, String>> aliasesSet) {
0N/A if (aliases.containsValue(id)) {
0N/A for (Map.Entry<String, String> entry : aliasesSet) {
0N/A if (entry.getValue().equals(id)) {
0N/A String alias = entry.getKey();
0N/A String[] names = buildZoneStrings(tznp, locale, alias);
0N/A if (names != null) {
0N/A return names;
0N/A } else {
0N/A names = examineAliases(tznp, locale, alias, aliases, aliasesSet);
0N/A if (names != null) {
0N/A return names;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A return null;
0N/A }
0N/A
0N/A private static String[] buildZoneStrings(TimeZoneNameProvider tznp,
0N/A Locale locale, String id) {
0N/A String[] names = new String[5];
0N/A
0N/A for (int i = 1; i <= 4; i ++) {
0N/A names[i] = tznp.getDisplayName(id, i>=3, i%2, locale);
0N/A if (i >= 3 && names[i] == null) {
0N/A names[i] = names[i-2];
0N/A }
0N/A }
0N/A
0N/A if (names[1] == null) {
0N/A // this id seems not localized by this provider
0N/A names = null;
0N/A }
0N/A
0N/A return names;
0N/A }
0N/A }
0N/A}