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.security.AccessController;
  29 import java.text.spi.BreakIteratorProvider;
  30 import java.text.spi.CollatorProvider;
  31 import java.text.spi.DateFormatProvider;
  32 import java.text.spi.DateFormatSymbolsProvider;
  33 import java.text.spi.DecimalFormatSymbolsProvider;
  34 import java.text.spi.NumberFormatProvider;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.Locale;
  38 import java.util.ResourceBundle;
  39 import java.util.Set;
  40 import java.util.spi.CalendarDataProvider;
  41 import java.util.spi.CurrencyNameProvider;
  42 import java.util.spi.LocaleNameProvider;
  43 import java.util.spi.LocaleServiceProvider;
  44 import java.util.spi.TimeZoneNameProvider;
  45 import sun.util.cldr.CLDRLocaleProviderAdapter;
  46 import sun.util.resources.LocaleData;
  47 
  48 /**
  49  * The LocaleProviderAdapter abstract class.
  50  *
  51  * @author Naoto Sato
  52  * @author Masayoshi Okutsu
  53  */
  54 public abstract class LocaleProviderAdapter {
  55     /**
  56      * Adapter type.
  57      */
  58     public static enum Type {
  59         JRE("sun.util.resources", "sun.text.resources"),
  60         CLDR("sun.util.resources.cldr", "sun.text.resources.cldr"),
  61         SPI,
  62         HOST,
  63         FALLBACK("sun.util.resources", "sun.text.resources");
  64 
  65         private final String UTIL_RESOURCES_PACKAGE;
  66         private final String TEXT_RESOURCES_PACKAGE;
  67 
  68         private Type() {
  69             this(null, null);
  70         }
  71 
  72         private Type(String util, String text) {
  73             UTIL_RESOURCES_PACKAGE = util;
  74             TEXT_RESOURCES_PACKAGE = text;
  75         }
  76 
  77         public String getUtilResourcesPackage() {
  78             return UTIL_RESOURCES_PACKAGE;
  79         }
  80 
  81         public String getTextResourcesPackage() {
  82             return TEXT_RESOURCES_PACKAGE;
  83         }
  84     }
  85 
  86     /**
  87      * LocaleProviderAdapter preference list. The default list is intended
  88      * to behave the same manner in JDK7.
  89      */
  90     private static Type[] adapterPreference = {
  91         Type.JRE,
  92         Type.SPI,
  93     };
  94 
  95     /**
  96      * JRE Locale Data Adapter instance
  97      */
  98     private static LocaleProviderAdapter jreLocaleProviderAdapter = new JRELocaleProviderAdapter();
  99 
 100     /**
 101      * SPI Locale Data Adapter instance
 102      */
 103     private static LocaleProviderAdapter spiLocaleProviderAdapter = new SPILocaleProviderAdapter();
 104 
 105     /**
 106      * CLDR Locale Data Adapter instance, if any.
 107      */
 108     private static LocaleProviderAdapter cldrLocaleProviderAdapter = null;
 109 
 110     /**
 111      * HOST Locale Data Adapter instance, if any.
 112      */
 113     private static LocaleProviderAdapter hostLocaleProviderAdapter = null;
 114 
 115     /**
 116      * FALLBACK Locale Data Adapter instance. It's basically the same with JRE, but only kicks
 117      * in for the root locale.
 118      */
 119     private static LocaleProviderAdapter fallbackLocaleProviderAdapter = null;
 120 
 121     static {
 122         String order = AccessController.doPrivileged(
 123                            new sun.security.action.GetPropertyAction("java.locale.providers"));
 124         // Override adapterPreference with the properties one
 125         if (order != null && order.length() != 0) {
 126             String[] types = order.split(",");
 127             List<Type> typeList = new ArrayList<>();
 128             for (String type : types) {
 129                 try {
 130                     Type aType = Type.valueOf(type.trim().toUpperCase(Locale.ROOT));
 131 
 132                     // load adapter if necessary
 133                     switch (aType) {
 134                         case CLDR:
 135                             cldrLocaleProviderAdapter = new CLDRLocaleProviderAdapter();
 136                             break;
 137                         case HOST:
 138                             hostLocaleProviderAdapter = new HostLocaleProviderAdapter();
 139                             break;
 140                     }
 141                     typeList.add(aType);
 142                 } catch (IllegalArgumentException | UnsupportedOperationException e) {
 143                     // could be caused by the user specifying wrong
 144                     // provider name or format in the system property
 145                     LocaleServiceProviderPool.config(LocaleProviderAdapter.class, e.toString());
 146                 }
 147             }
 148 
 149             if (!typeList.isEmpty()) {
 150                 if (!typeList.contains(Type.JRE)) {
 151                     // Append FALLBACK as the last resort.
 152                     fallbackLocaleProviderAdapter = new FallbackLocaleProviderAdapter();
 153                     typeList.add(Type.FALLBACK);
 154                 }
 155                 adapterPreference = typeList.toArray(new Type[0]);
 156             }
 157         }
 158     }
 159 
 160 
 161     /**
 162      * Returns the singleton instance for each adapter type
 163      */
 164     public static LocaleProviderAdapter forType(Type type) {
 165         switch (type) {
 166         case JRE:
 167             return jreLocaleProviderAdapter;
 168         case CLDR:
 169             return cldrLocaleProviderAdapter;
 170         case SPI:
 171             return spiLocaleProviderAdapter;
 172         case HOST:
 173             return hostLocaleProviderAdapter;
 174         case FALLBACK:
 175             return fallbackLocaleProviderAdapter;
 176         default:
 177             throw new InternalError("unknown locale data adapter type");
 178         }
 179     }
 180 
 181     public static LocaleProviderAdapter forJRE() {
 182         return jreLocaleProviderAdapter;
 183     }
 184 
 185     public static LocaleProviderAdapter getResourceBundleBased() {
 186         for (Type type : getAdapterPreference()) {
 187             if (type == Type.JRE || type == Type.CLDR || type == Type.FALLBACK) {
 188                 return forType(type);
 189             }
 190         }
 191         // Shouldn't happen.
 192         throw new InternalError();
 193     }
 194     /**
 195      * Returns the preference order of LocaleProviderAdapter.Type
 196      */
 197     public static Type[] getAdapterPreference() {
 198         return adapterPreference;
 199     }
 200 
 201     /**
 202      * Returns a LocaleProviderAdapter for the given locale service provider that
 203      * best matches the given locale. This method returns the LocaleProviderAdapter
 204      * for JRE if none is found for the given locale.
 205      *
 206      * @param providerClass the class for the locale service provider
 207      * @param locale the desired locale.
 208      * @return a LocaleProviderAdapter
 209      */
 210     public static LocaleProviderAdapter getAdapter(Class<? extends LocaleServiceProvider> providerClass,
 211                                                Locale locale) {
 212         // Fast look-up for the given locale
 213         LocaleProviderAdapter adapter = findAdapter(providerClass, locale);
 214         if (adapter != null) {
 215             return adapter;
 216         }
 217 
 218         // Try finding an adapter in the normal candidate locales path of the given locale.
 219         List<Locale> lookupLocales = ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_DEFAULT)
 220                                         .getCandidateLocales("", locale);
 221         for (Locale loc : lookupLocales) {
 222             if (loc.equals(locale)) {
 223                 // We've already done with this loc.
 224                 continue;
 225             }
 226             adapter = findAdapter(providerClass, loc);
 227             if (adapter != null) {
 228                 return adapter;
 229             }
 230         }
 231 
 232         // returns the adapter for FALLBACK as the last resort
 233         return fallbackLocaleProviderAdapter;
 234     }
 235 
 236     private static LocaleProviderAdapter findAdapter(Class<? extends LocaleServiceProvider> providerClass,
 237                                                  Locale locale) {
 238         for (Type type : getAdapterPreference()) {
 239             LocaleProviderAdapter adapter = forType(type);
 240             LocaleServiceProvider provider = adapter.getLocaleServiceProvider(providerClass);
 241             if (provider != null) {
 242                 if (provider.isSupportedLocale(locale)) {
 243                     return adapter;
 244                 }
 245             }
 246         }
 247         return null;
 248     }
 249 
 250     /**
 251      * A utility method for implementing the default LocaleServiceProvider.isSupportedLocale
 252      * for the JRE, CLDR, and FALLBACK adapters.
 253      */
 254     static boolean isSupportedLocale(Locale locale, LocaleProviderAdapter.Type type, Set<String> langtags) {
 255         assert type == Type.JRE || type == Type.CLDR || type == Type.FALLBACK;
 256         if (Locale.ROOT.equals(locale)) {
 257             return true;
 258         }
 259 
 260         if (type == Type.FALLBACK) {
 261             // no other locales except ROOT are supported for FALLBACK
 262             return false;
 263         }
 264 
 265         locale = locale.stripExtensions();
 266         if (langtags.contains(locale.toLanguageTag())) {
 267             return true;
 268         }
 269         if (type == Type.JRE) {
 270             String oldname = locale.toString().replace('_', '-');
 271             return langtags.contains(oldname);
 272         }
 273         return false;
 274     }
 275 
 276     public static Locale[] toLocaleArray(Set<String> tags) {
 277         Locale[] locs = new Locale[tags.size() + 1];
 278         int index = 0;
 279         locs[index++] = Locale.ROOT;
 280         for (String tag : tags) {
 281             switch (tag) {
 282             case "ja-JP-JP":
 283                 locs[index++] = JRELocaleConstants.JA_JP_JP;
 284                 break;
 285             case "th-TH-TH":
 286                 locs[index++] = JRELocaleConstants.TH_TH_TH;
 287                 break;
 288             default:
 289                 locs[index++] = Locale.forLanguageTag(tag);
 290                 break;
 291             }
 292         }
 293         return locs;
 294     }
 295 
 296     /**
 297      * Returns the type of this LocaleProviderAdapter
 298      */
 299     public abstract LocaleProviderAdapter.Type getAdapterType();
 300 
 301     /**
 302      * Getter method for Locale Service Providers.
 303      */
 304     public abstract <P extends LocaleServiceProvider> P getLocaleServiceProvider(Class<P> c);
 305 
 306     /**
 307      * Returns a BreakIteratorProvider for this LocaleProviderAdapter, or null if no
 308      * BreakIteratorProvider is available.
 309      *
 310      * @return a BreakIteratorProvider
 311      */
 312     public abstract BreakIteratorProvider getBreakIteratorProvider();
 313 
 314     /**
 315      * Returns a ollatorProvider for this LocaleProviderAdapter, or null if no
 316      * ollatorProvider is available.
 317      *
 318      * @return a ollatorProvider
 319      */
 320     public abstract CollatorProvider getCollatorProvider();
 321 
 322     /**
 323      * Returns a DateFormatProvider for this LocaleProviderAdapter, or null if no
 324      * DateFormatProvider is available.
 325      *
 326      * @return a DateFormatProvider
 327      */
 328     public abstract DateFormatProvider getDateFormatProvider();
 329 
 330     /**
 331      * Returns a DateFormatSymbolsProvider for this LocaleProviderAdapter, or null if no
 332      * DateFormatSymbolsProvider is available.
 333      *
 334      * @return a DateFormatSymbolsProvider
 335      */
 336     public abstract DateFormatSymbolsProvider getDateFormatSymbolsProvider();
 337 
 338     /**
 339      * Returns a DecimalFormatSymbolsProvider for this LocaleProviderAdapter, or null if no
 340      * DecimalFormatSymbolsProvider is available.
 341      *
 342      * @return a DecimalFormatSymbolsProvider
 343      */
 344     public abstract DecimalFormatSymbolsProvider getDecimalFormatSymbolsProvider();
 345 
 346     /**
 347      * Returns a NumberFormatProvider for this LocaleProviderAdapter, or null if no
 348      * NumberFormatProvider is available.
 349      *
 350      * @return a NumberFormatProvider
 351      */
 352     public abstract NumberFormatProvider getNumberFormatProvider();
 353 
 354     /*
 355      * Getter methods for java.util.spi.* providers
 356      */
 357 
 358     /**
 359      * Returns a CurrencyNameProvider for this LocaleProviderAdapter, or null if no
 360      * CurrencyNameProvider is available.
 361      *
 362      * @return a CurrencyNameProvider
 363      */
 364     public abstract CurrencyNameProvider getCurrencyNameProvider();
 365 
 366     /**
 367      * Returns a LocaleNameProvider for this LocaleProviderAdapter, or null if no
 368      * LocaleNameProvider is available.
 369      *
 370      * @return a LocaleNameProvider
 371      */
 372     public abstract LocaleNameProvider getLocaleNameProvider();
 373 
 374     /**
 375      * Returns a TimeZoneNameProvider for this LocaleProviderAdapter, or null if no
 376      * TimeZoneNameProvider is available.
 377      *
 378      * @return a TimeZoneNameProvider
 379      */
 380     public abstract TimeZoneNameProvider getTimeZoneNameProvider();
 381 
 382     /**
 383      * Returns a CalendarDataProvider for this LocaleProviderAdapter, or null if no
 384      * CalendarDataProvider is available.
 385      *
 386      * @return a CalendarDataProvider
 387      */
 388     public abstract CalendarDataProvider getCalendarDataProvider();
 389 
 390     public abstract LocaleResources getLocaleResources(Locale locale);
 391 
 392     public abstract LocaleData getLocaleData();
 393 
 394     public abstract Locale[] getAvailableLocales();
 395 }