LocaleData.java 11.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 */

/*
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
 *
 * The original version of this source code and documentation
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
 * subsidiary of IBM. These materials are provided under terms
 * of a License Agreement between Taligent and Sun. This technology
 * is protected by multiple US and International patents.
 *
 * This notice and attribution to Taligent may not be removed.
 * Taligent is a registered trademark of Taligent, Inc.
 *
 */

package sun.util.resources;

import java.security.AccessController;
import java.security.PrivilegedAction;
45
import java.util.Arrays;
D
duke 已提交
46 47 48
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
49
import java.util.MissingResourceException;
D
duke 已提交
50
import java.util.ResourceBundle;
51 52
import java.util.Set;
import sun.util.locale.provider.JRELocaleProviderAdapter;
53
import sun.util.locale.provider.LocaleDataMetaInfo;
54
import sun.util.locale.provider.LocaleProviderAdapter;
55
import static sun.util.locale.provider.LocaleProviderAdapter.Type.CLDR;
56
import static sun.util.locale.provider.LocaleProviderAdapter.Type.JRE;
D
duke 已提交
57 58 59

/**
 * Provides information about and access to resource bundles in the
60 61
 * sun.text.resources and sun.util.resources packages or in their corresponding
 * packages for CLDR.
D
duke 已提交
62 63 64 65 66 67
 *
 * @author Asmus Freytag
 * @author Mark Davis
 */

public class LocaleData {
68
    private final LocaleProviderAdapter.Type type;
D
duke 已提交
69

70 71
    public LocaleData(LocaleProviderAdapter.Type type) {
        this.type = type;
D
duke 已提交
72 73 74 75 76 77
    }

    /**
     * Gets a calendar data resource bundle, using privileges
     * to allow accessing a sun.* package.
     */
78 79
    public ResourceBundle getCalendarData(Locale locale) {
        return getBundle(type.getUtilResourcesPackage() + ".CalendarData", locale);
D
duke 已提交
80 81 82 83 84 85
    }

    /**
     * Gets a currency names resource bundle, using privileges
     * to allow accessing a sun.* package.
     */
86 87
    public OpenListResourceBundle getCurrencyNames(Locale locale) {
        return (OpenListResourceBundle) getBundle(type.getUtilResourcesPackage() + ".CurrencyNames", locale);
D
duke 已提交
88 89 90 91 92 93
    }

    /**
     * Gets a locale names resource bundle, using privileges
     * to allow accessing a sun.* package.
     */
94 95
    public OpenListResourceBundle getLocaleNames(Locale locale) {
        return (OpenListResourceBundle) getBundle(type.getUtilResourcesPackage() + ".LocaleNames", locale);
D
duke 已提交
96 97 98 99 100 101
    }

    /**
     * Gets a time zone names resource bundle, using privileges
     * to allow accessing a sun.* package.
     */
102 103
    public TimeZoneNamesBundle getTimeZoneNames(Locale locale) {
        return (TimeZoneNamesBundle) getBundle(type.getUtilResourcesPackage() + ".TimeZoneNames", locale);
D
duke 已提交
104
    }
N
naoto 已提交
105 106 107 108 109 110 111 112

    /**
     * Gets a break iterator info resource bundle, using privileges
     * to allow accessing a sun.* package.
     */
    public ResourceBundle getBreakIteratorInfo(Locale locale) {
        return getBundle(type.getTextResourcesPackage() + ".BreakIteratorInfo", locale);
    }
D
duke 已提交
113 114 115 116 117

    /**
     * Gets a collation data resource bundle, using privileges
     * to allow accessing a sun.* package.
     */
118 119
    public ResourceBundle getCollationData(Locale locale) {
        return getBundle(type.getTextResourcesPackage() + ".CollationData", locale);
D
duke 已提交
120 121 122 123 124 125
    }

    /**
     * Gets a date format data resource bundle, using privileges
     * to allow accessing a sun.* package.
     */
126 127
    public ResourceBundle getDateFormatData(Locale locale) {
        return getBundle(type.getTextResourcesPackage() + ".FormatData", locale);
D
duke 已提交
128 129
    }

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    public void setSupplementary(ParallelListResourceBundle formatData) {
        if (!formatData.areParallelContentsComplete()) {
            String suppName = type.getTextResourcesPackage() + ".JavaTimeSupplementary";
            setSupplementary(suppName, formatData);
        }
    }

    private boolean setSupplementary(String suppName, ParallelListResourceBundle formatData) {
        ParallelListResourceBundle parent = (ParallelListResourceBundle) formatData.getParent();
        boolean resetKeySet = false;
        if (parent != null) {
            resetKeySet = setSupplementary(suppName, parent);
        }
        OpenListResourceBundle supp = getSupplementary(suppName, formatData.getLocale());
        formatData.setParallelContents(supp);
        resetKeySet |= supp != null;
        // If any parents or this bundle has parallel data, reset keyset to create
        // a new keyset with the data.
        if (resetKeySet) {
            formatData.resetKeySet();
        }
        return resetKeySet;
    }

D
duke 已提交
154 155 156 157
    /**
     * Gets a number format data resource bundle, using privileges
     * to allow accessing a sun.* package.
     */
158 159
    public ResourceBundle getNumberFormatData(Locale locale) {
        return getBundle(type.getTextResourcesPackage() + ".FormatData", locale);
D
duke 已提交
160 161
    }

162
    public static ResourceBundle getBundle(final String baseName, final Locale locale) {
163
        return AccessController.doPrivileged(new PrivilegedAction<ResourceBundle>() {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
            @Override
            public ResourceBundle run() {
                return ResourceBundle
                        .getBundle(baseName, locale, LocaleDataResourceBundleControl.INSTANCE);
            }
        });
    }

    private static OpenListResourceBundle getSupplementary(final String baseName, final Locale locale) {
        return AccessController.doPrivileged(new PrivilegedAction<OpenListResourceBundle>() {
           @Override
           public OpenListResourceBundle run() {
               OpenListResourceBundle rb = null;
               try {
                   rb = (OpenListResourceBundle) ResourceBundle.getBundle(baseName,
                           locale, SupplementaryResourceBundleControl.INSTANCE);

               } catch (MissingResourceException e) {
                   // return null if no supplementary is available
               }
               return rb;
           }
        });
D
duke 已提交
187 188
    }

189
    private static class LocaleDataResourceBundleControl extends ResourceBundle.Control {
D
duke 已提交
190
        /* Singlton instance of ResourceBundle.Control. */
191
        private static final LocaleDataResourceBundleControl INSTANCE =
D
duke 已提交
192 193
            new LocaleDataResourceBundleControl();

194
        private LocaleDataResourceBundleControl() {
D
duke 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        }

        /*
         * This method overrides the default implementation to search
         * from a prebaked locale string list to determin the candidate
         * locale list.
         *
         * @param baseName the resource bundle base name.
         *        locale   the requested locale for the resource bundle.
         * @returns a list of candidate locales to search from.
         * @exception NullPointerException if baseName or locale is null.
         */
        @Override
         public List<Locale> getCandidateLocales(String baseName, Locale locale) {
            List<Locale> candidates = super.getCandidateLocales(baseName, locale);
210 211 212 213 214 215 216 217 218 219
            // Weed out Locales which are known to have no resource bundles
            int lastDot = baseName.lastIndexOf('.');
            String category = (lastDot >= 0) ? baseName.substring(lastDot + 1) : baseName;
            LocaleProviderAdapter.Type type = baseName.contains(DOTCLDR) ? CLDR : JRE;
            LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
            Set<String> langtags = ((JRELocaleProviderAdapter)adapter).getLanguageTagSet(category);
            if (!langtags.isEmpty()) {
                for (Iterator<Locale> itr = candidates.iterator(); itr.hasNext();) {
                    if (!LocaleProviderAdapter.isSupportedLocale(itr.next(), type, langtags)) {
                        itr.remove();
220
                    }
N
naoto 已提交
221
                }
222
            }
223

224 225
            // Force fallback to Locale.ENGLISH for CLDR time zone names support
            if (locale.getLanguage() != "en"
226
                    && type == CLDR && category.equals("TimeZoneNames")) {
227
                candidates.add(candidates.size() - 1, Locale.ENGLISH);
D
duke 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
            }
            return candidates;
        }

        /*
         * Overrides "getFallbackLocale" to return null so
         * that the fallback locale will be null.
         * @param baseName the resource bundle base name.
         *        locale   the requested locale for the resource bundle.
         * @return null for the fallback locale.
         * @exception NullPointerException if baseName or locale is null.
         */
        @Override
        public Locale getFallbackLocale(String baseName, Locale locale) {
            if (baseName == null || locale == null) {
                throw new NullPointerException();
            }
            return null;
        }

248
        private static final String DOTCLDR      = ".cldr";
D
duke 已提交
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        /**
         * Changes baseName to its per-language package name and
         * calls the super class implementation. For example,
         * if the baseName is "sun.text.resources.FormatData" and locale is ja_JP,
         * the baseName is changed to "sun.text.resources.ja.FormatData". If
         * baseName contains "cldr", such as "sun.text.resources.cldr.FormatData",
         * the name is changed to "sun.text.resources.cldr.jp.FormatData".
         */
        @Override
        public String toBundleName(String baseName, Locale locale) {
            String newBaseName = baseName;
            String lang = locale.getLanguage();
            if (lang.length() > 0) {
                if (baseName.startsWith(JRE.getUtilResourcesPackage())
                        || baseName.startsWith(JRE.getTextResourcesPackage())) {
                    // Assume the lengths are the same.
                    assert JRE.getUtilResourcesPackage().length()
                        == JRE.getTextResourcesPackage().length();
                    int index = JRE.getUtilResourcesPackage().length();
269 270
                    if (baseName.indexOf(DOTCLDR, index) > 0) {
                        index += DOTCLDR.length();
D
duke 已提交
271
                    }
272 273 274
                    newBaseName = baseName.substring(0, index + 1) + lang
                                      + baseName.substring(index);
                }
D
duke 已提交
275
            }
276
            return super.toBundleName(newBaseName, locale);
D
duke 已提交
277
        }
278 279 280 281 282 283 284 285
    }

    private static class SupplementaryResourceBundleControl extends LocaleDataResourceBundleControl {
        private static final SupplementaryResourceBundleControl INSTANCE =
                new SupplementaryResourceBundleControl();

        private SupplementaryResourceBundleControl() {
        }
D
duke 已提交
286

287 288 289 290 291 292 293 294 295 296 297
        @Override
        public List<Locale> getCandidateLocales(String baseName, Locale locale) {
            // Specifiy only the given locale
            return Arrays.asList(locale);
        }

        @Override
        public long getTimeToLive(String baseName, Locale locale) {
            assert baseName.contains("JavaTimeSupplementary");
            return TTL_DONT_CACHE;
        }
298
    }
D
duke 已提交
299
}