rt/emul/compact/src/main/java/java/util/ResourceBundle.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 03 Oct 2013 17:36:44 +0200
changeset 1337 c794024954b5
parent 1334 588d5bf7a560
permissions -rw-r--r--
Implementation of few more JDK classes
jtulach@1334
     1
/*
jtulach@1334
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
jtulach@1334
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1334
     4
 *
jtulach@1334
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@1334
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1334
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1334
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@1334
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1334
    10
 *
jtulach@1334
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1334
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1334
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1334
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1334
    15
 * accompanied this code).
jtulach@1334
    16
 *
jtulach@1334
    17
 * You should have received a copy of the GNU General Public License version
jtulach@1334
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1334
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1334
    20
 *
jtulach@1334
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1334
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@1334
    23
 * questions.
jtulach@1334
    24
 */
jtulach@1334
    25
jtulach@1334
    26
/*
jtulach@1334
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
jtulach@1334
    28
 * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
jtulach@1334
    29
 *
jtulach@1334
    30
 * The original version of this source code and documentation
jtulach@1334
    31
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
jtulach@1334
    32
 * subsidiary of IBM. These materials are provided under terms
jtulach@1334
    33
 * of a License Agreement between Taligent and Sun. This technology
jtulach@1334
    34
 * is protected by multiple US and International patents.
jtulach@1334
    35
 *
jtulach@1334
    36
 * This notice and attribution to Taligent may not be removed.
jtulach@1334
    37
 * Taligent is a registered trademark of Taligent, Inc.
jtulach@1334
    38
 *
jtulach@1334
    39
 */
jtulach@1334
    40
jtulach@1334
    41
package java.util;
jtulach@1334
    42
jtulach@1334
    43
import java.io.IOException;
jtulach@1334
    44
import java.io.InputStream;
jtulach@1334
    45
import java.lang.ref.ReferenceQueue;
jtulach@1334
    46
import java.lang.ref.SoftReference;
jtulach@1334
    47
import java.lang.ref.WeakReference;
jtulach@1334
    48
import java.net.URL;
jtulach@1334
    49
jtulach@1334
    50
jtulach@1334
    51
/**
jtulach@1334
    52
 *
jtulach@1334
    53
 * Resource bundles contain locale-specific objects.  When your program needs a
jtulach@1334
    54
 * locale-specific resource, a <code>String</code> for example, your program can
jtulach@1334
    55
 * load it from the resource bundle that is appropriate for the current user's
jtulach@1334
    56
 * locale. In this way, you can write program code that is largely independent
jtulach@1334
    57
 * of the user's locale isolating most, if not all, of the locale-specific
jtulach@1334
    58
 * information in resource bundles.
jtulach@1334
    59
 *
jtulach@1334
    60
 * <p>
jtulach@1334
    61
 * This allows you to write programs that can:
jtulach@1334
    62
 * <UL type=SQUARE>
jtulach@1334
    63
 * <LI> be easily localized, or translated, into different languages
jtulach@1334
    64
 * <LI> handle multiple locales at once
jtulach@1334
    65
 * <LI> be easily modified later to support even more locales
jtulach@1334
    66
 * </UL>
jtulach@1334
    67
 *
jtulach@1334
    68
 * <P>
jtulach@1334
    69
 * Resource bundles belong to families whose members share a common base
jtulach@1334
    70
 * name, but whose names also have additional components that identify
jtulach@1334
    71
 * their locales. For example, the base name of a family of resource
jtulach@1334
    72
 * bundles might be "MyResources". The family should have a default
jtulach@1334
    73
 * resource bundle which simply has the same name as its family -
jtulach@1334
    74
 * "MyResources" - and will be used as the bundle of last resort if a
jtulach@1334
    75
 * specific locale is not supported. The family can then provide as
jtulach@1334
    76
 * many locale-specific members as needed, for example a German one
jtulach@1334
    77
 * named "MyResources_de".
jtulach@1334
    78
 *
jtulach@1334
    79
 * <P>
jtulach@1334
    80
 * Each resource bundle in a family contains the same items, but the items have
jtulach@1334
    81
 * been translated for the locale represented by that resource bundle.
jtulach@1334
    82
 * For example, both "MyResources" and "MyResources_de" may have a
jtulach@1334
    83
 * <code>String</code> that's used on a button for canceling operations.
jtulach@1334
    84
 * In "MyResources" the <code>String</code> may contain "Cancel" and in
jtulach@1334
    85
 * "MyResources_de" it may contain "Abbrechen".
jtulach@1334
    86
 *
jtulach@1334
    87
 * <P>
jtulach@1334
    88
 * If there are different resources for different countries, you
jtulach@1334
    89
 * can make specializations: for example, "MyResources_de_CH" contains objects for
jtulach@1334
    90
 * the German language (de) in Switzerland (CH). If you want to only
jtulach@1334
    91
 * modify some of the resources
jtulach@1334
    92
 * in the specialization, you can do so.
jtulach@1334
    93
 *
jtulach@1334
    94
 * <P>
jtulach@1334
    95
 * When your program needs a locale-specific object, it loads
jtulach@1334
    96
 * the <code>ResourceBundle</code> class using the
jtulach@1334
    97
 * {@link #getBundle(java.lang.String, java.util.Locale) getBundle}
jtulach@1334
    98
 * method:
jtulach@1334
    99
 * <blockquote>
jtulach@1334
   100
 * <pre>
jtulach@1334
   101
 * ResourceBundle myResources =
jtulach@1334
   102
 *      ResourceBundle.getBundle("MyResources", currentLocale);
jtulach@1334
   103
 * </pre>
jtulach@1334
   104
 * </blockquote>
jtulach@1334
   105
 *
jtulach@1334
   106
 * <P>
jtulach@1334
   107
 * Resource bundles contain key/value pairs. The keys uniquely
jtulach@1334
   108
 * identify a locale-specific object in the bundle. Here's an
jtulach@1334
   109
 * example of a <code>ListResourceBundle</code> that contains
jtulach@1334
   110
 * two key/value pairs:
jtulach@1334
   111
 * <blockquote>
jtulach@1334
   112
 * <pre>
jtulach@1334
   113
 * public class MyResources extends ListResourceBundle {
jtulach@1334
   114
 *     protected Object[][] getContents() {
jtulach@1334
   115
 *         return new Object[][] {
jtulach@1334
   116
 *             // LOCALIZE THE SECOND STRING OF EACH ARRAY (e.g., "OK")
jtulach@1334
   117
 *             {"OkKey", "OK"},
jtulach@1334
   118
 *             {"CancelKey", "Cancel"},
jtulach@1334
   119
 *             // END OF MATERIAL TO LOCALIZE
jtulach@1334
   120
 *        };
jtulach@1334
   121
 *     }
jtulach@1334
   122
 * }
jtulach@1334
   123
 * </pre>
jtulach@1334
   124
 * </blockquote>
jtulach@1334
   125
 * Keys are always <code>String</code>s.
jtulach@1334
   126
 * In this example, the keys are "OkKey" and "CancelKey".
jtulach@1334
   127
 * In the above example, the values
jtulach@1334
   128
 * are also <code>String</code>s--"OK" and "Cancel"--but
jtulach@1334
   129
 * they don't have to be. The values can be any type of object.
jtulach@1334
   130
 *
jtulach@1334
   131
 * <P>
jtulach@1334
   132
 * You retrieve an object from resource bundle using the appropriate
jtulach@1334
   133
 * getter method. Because "OkKey" and "CancelKey"
jtulach@1334
   134
 * are both strings, you would use <code>getString</code> to retrieve them:
jtulach@1334
   135
 * <blockquote>
jtulach@1334
   136
 * <pre>
jtulach@1334
   137
 * button1 = new Button(myResources.getString("OkKey"));
jtulach@1334
   138
 * button2 = new Button(myResources.getString("CancelKey"));
jtulach@1334
   139
 * </pre>
jtulach@1334
   140
 * </blockquote>
jtulach@1334
   141
 * The getter methods all require the key as an argument and return
jtulach@1334
   142
 * the object if found. If the object is not found, the getter method
jtulach@1334
   143
 * throws a <code>MissingResourceException</code>.
jtulach@1334
   144
 *
jtulach@1334
   145
 * <P>
jtulach@1334
   146
 * Besides <code>getString</code>, <code>ResourceBundle</code> also provides
jtulach@1334
   147
 * a method for getting string arrays, <code>getStringArray</code>,
jtulach@1334
   148
 * as well as a generic <code>getObject</code> method for any other
jtulach@1334
   149
 * type of object. When using <code>getObject</code>, you'll
jtulach@1334
   150
 * have to cast the result to the appropriate type. For example:
jtulach@1334
   151
 * <blockquote>
jtulach@1334
   152
 * <pre>
jtulach@1334
   153
 * int[] myIntegers = (int[]) myResources.getObject("intList");
jtulach@1334
   154
 * </pre>
jtulach@1334
   155
 * </blockquote>
jtulach@1334
   156
 *
jtulach@1334
   157
 * <P>
jtulach@1334
   158
 * The Java Platform provides two subclasses of <code>ResourceBundle</code>,
jtulach@1334
   159
 * <code>ListResourceBundle</code> and <code>PropertyResourceBundle</code>,
jtulach@1334
   160
 * that provide a fairly simple way to create resources.
jtulach@1334
   161
 * As you saw briefly in a previous example, <code>ListResourceBundle</code>
jtulach@1334
   162
 * manages its resource as a list of key/value pairs.
jtulach@1334
   163
 * <code>PropertyResourceBundle</code> uses a properties file to manage
jtulach@1334
   164
 * its resources.
jtulach@1334
   165
 *
jtulach@1334
   166
 * <p>
jtulach@1334
   167
 * If <code>ListResourceBundle</code> or <code>PropertyResourceBundle</code>
jtulach@1334
   168
 * do not suit your needs, you can write your own <code>ResourceBundle</code>
jtulach@1334
   169
 * subclass.  Your subclasses must override two methods: <code>handleGetObject</code>
jtulach@1334
   170
 * and <code>getKeys()</code>.
jtulach@1334
   171
 *
jtulach@1334
   172
 * <h4>ResourceBundle.Control</h4>
jtulach@1334
   173
 *
jtulach@1334
   174
 * The {@link ResourceBundle.Control} class provides information necessary
jtulach@1334
   175
 * to perform the bundle loading process by the <code>getBundle</code>
jtulach@1334
   176
 * factory methods that take a <code>ResourceBundle.Control</code>
jtulach@1334
   177
 * instance. You can implement your own subclass in order to enable
jtulach@1334
   178
 * non-standard resource bundle formats, change the search strategy, or
jtulach@1334
   179
 * define caching parameters. Refer to the descriptions of the class and the
jtulach@1334
   180
 * {@link #getBundle(String, Locale, ClassLoader, Control) getBundle}
jtulach@1334
   181
 * factory method for details.
jtulach@1334
   182
 *
jtulach@1334
   183
 * <h4>Cache Management</h4>
jtulach@1334
   184
 *
jtulach@1334
   185
 * Resource bundle instances created by the <code>getBundle</code> factory
jtulach@1334
   186
 * methods are cached by default, and the factory methods return the same
jtulach@1334
   187
 * resource bundle instance multiple times if it has been
jtulach@1334
   188
 * cached. <code>getBundle</code> clients may clear the cache, manage the
jtulach@1334
   189
 * lifetime of cached resource bundle instances using time-to-live values,
jtulach@1334
   190
 * or specify not to cache resource bundle instances. Refer to the
jtulach@1334
   191
 * descriptions of the {@linkplain #getBundle(String, Locale, ClassLoader,
jtulach@1334
   192
 * Control) <code>getBundle</code> factory method}, {@link
jtulach@1334
   193
 * #clearCache(ClassLoader) clearCache}, {@link
jtulach@1334
   194
 * Control#getTimeToLive(String, Locale)
jtulach@1334
   195
 * ResourceBundle.Control.getTimeToLive}, and {@link
jtulach@1334
   196
 * Control#needsReload(String, Locale, String, ClassLoader, ResourceBundle,
jtulach@1334
   197
 * long) ResourceBundle.Control.needsReload} for details.
jtulach@1334
   198
 *
jtulach@1334
   199
 * <h4>Example</h4>
jtulach@1334
   200
 *
jtulach@1334
   201
 * The following is a very simple example of a <code>ResourceBundle</code>
jtulach@1334
   202
 * subclass, <code>MyResources</code>, that manages two resources (for a larger number of
jtulach@1334
   203
 * resources you would probably use a <code>Map</code>).
jtulach@1334
   204
 * Notice that you don't need to supply a value if
jtulach@1334
   205
 * a "parent-level" <code>ResourceBundle</code> handles the same
jtulach@1334
   206
 * key with the same value (as for the okKey below).
jtulach@1334
   207
 * <blockquote>
jtulach@1334
   208
 * <pre>
jtulach@1334
   209
 * // default (English language, United States)
jtulach@1334
   210
 * public class MyResources extends ResourceBundle {
jtulach@1334
   211
 *     public Object handleGetObject(String key) {
jtulach@1334
   212
 *         if (key.equals("okKey")) return "Ok";
jtulach@1334
   213
 *         if (key.equals("cancelKey")) return "Cancel";
jtulach@1334
   214
 *         return null;
jtulach@1334
   215
 *     }
jtulach@1334
   216
 *
jtulach@1334
   217
 *     public Enumeration&lt;String&gt; getKeys() {
jtulach@1334
   218
 *         return Collections.enumeration(keySet());
jtulach@1334
   219
 *     }
jtulach@1334
   220
 *
jtulach@1334
   221
 *     // Overrides handleKeySet() so that the getKeys() implementation
jtulach@1334
   222
 *     // can rely on the keySet() value.
jtulach@1334
   223
 *     protected Set&lt;String&gt; handleKeySet() {
jtulach@1334
   224
 *         return new HashSet&lt;String&gt;(Arrays.asList("okKey", "cancelKey"));
jtulach@1334
   225
 *     }
jtulach@1334
   226
 * }
jtulach@1334
   227
 *
jtulach@1334
   228
 * // German language
jtulach@1334
   229
 * public class MyResources_de extends MyResources {
jtulach@1334
   230
 *     public Object handleGetObject(String key) {
jtulach@1334
   231
 *         // don't need okKey, since parent level handles it.
jtulach@1334
   232
 *         if (key.equals("cancelKey")) return "Abbrechen";
jtulach@1334
   233
 *         return null;
jtulach@1334
   234
 *     }
jtulach@1334
   235
 *
jtulach@1334
   236
 *     protected Set&lt;String&gt; handleKeySet() {
jtulach@1334
   237
 *         return new HashSet&lt;String&gt;(Arrays.asList("cancelKey"));
jtulach@1334
   238
 *     }
jtulach@1334
   239
 * }
jtulach@1334
   240
 * </pre>
jtulach@1334
   241
 * </blockquote>
jtulach@1334
   242
 * You do not have to restrict yourself to using a single family of
jtulach@1334
   243
 * <code>ResourceBundle</code>s. For example, you could have a set of bundles for
jtulach@1334
   244
 * exception messages, <code>ExceptionResources</code>
jtulach@1334
   245
 * (<code>ExceptionResources_fr</code>, <code>ExceptionResources_de</code>, ...),
jtulach@1334
   246
 * and one for widgets, <code>WidgetResource</code> (<code>WidgetResources_fr</code>,
jtulach@1334
   247
 * <code>WidgetResources_de</code>, ...); breaking up the resources however you like.
jtulach@1334
   248
 *
jtulach@1334
   249
 * @see ListResourceBundle
jtulach@1334
   250
 * @see PropertyResourceBundle
jtulach@1334
   251
 * @see MissingResourceException
jtulach@1334
   252
 * @since JDK1.1
jtulach@1334
   253
 */
jtulach@1334
   254
public abstract class ResourceBundle {
jtulach@1334
   255
jtulach@1334
   256
    /** initial size of the bundle cache */
jtulach@1334
   257
    private static final int INITIAL_CACHE_SIZE = 32;
jtulach@1334
   258
jtulach@1334
   259
    /** constant indicating that no resource bundle exists */
jtulach@1334
   260
    private static final ResourceBundle NONEXISTENT_BUNDLE = new ResourceBundle() {
jtulach@1334
   261
            public Enumeration<String> getKeys() { return null; }
jtulach@1334
   262
            protected Object handleGetObject(String key) { return null; }
jtulach@1334
   263
            public String toString() { return "NONEXISTENT_BUNDLE"; }
jtulach@1334
   264
        };
jtulach@1334
   265
jtulach@1334
   266
jtulach@1334
   267
    /**
jtulach@1334
   268
     * The cache is a map from cache keys (with bundle base name, locale, and
jtulach@1334
   269
     * class loader) to either a resource bundle or NONEXISTENT_BUNDLE wrapped by a
jtulach@1334
   270
     * BundleReference.
jtulach@1334
   271
     *
jtulach@1334
   272
     * The cache is a ConcurrentMap, allowing the cache to be searched
jtulach@1334
   273
     * concurrently by multiple threads.  This will also allow the cache keys
jtulach@1334
   274
     * to be reclaimed along with the ClassLoaders they reference.
jtulach@1334
   275
     *
jtulach@1334
   276
     * This variable would be better named "cache", but we keep the old
jtulach@1334
   277
     * name for compatibility with some workarounds for bug 4212439.
jtulach@1334
   278
     */
jaroslav@1337
   279
    private static final Map<CacheKey, BundleReference> cacheList
jaroslav@1337
   280
        = new HashMap<>(INITIAL_CACHE_SIZE);
jtulach@1334
   281
jtulach@1334
   282
    /**
jtulach@1334
   283
     * Queue for reference objects referring to class loaders or bundles.
jtulach@1334
   284
     */
jtulach@1334
   285
    private static final ReferenceQueue referenceQueue = new ReferenceQueue();
jtulach@1334
   286
jtulach@1334
   287
    /**
jtulach@1334
   288
     * The parent bundle of this bundle.
jtulach@1334
   289
     * The parent bundle is searched by {@link #getObject getObject}
jtulach@1334
   290
     * when this bundle does not contain a particular resource.
jtulach@1334
   291
     */
jtulach@1334
   292
    protected ResourceBundle parent = null;
jtulach@1334
   293
jtulach@1334
   294
    /**
jtulach@1334
   295
     * The locale for this bundle.
jtulach@1334
   296
     */
jtulach@1334
   297
    private Locale locale = null;
jtulach@1334
   298
jtulach@1334
   299
    /**
jtulach@1334
   300
     * The base bundle name for this bundle.
jtulach@1334
   301
     */
jtulach@1334
   302
    private String name;
jtulach@1334
   303
jtulach@1334
   304
    /**
jtulach@1334
   305
     * The flag indicating this bundle has expired in the cache.
jtulach@1334
   306
     */
jtulach@1334
   307
    private volatile boolean expired;
jtulach@1334
   308
jtulach@1334
   309
    /**
jtulach@1334
   310
     * The back link to the cache key. null if this bundle isn't in
jtulach@1334
   311
     * the cache (yet) or has expired.
jtulach@1334
   312
     */
jtulach@1334
   313
    private volatile CacheKey cacheKey;
jtulach@1334
   314
jtulach@1334
   315
    /**
jtulach@1334
   316
     * A Set of the keys contained only in this ResourceBundle.
jtulach@1334
   317
     */
jtulach@1334
   318
    private volatile Set<String> keySet;
jtulach@1334
   319
jtulach@1334
   320
    /**
jtulach@1334
   321
     * Sole constructor.  (For invocation by subclass constructors, typically
jtulach@1334
   322
     * implicit.)
jtulach@1334
   323
     */
jtulach@1334
   324
    public ResourceBundle() {
jtulach@1334
   325
    }
jtulach@1334
   326
jtulach@1334
   327
    /**
jtulach@1334
   328
     * Gets a string for the given key from this resource bundle or one of its parents.
jtulach@1334
   329
     * Calling this method is equivalent to calling
jtulach@1334
   330
     * <blockquote>
jtulach@1334
   331
     * <code>(String) {@link #getObject(java.lang.String) getObject}(key)</code>.
jtulach@1334
   332
     * </blockquote>
jtulach@1334
   333
     *
jtulach@1334
   334
     * @param key the key for the desired string
jtulach@1334
   335
     * @exception NullPointerException if <code>key</code> is <code>null</code>
jtulach@1334
   336
     * @exception MissingResourceException if no object for the given key can be found
jtulach@1334
   337
     * @exception ClassCastException if the object found for the given key is not a string
jtulach@1334
   338
     * @return the string for the given key
jtulach@1334
   339
     */
jtulach@1334
   340
    public final String getString(String key) {
jtulach@1334
   341
        return (String) getObject(key);
jtulach@1334
   342
    }
jtulach@1334
   343
jtulach@1334
   344
    /**
jtulach@1334
   345
     * Gets a string array for the given key from this resource bundle or one of its parents.
jtulach@1334
   346
     * Calling this method is equivalent to calling
jtulach@1334
   347
     * <blockquote>
jtulach@1334
   348
     * <code>(String[]) {@link #getObject(java.lang.String) getObject}(key)</code>.
jtulach@1334
   349
     * </blockquote>
jtulach@1334
   350
     *
jtulach@1334
   351
     * @param key the key for the desired string array
jtulach@1334
   352
     * @exception NullPointerException if <code>key</code> is <code>null</code>
jtulach@1334
   353
     * @exception MissingResourceException if no object for the given key can be found
jtulach@1334
   354
     * @exception ClassCastException if the object found for the given key is not a string array
jtulach@1334
   355
     * @return the string array for the given key
jtulach@1334
   356
     */
jtulach@1334
   357
    public final String[] getStringArray(String key) {
jtulach@1334
   358
        return (String[]) getObject(key);
jtulach@1334
   359
    }
jtulach@1334
   360
jtulach@1334
   361
    /**
jtulach@1334
   362
     * Gets an object for the given key from this resource bundle or one of its parents.
jtulach@1334
   363
     * This method first tries to obtain the object from this resource bundle using
jtulach@1334
   364
     * {@link #handleGetObject(java.lang.String) handleGetObject}.
jtulach@1334
   365
     * If not successful, and the parent resource bundle is not null,
jtulach@1334
   366
     * it calls the parent's <code>getObject</code> method.
jtulach@1334
   367
     * If still not successful, it throws a MissingResourceException.
jtulach@1334
   368
     *
jtulach@1334
   369
     * @param key the key for the desired object
jtulach@1334
   370
     * @exception NullPointerException if <code>key</code> is <code>null</code>
jtulach@1334
   371
     * @exception MissingResourceException if no object for the given key can be found
jtulach@1334
   372
     * @return the object for the given key
jtulach@1334
   373
     */
jtulach@1334
   374
    public final Object getObject(String key) {
jtulach@1334
   375
        Object obj = handleGetObject(key);
jtulach@1334
   376
        if (obj == null) {
jtulach@1334
   377
            if (parent != null) {
jtulach@1334
   378
                obj = parent.getObject(key);
jtulach@1334
   379
            }
jtulach@1334
   380
            if (obj == null)
jtulach@1334
   381
                throw new MissingResourceException("Can't find resource for bundle "
jtulach@1334
   382
                                                   +this.getClass().getName()
jtulach@1334
   383
                                                   +", key "+key,
jtulach@1334
   384
                                                   this.getClass().getName(),
jtulach@1334
   385
                                                   key);
jtulach@1334
   386
        }
jtulach@1334
   387
        return obj;
jtulach@1334
   388
    }
jtulach@1334
   389
jtulach@1334
   390
    /**
jtulach@1334
   391
     * Returns the locale of this resource bundle. This method can be used after a
jtulach@1334
   392
     * call to getBundle() to determine whether the resource bundle returned really
jtulach@1334
   393
     * corresponds to the requested locale or is a fallback.
jtulach@1334
   394
     *
jtulach@1334
   395
     * @return the locale of this resource bundle
jtulach@1334
   396
     */
jtulach@1334
   397
    public Locale getLocale() {
jtulach@1334
   398
        return locale;
jtulach@1334
   399
    }
jtulach@1334
   400
jtulach@1334
   401
    /**
jtulach@1334
   402
     * Sets the parent bundle of this bundle.
jtulach@1334
   403
     * The parent bundle is searched by {@link #getObject getObject}
jtulach@1334
   404
     * when this bundle does not contain a particular resource.
jtulach@1334
   405
     *
jtulach@1334
   406
     * @param parent this bundle's parent bundle.
jtulach@1334
   407
     */
jtulach@1334
   408
    protected void setParent(ResourceBundle parent) {
jtulach@1334
   409
        assert parent != NONEXISTENT_BUNDLE;
jtulach@1334
   410
        this.parent = parent;
jtulach@1334
   411
    }
jtulach@1334
   412
jtulach@1334
   413
    /**
jtulach@1334
   414
     * Key used for cached resource bundles.  The key checks the base
jtulach@1334
   415
     * name, the locale, and the class loader to determine if the
jtulach@1334
   416
     * resource is a match to the requested one. The loader may be
jtulach@1334
   417
     * null, but the base name and the locale must have a non-null
jtulach@1334
   418
     * value.
jtulach@1334
   419
     */
jtulach@1334
   420
    private static final class CacheKey implements Cloneable {
jtulach@1334
   421
        // These three are the actual keys for lookup in Map.
jtulach@1334
   422
        private String name;
jtulach@1334
   423
        private Locale locale;
jtulach@1334
   424
jtulach@1334
   425
        // bundle format which is necessary for calling
jtulach@1334
   426
        // Control.needsReload().
jtulach@1334
   427
        private String format;
jtulach@1334
   428
jtulach@1334
   429
        // These time values are in CacheKey so that NONEXISTENT_BUNDLE
jtulach@1334
   430
        // doesn't need to be cloned for caching.
jtulach@1334
   431
jtulach@1334
   432
        // The time when the bundle has been loaded
jtulach@1334
   433
        private volatile long loadTime;
jtulach@1334
   434
jtulach@1334
   435
        // The time when the bundle expires in the cache, or either
jtulach@1334
   436
        // Control.TTL_DONT_CACHE or Control.TTL_NO_EXPIRATION_CONTROL.
jtulach@1334
   437
        private volatile long expirationTime;
jtulach@1334
   438
jtulach@1334
   439
        // Placeholder for an error report by a Throwable
jtulach@1334
   440
        private Throwable cause;
jtulach@1334
   441
jtulach@1334
   442
        // Hash code value cache to avoid recalculating the hash code
jtulach@1334
   443
        // of this instance.
jtulach@1334
   444
        private int hashCodeCache;
jtulach@1334
   445
jaroslav@1337
   446
        CacheKey(String baseName, Locale locale) {
jtulach@1334
   447
            this.name = baseName;
jtulach@1334
   448
            this.locale = locale;
jtulach@1334
   449
            calculateHashCode();
jtulach@1334
   450
        }
jtulach@1334
   451
jtulach@1334
   452
        String getName() {
jtulach@1334
   453
            return name;
jtulach@1334
   454
        }
jtulach@1334
   455
jtulach@1334
   456
        CacheKey setName(String baseName) {
jtulach@1334
   457
            if (!this.name.equals(baseName)) {
jtulach@1334
   458
                this.name = baseName;
jtulach@1334
   459
                calculateHashCode();
jtulach@1334
   460
            }
jtulach@1334
   461
            return this;
jtulach@1334
   462
        }
jtulach@1334
   463
jtulach@1334
   464
        Locale getLocale() {
jtulach@1334
   465
            return locale;
jtulach@1334
   466
        }
jtulach@1334
   467
jtulach@1334
   468
        CacheKey setLocale(Locale locale) {
jtulach@1334
   469
            if (!this.locale.equals(locale)) {
jtulach@1334
   470
                this.locale = locale;
jtulach@1334
   471
                calculateHashCode();
jtulach@1334
   472
            }
jtulach@1334
   473
            return this;
jtulach@1334
   474
        }
jtulach@1334
   475
jtulach@1334
   476
        public boolean equals(Object other) {
jtulach@1334
   477
            if (this == other) {
jtulach@1334
   478
                return true;
jtulach@1334
   479
            }
jtulach@1334
   480
            try {
jtulach@1334
   481
                final CacheKey otherEntry = (CacheKey)other;
jtulach@1334
   482
                //quick check to see if they are not equal
jtulach@1334
   483
                if (hashCodeCache != otherEntry.hashCodeCache) {
jtulach@1334
   484
                    return false;
jtulach@1334
   485
                }
jtulach@1334
   486
                //are the names the same?
jtulach@1334
   487
                if (!name.equals(otherEntry.name)) {
jtulach@1334
   488
                    return false;
jtulach@1334
   489
                }
jtulach@1334
   490
                // are the locales the same?
jtulach@1334
   491
                if (!locale.equals(otherEntry.locale)) {
jtulach@1334
   492
                    return false;
jtulach@1334
   493
                }
jaroslav@1337
   494
                return true;
jtulach@1334
   495
            } catch (NullPointerException e) {
jtulach@1334
   496
            } catch (ClassCastException e) {
jtulach@1334
   497
            }
jtulach@1334
   498
            return false;
jtulach@1334
   499
        }
jtulach@1334
   500
jtulach@1334
   501
        public int hashCode() {
jtulach@1334
   502
            return hashCodeCache;
jtulach@1334
   503
        }
jtulach@1334
   504
jtulach@1334
   505
        private void calculateHashCode() {
jtulach@1334
   506
            hashCodeCache = name.hashCode() << 3;
jtulach@1334
   507
            hashCodeCache ^= locale.hashCode();
jtulach@1334
   508
        }
jtulach@1334
   509
jtulach@1334
   510
        public Object clone() {
jtulach@1334
   511
            try {
jtulach@1334
   512
                CacheKey clone = (CacheKey) super.clone();
jtulach@1334
   513
                // Clear the reference to a Throwable
jtulach@1334
   514
                clone.cause = null;
jtulach@1334
   515
                return clone;
jtulach@1334
   516
            } catch (CloneNotSupportedException e) {
jtulach@1334
   517
                //this should never happen
jtulach@1334
   518
                throw new InternalError();
jtulach@1334
   519
            }
jtulach@1334
   520
        }
jtulach@1334
   521
jtulach@1334
   522
        String getFormat() {
jtulach@1334
   523
            return format;
jtulach@1334
   524
        }
jtulach@1334
   525
jtulach@1334
   526
        void setFormat(String format) {
jtulach@1334
   527
            this.format = format;
jtulach@1334
   528
        }
jtulach@1334
   529
jtulach@1334
   530
        private void setCause(Throwable cause) {
jtulach@1334
   531
            if (this.cause == null) {
jtulach@1334
   532
                this.cause = cause;
jtulach@1334
   533
            } else {
jtulach@1334
   534
                // Override the cause if the previous one is
jtulach@1334
   535
                // ClassNotFoundException.
jtulach@1334
   536
                if (this.cause instanceof ClassNotFoundException) {
jtulach@1334
   537
                    this.cause = cause;
jtulach@1334
   538
                }
jtulach@1334
   539
            }
jtulach@1334
   540
        }
jtulach@1334
   541
jtulach@1334
   542
        private Throwable getCause() {
jtulach@1334
   543
            return cause;
jtulach@1334
   544
        }
jtulach@1334
   545
jtulach@1334
   546
        public String toString() {
jtulach@1334
   547
            String l = locale.toString();
jtulach@1334
   548
            if (l.length() == 0) {
jtulach@1334
   549
                if (locale.getVariant().length() != 0) {
jtulach@1334
   550
                    l = "__" + locale.getVariant();
jtulach@1334
   551
                } else {
jtulach@1334
   552
                    l = "\"\"";
jtulach@1334
   553
                }
jtulach@1334
   554
            }
jaroslav@1337
   555
            return "CacheKey[" + name + ", lc=" + l
jtulach@1334
   556
                + "(format=" + format + ")]";
jtulach@1334
   557
        }
jtulach@1334
   558
    }
jtulach@1334
   559
jtulach@1334
   560
    /**
jtulach@1334
   561
     * The common interface to get a CacheKey in LoaderReference and
jtulach@1334
   562
     * BundleReference.
jtulach@1334
   563
     */
jtulach@1334
   564
    private static interface CacheKeyReference {
jtulach@1334
   565
        public CacheKey getCacheKey();
jtulach@1334
   566
    }
jtulach@1334
   567
jtulach@1334
   568
    /**
jtulach@1334
   569
     * References to bundles are soft references so that they can be garbage
jtulach@1334
   570
     * collected when they have no hard references.
jtulach@1334
   571
     */
jtulach@1334
   572
    private static final class BundleReference extends SoftReference<ResourceBundle>
jtulach@1334
   573
                                               implements CacheKeyReference {
jtulach@1334
   574
        private CacheKey cacheKey;
jtulach@1334
   575
jtulach@1334
   576
        BundleReference(ResourceBundle referent, ReferenceQueue q, CacheKey key) {
jtulach@1334
   577
            super(referent, q);
jtulach@1334
   578
            cacheKey = key;
jtulach@1334
   579
        }
jtulach@1334
   580
jtulach@1334
   581
        public CacheKey getCacheKey() {
jtulach@1334
   582
            return cacheKey;
jtulach@1334
   583
        }
jtulach@1334
   584
    }
jtulach@1334
   585
jtulach@1334
   586
    /**
jtulach@1334
   587
     * Gets a resource bundle using the specified base name, the default locale,
jtulach@1334
   588
     * and the caller's class loader. Calling this method is equivalent to calling
jtulach@1334
   589
     * <blockquote>
jtulach@1334
   590
     * <code>getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader())</code>,
jtulach@1334
   591
     * </blockquote>
jtulach@1334
   592
     * except that <code>getClassLoader()</code> is run with the security
jtulach@1334
   593
     * privileges of <code>ResourceBundle</code>.
jtulach@1334
   594
     * See {@link #getBundle(String, Locale, ClassLoader) getBundle}
jtulach@1334
   595
     * for a complete description of the search and instantiation strategy.
jtulach@1334
   596
     *
jtulach@1334
   597
     * @param baseName the base name of the resource bundle, a fully qualified class name
jtulach@1334
   598
     * @exception java.lang.NullPointerException
jtulach@1334
   599
     *     if <code>baseName</code> is <code>null</code>
jtulach@1334
   600
     * @exception MissingResourceException
jtulach@1334
   601
     *     if no resource bundle for the specified base name can be found
jtulach@1334
   602
     * @return a resource bundle for the given base name and the default locale
jtulach@1334
   603
     */
jtulach@1334
   604
    public static final ResourceBundle getBundle(String baseName)
jtulach@1334
   605
    {
jtulach@1334
   606
        return getBundleImpl(baseName, Locale.getDefault(),
jtulach@1334
   607
                             Control.INSTANCE);
jtulach@1334
   608
    }
jtulach@1334
   609
jtulach@1334
   610
    /**
jtulach@1334
   611
     * Returns a resource bundle using the specified base name, the
jtulach@1334
   612
     * default locale and the specified control. Calling this method
jtulach@1334
   613
     * is equivalent to calling
jtulach@1334
   614
     * <pre>
jtulach@1334
   615
     * getBundle(baseName, Locale.getDefault(),
jtulach@1334
   616
     *           this.getClass().getClassLoader(), control),
jtulach@1334
   617
     * </pre>
jtulach@1334
   618
     * except that <code>getClassLoader()</code> is run with the security
jtulach@1334
   619
     * privileges of <code>ResourceBundle</code>.  See {@link
jtulach@1334
   620
     * #getBundle(String, Locale, ClassLoader, Control) getBundle} for the
jtulach@1334
   621
     * complete description of the resource bundle loading process with a
jtulach@1334
   622
     * <code>ResourceBundle.Control</code>.
jtulach@1334
   623
     *
jtulach@1334
   624
     * @param baseName
jtulach@1334
   625
     *        the base name of the resource bundle, a fully qualified class
jtulach@1334
   626
     *        name
jtulach@1334
   627
     * @param control
jtulach@1334
   628
     *        the control which gives information for the resource bundle
jtulach@1334
   629
     *        loading process
jtulach@1334
   630
     * @return a resource bundle for the given base name and the default
jtulach@1334
   631
     *        locale
jtulach@1334
   632
     * @exception NullPointerException
jtulach@1334
   633
     *        if <code>baseName</code> or <code>control</code> is
jtulach@1334
   634
     *        <code>null</code>
jtulach@1334
   635
     * @exception MissingResourceException
jtulach@1334
   636
     *        if no resource bundle for the specified base name can be found
jtulach@1334
   637
     * @exception IllegalArgumentException
jtulach@1334
   638
     *        if the given <code>control</code> doesn't perform properly
jtulach@1334
   639
     *        (e.g., <code>control.getCandidateLocales</code> returns null.)
jtulach@1334
   640
     *        Note that validation of <code>control</code> is performed as
jtulach@1334
   641
     *        needed.
jtulach@1334
   642
     * @since 1.6
jtulach@1334
   643
     */
jtulach@1334
   644
    public static final ResourceBundle getBundle(String baseName,
jtulach@1334
   645
                                                 Control control) {
jtulach@1334
   646
        return getBundleImpl(baseName, Locale.getDefault(),
jtulach@1334
   647
                             /* must determine loader here, else we break stack invariant */
jtulach@1334
   648
                             control);
jtulach@1334
   649
    }
jtulach@1334
   650
jtulach@1334
   651
    /**
jtulach@1334
   652
     * Gets a resource bundle using the specified base name and locale,
jtulach@1334
   653
     * and the caller's class loader. Calling this method is equivalent to calling
jtulach@1334
   654
     * <blockquote>
jtulach@1334
   655
     * <code>getBundle(baseName, locale, this.getClass().getClassLoader())</code>,
jtulach@1334
   656
     * </blockquote>
jtulach@1334
   657
     * except that <code>getClassLoader()</code> is run with the security
jtulach@1334
   658
     * privileges of <code>ResourceBundle</code>.
jtulach@1334
   659
     * See {@link #getBundle(String, Locale, ClassLoader) getBundle}
jtulach@1334
   660
     * for a complete description of the search and instantiation strategy.
jtulach@1334
   661
     *
jtulach@1334
   662
     * @param baseName
jtulach@1334
   663
     *        the base name of the resource bundle, a fully qualified class name
jtulach@1334
   664
     * @param locale
jtulach@1334
   665
     *        the locale for which a resource bundle is desired
jtulach@1334
   666
     * @exception NullPointerException
jtulach@1334
   667
     *        if <code>baseName</code> or <code>locale</code> is <code>null</code>
jtulach@1334
   668
     * @exception MissingResourceException
jtulach@1334
   669
     *        if no resource bundle for the specified base name can be found
jtulach@1334
   670
     * @return a resource bundle for the given base name and locale
jtulach@1334
   671
     */
jtulach@1334
   672
    public static final ResourceBundle getBundle(String baseName,
jtulach@1334
   673
                                                 Locale locale)
jtulach@1334
   674
    {
jtulach@1334
   675
        return getBundleImpl(baseName, locale,
jtulach@1334
   676
                             /* must determine loader here, else we break stack invariant */
jtulach@1334
   677
                             Control.INSTANCE);
jtulach@1334
   678
    }
jtulach@1334
   679
jtulach@1334
   680
    /**
jtulach@1334
   681
     * Returns a resource bundle using the specified base name, target
jtulach@1334
   682
     * locale and control, and the caller's class loader. Calling this
jtulach@1334
   683
     * method is equivalent to calling
jtulach@1334
   684
     * <pre>
jtulach@1334
   685
     * getBundle(baseName, targetLocale, this.getClass().getClassLoader(),
jtulach@1334
   686
     *           control),
jtulach@1334
   687
     * </pre>
jtulach@1334
   688
     * except that <code>getClassLoader()</code> is run with the security
jtulach@1334
   689
     * privileges of <code>ResourceBundle</code>.  See {@link
jtulach@1334
   690
     * #getBundle(String, Locale, ClassLoader, Control) getBundle} for the
jtulach@1334
   691
     * complete description of the resource bundle loading process with a
jtulach@1334
   692
     * <code>ResourceBundle.Control</code>.
jtulach@1334
   693
     *
jtulach@1334
   694
     * @param baseName
jtulach@1334
   695
     *        the base name of the resource bundle, a fully qualified
jtulach@1334
   696
     *        class name
jtulach@1334
   697
     * @param targetLocale
jtulach@1334
   698
     *        the locale for which a resource bundle is desired
jtulach@1334
   699
     * @param control
jtulach@1334
   700
     *        the control which gives information for the resource
jtulach@1334
   701
     *        bundle loading process
jtulach@1334
   702
     * @return a resource bundle for the given base name and a
jtulach@1334
   703
     *        <code>Locale</code> in <code>locales</code>
jtulach@1334
   704
     * @exception NullPointerException
jtulach@1334
   705
     *        if <code>baseName</code>, <code>locales</code> or
jtulach@1334
   706
     *        <code>control</code> is <code>null</code>
jtulach@1334
   707
     * @exception MissingResourceException
jtulach@1334
   708
     *        if no resource bundle for the specified base name in any
jtulach@1334
   709
     *        of the <code>locales</code> can be found.
jtulach@1334
   710
     * @exception IllegalArgumentException
jtulach@1334
   711
     *        if the given <code>control</code> doesn't perform properly
jtulach@1334
   712
     *        (e.g., <code>control.getCandidateLocales</code> returns null.)
jtulach@1334
   713
     *        Note that validation of <code>control</code> is performed as
jtulach@1334
   714
     *        needed.
jtulach@1334
   715
     * @since 1.6
jtulach@1334
   716
     */
jtulach@1334
   717
    public static final ResourceBundle getBundle(String baseName, Locale targetLocale,
jtulach@1334
   718
                                                 Control control) {
jtulach@1334
   719
        return getBundleImpl(baseName, targetLocale,
jtulach@1334
   720
                             /* must determine loader here, else we break stack invariant */
jtulach@1334
   721
                             control);
jtulach@1334
   722
    }
jtulach@1334
   723
jtulach@1334
   724
    /**
jtulach@1334
   725
     * Gets a resource bundle using the specified base name, locale, and class
jtulach@1334
   726
     * loader.
jtulach@1334
   727
     *
jtulach@1334
   728
     * <p><a name="default_behavior"/>This method behaves the same as calling
jtulach@1334
   729
     * {@link #getBundle(String, Locale, ClassLoader, Control)} passing a
jtulach@1334
   730
     * default instance of {@link Control}. The following describes this behavior.
jtulach@1334
   731
     *
jtulach@1334
   732
     * <p><code>getBundle</code> uses the base name, the specified locale, and
jtulach@1334
   733
     * the default locale (obtained from {@link java.util.Locale#getDefault()
jtulach@1334
   734
     * Locale.getDefault}) to generate a sequence of <a
jtulach@1334
   735
     * name="candidates"><em>candidate bundle names</em></a>.  If the specified
jtulach@1334
   736
     * locale's language, script, country, and variant are all empty strings,
jtulach@1334
   737
     * then the base name is the only candidate bundle name.  Otherwise, a list
jtulach@1334
   738
     * of candidate locales is generated from the attribute values of the
jtulach@1334
   739
     * specified locale (language, script, country and variant) and appended to
jtulach@1334
   740
     * the base name.  Typically, this will look like the following:
jtulach@1334
   741
     *
jtulach@1334
   742
     * <pre>
jtulach@1334
   743
     *     baseName + "_" + language + "_" + script + "_" + country + "_" + variant
jtulach@1334
   744
     *     baseName + "_" + language + "_" + script + "_" + country
jtulach@1334
   745
     *     baseName + "_" + language + "_" + script
jtulach@1334
   746
     *     baseName + "_" + language + "_" + country + "_" + variant
jtulach@1334
   747
     *     baseName + "_" + language + "_" + country
jtulach@1334
   748
     *     baseName + "_" + language
jtulach@1334
   749
     * </pre>
jtulach@1334
   750
     *
jtulach@1334
   751
     * <p>Candidate bundle names where the final component is an empty string
jtulach@1334
   752
     * are omitted, along with the underscore.  For example, if country is an
jtulach@1334
   753
     * empty string, the second and the fifth candidate bundle names above
jtulach@1334
   754
     * would be omitted.  Also, if script is an empty string, the candidate names
jtulach@1334
   755
     * including script are omitted.  For example, a locale with language "de"
jtulach@1334
   756
     * and variant "JAVA" will produce candidate names with base name
jtulach@1334
   757
     * "MyResource" below.
jtulach@1334
   758
     *
jtulach@1334
   759
     * <pre>
jtulach@1334
   760
     *     MyResource_de__JAVA
jtulach@1334
   761
     *     MyResource_de
jtulach@1334
   762
     * </pre>
jtulach@1334
   763
     *
jtulach@1334
   764
     * In the case that the variant contains one or more underscores ('_'), a
jtulach@1334
   765
     * sequence of bundle names generated by truncating the last underscore and
jtulach@1334
   766
     * the part following it is inserted after a candidate bundle name with the
jtulach@1334
   767
     * original variant.  For example, for a locale with language "en", script
jtulach@1334
   768
     * "Latn, country "US" and variant "WINDOWS_VISTA", and bundle base name
jtulach@1334
   769
     * "MyResource", the list of candidate bundle names below is generated:
jtulach@1334
   770
     *
jtulach@1334
   771
     * <pre>
jtulach@1334
   772
     * MyResource_en_Latn_US_WINDOWS_VISTA
jtulach@1334
   773
     * MyResource_en_Latn_US_WINDOWS
jtulach@1334
   774
     * MyResource_en_Latn_US
jtulach@1334
   775
     * MyResource_en_Latn
jtulach@1334
   776
     * MyResource_en_US_WINDOWS_VISTA
jtulach@1334
   777
     * MyResource_en_US_WINDOWS
jtulach@1334
   778
     * MyResource_en_US
jtulach@1334
   779
     * MyResource_en
jtulach@1334
   780
     * </pre>
jtulach@1334
   781
     *
jtulach@1334
   782
     * <blockquote><b>Note:</b> For some <code>Locale</code>s, the list of
jtulach@1334
   783
     * candidate bundle names contains extra names, or the order of bundle names
jtulach@1334
   784
     * is slightly modified.  See the description of the default implementation
jtulach@1334
   785
     * of {@link Control#getCandidateLocales(String, Locale)
jtulach@1334
   786
     * getCandidateLocales} for details.</blockquote>
jtulach@1334
   787
     *
jtulach@1334
   788
     * <p><code>getBundle</code> then iterates over the candidate bundle names
jtulach@1334
   789
     * to find the first one for which it can <em>instantiate</em> an actual
jtulach@1334
   790
     * resource bundle. It uses the default controls' {@link Control#getFormats
jtulach@1334
   791
     * getFormats} method, which generates two bundle names for each generated
jtulach@1334
   792
     * name, the first a class name and the second a properties file name. For
jtulach@1334
   793
     * each candidate bundle name, it attempts to create a resource bundle:
jtulach@1334
   794
     *
jtulach@1334
   795
     * <ul><li>First, it attempts to load a class using the generated class name.
jtulach@1334
   796
     * If such a class can be found and loaded using the specified class
jtulach@1334
   797
     * loader, is assignment compatible with ResourceBundle, is accessible from
jtulach@1334
   798
     * ResourceBundle, and can be instantiated, <code>getBundle</code> creates a
jtulach@1334
   799
     * new instance of this class and uses it as the <em>result resource
jtulach@1334
   800
     * bundle</em>.
jtulach@1334
   801
     *
jtulach@1334
   802
     * <li>Otherwise, <code>getBundle</code> attempts to locate a property
jtulach@1334
   803
     * resource file using the generated properties file name.  It generates a
jtulach@1334
   804
     * path name from the candidate bundle name by replacing all "." characters
jtulach@1334
   805
     * with "/" and appending the string ".properties".  It attempts to find a
jtulach@1334
   806
     * "resource" with this name using {@link
jtulach@1334
   807
     * java.lang.ClassLoader#getResource(java.lang.String)
jtulach@1334
   808
     * ClassLoader.getResource}.  (Note that a "resource" in the sense of
jtulach@1334
   809
     * <code>getResource</code> has nothing to do with the contents of a
jtulach@1334
   810
     * resource bundle, it is just a container of data, such as a file.)  If it
jtulach@1334
   811
     * finds a "resource", it attempts to create a new {@link
jtulach@1334
   812
     * PropertyResourceBundle} instance from its contents.  If successful, this
jtulach@1334
   813
     * instance becomes the <em>result resource bundle</em>.  </ul>
jtulach@1334
   814
     *
jtulach@1334
   815
     * <p>This continues until a result resource bundle is instantiated or the
jtulach@1334
   816
     * list of candidate bundle names is exhausted.  If no matching resource
jtulach@1334
   817
     * bundle is found, the default control's {@link Control#getFallbackLocale
jtulach@1334
   818
     * getFallbackLocale} method is called, which returns the current default
jtulach@1334
   819
     * locale.  A new sequence of candidate locale names is generated using this
jtulach@1334
   820
     * locale and and searched again, as above.
jtulach@1334
   821
     *
jtulach@1334
   822
     * <p>If still no result bundle is found, the base name alone is looked up. If
jtulach@1334
   823
     * this still fails, a <code>MissingResourceException</code> is thrown.
jtulach@1334
   824
     *
jtulach@1334
   825
     * <p><a name="parent_chain"/> Once a result resource bundle has been found,
jtulach@1334
   826
     * its <em>parent chain</em> is instantiated.  If the result bundle already
jtulach@1334
   827
     * has a parent (perhaps because it was returned from a cache) the chain is
jtulach@1334
   828
     * complete.
jtulach@1334
   829
     *
jtulach@1334
   830
     * <p>Otherwise, <code>getBundle</code> examines the remainder of the
jtulach@1334
   831
     * candidate locale list that was used during the pass that generated the
jtulach@1334
   832
     * result resource bundle.  (As before, candidate bundle names where the
jtulach@1334
   833
     * final component is an empty string are omitted.)  When it comes to the
jtulach@1334
   834
     * end of the candidate list, it tries the plain bundle name.  With each of the
jtulach@1334
   835
     * candidate bundle names it attempts to instantiate a resource bundle (first
jtulach@1334
   836
     * looking for a class and then a properties file, as described above).
jtulach@1334
   837
     *
jtulach@1334
   838
     * <p>Whenever it succeeds, it calls the previously instantiated resource
jtulach@1334
   839
     * bundle's {@link #setParent(java.util.ResourceBundle) setParent} method
jtulach@1334
   840
     * with the new resource bundle.  This continues until the list of names
jtulach@1334
   841
     * is exhausted or the current bundle already has a non-null parent.
jtulach@1334
   842
     *
jtulach@1334
   843
     * <p>Once the parent chain is complete, the bundle is returned.
jtulach@1334
   844
     *
jtulach@1334
   845
     * <p><b>Note:</b> <code>getBundle</code> caches instantiated resource
jtulach@1334
   846
     * bundles and might return the same resource bundle instance multiple times.
jtulach@1334
   847
     *
jtulach@1334
   848
     * <p><b>Note:</b>The <code>baseName</code> argument should be a fully
jtulach@1334
   849
     * qualified class name. However, for compatibility with earlier versions,
jtulach@1334
   850
     * Sun's Java SE Runtime Environments do not verify this, and so it is
jtulach@1334
   851
     * possible to access <code>PropertyResourceBundle</code>s by specifying a
jtulach@1334
   852
     * path name (using "/") instead of a fully qualified class name (using
jtulach@1334
   853
     * ".").
jtulach@1334
   854
     *
jtulach@1334
   855
     * <p><a name="default_behavior_example"/>
jtulach@1334
   856
     * <strong>Example:</strong>
jtulach@1334
   857
     * <p>
jtulach@1334
   858
     * The following class and property files are provided:
jtulach@1334
   859
     * <pre>
jtulach@1334
   860
     *     MyResources.class
jtulach@1334
   861
     *     MyResources.properties
jtulach@1334
   862
     *     MyResources_fr.properties
jtulach@1334
   863
     *     MyResources_fr_CH.class
jtulach@1334
   864
     *     MyResources_fr_CH.properties
jtulach@1334
   865
     *     MyResources_en.properties
jtulach@1334
   866
     *     MyResources_es_ES.class
jtulach@1334
   867
     * </pre>
jtulach@1334
   868
     *
jtulach@1334
   869
     * The contents of all files are valid (that is, public non-abstract
jtulach@1334
   870
     * subclasses of <code>ResourceBundle</code> for the ".class" files,
jtulach@1334
   871
     * syntactically correct ".properties" files).  The default locale is
jtulach@1334
   872
     * <code>Locale("en", "GB")</code>.
jtulach@1334
   873
     *
jtulach@1334
   874
     * <p>Calling <code>getBundle</code> with the locale arguments below will
jtulach@1334
   875
     * instantiate resource bundles as follows:
jtulach@1334
   876
     *
jtulach@1334
   877
     * <table>
jtulach@1334
   878
     * <tr><td>Locale("fr", "CH")</td><td>MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class</td></tr>
jtulach@1334
   879
     * <tr><td>Locale("fr", "FR")</td><td>MyResources_fr.properties, parent MyResources.class</td></tr>
jtulach@1334
   880
     * <tr><td>Locale("de", "DE")</td><td>MyResources_en.properties, parent MyResources.class</td></tr>
jtulach@1334
   881
     * <tr><td>Locale("en", "US")</td><td>MyResources_en.properties, parent MyResources.class</td></tr>
jtulach@1334
   882
     * <tr><td>Locale("es", "ES")</td><td>MyResources_es_ES.class, parent MyResources.class</td></tr>
jtulach@1334
   883
     * </table>
jtulach@1334
   884
     *
jtulach@1334
   885
     * <p>The file MyResources_fr_CH.properties is never used because it is
jtulach@1334
   886
     * hidden by the MyResources_fr_CH.class. Likewise, MyResources.properties
jtulach@1334
   887
     * is also hidden by MyResources.class.
jtulach@1334
   888
     *
jtulach@1334
   889
     * @param baseName the base name of the resource bundle, a fully qualified class name
jtulach@1334
   890
     * @param locale the locale for which a resource bundle is desired
jtulach@1334
   891
     * @param loader the class loader from which to load the resource bundle
jtulach@1334
   892
     * @return a resource bundle for the given base name and locale
jtulach@1334
   893
     * @exception java.lang.NullPointerException
jtulach@1334
   894
     *        if <code>baseName</code>, <code>locale</code>, or <code>loader</code> is <code>null</code>
jtulach@1334
   895
     * @exception MissingResourceException
jtulach@1334
   896
     *        if no resource bundle for the specified base name can be found
jtulach@1334
   897
     * @since 1.2
jtulach@1334
   898
     */
jtulach@1334
   899
    public static ResourceBundle getBundle(String baseName, Locale locale,
jtulach@1334
   900
                                           ClassLoader loader)
jtulach@1334
   901
    {
jtulach@1334
   902
        if (loader == null) {
jtulach@1334
   903
            throw new NullPointerException();
jtulach@1334
   904
        }
jaroslav@1337
   905
        return getBundleImpl(baseName, locale, Control.INSTANCE);
jtulach@1334
   906
    }
jtulach@1334
   907
jtulach@1334
   908
    /**
jtulach@1334
   909
     * Returns a resource bundle using the specified base name, target
jtulach@1334
   910
     * locale, class loader and control. Unlike the {@linkplain
jtulach@1334
   911
     * #getBundle(String, Locale, ClassLoader) <code>getBundle</code>
jtulach@1334
   912
     * factory methods with no <code>control</code> argument}, the given
jtulach@1334
   913
     * <code>control</code> specifies how to locate and instantiate resource
jtulach@1334
   914
     * bundles. Conceptually, the bundle loading process with the given
jtulach@1334
   915
     * <code>control</code> is performed in the following steps.
jtulach@1334
   916
     *
jtulach@1334
   917
     * <p>
jtulach@1334
   918
     * <ol>
jtulach@1334
   919
     * <li>This factory method looks up the resource bundle in the cache for
jtulach@1334
   920
     * the specified <code>baseName</code>, <code>targetLocale</code> and
jtulach@1334
   921
     * <code>loader</code>.  If the requested resource bundle instance is
jtulach@1334
   922
     * found in the cache and the time-to-live periods of the instance and
jtulach@1334
   923
     * all of its parent instances have not expired, the instance is returned
jtulach@1334
   924
     * to the caller. Otherwise, this factory method proceeds with the
jtulach@1334
   925
     * loading process below.</li>
jtulach@1334
   926
     *
jtulach@1334
   927
     * <li>The {@link ResourceBundle.Control#getFormats(String)
jtulach@1334
   928
     * control.getFormats} method is called to get resource bundle formats
jtulach@1334
   929
     * to produce bundle or resource names. The strings
jtulach@1334
   930
     * <code>"java.class"</code> and <code>"java.properties"</code>
jtulach@1334
   931
     * designate class-based and {@linkplain PropertyResourceBundle
jtulach@1334
   932
     * property}-based resource bundles, respectively. Other strings
jtulach@1334
   933
     * starting with <code>"java."</code> are reserved for future extensions
jtulach@1334
   934
     * and must not be used for application-defined formats. Other strings
jtulach@1334
   935
     * designate application-defined formats.</li>
jtulach@1334
   936
     *
jtulach@1334
   937
     * <li>The {@link ResourceBundle.Control#getCandidateLocales(String,
jtulach@1334
   938
     * Locale) control.getCandidateLocales} method is called with the target
jtulach@1334
   939
     * locale to get a list of <em>candidate <code>Locale</code>s</em> for
jtulach@1334
   940
     * which resource bundles are searched.</li>
jtulach@1334
   941
     *
jtulach@1334
   942
     * <li>The {@link ResourceBundle.Control#newBundle(String, Locale,
jtulach@1334
   943
     * String, ClassLoader, boolean) control.newBundle} method is called to
jtulach@1334
   944
     * instantiate a <code>ResourceBundle</code> for the base bundle name, a
jtulach@1334
   945
     * candidate locale, and a format. (Refer to the note on the cache
jtulach@1334
   946
     * lookup below.) This step is iterated over all combinations of the
jtulach@1334
   947
     * candidate locales and formats until the <code>newBundle</code> method
jtulach@1334
   948
     * returns a <code>ResourceBundle</code> instance or the iteration has
jtulach@1334
   949
     * used up all the combinations. For example, if the candidate locales
jtulach@1334
   950
     * are <code>Locale("de", "DE")</code>, <code>Locale("de")</code> and
jtulach@1334
   951
     * <code>Locale("")</code> and the formats are <code>"java.class"</code>
jtulach@1334
   952
     * and <code>"java.properties"</code>, then the following is the
jtulach@1334
   953
     * sequence of locale-format combinations to be used to call
jtulach@1334
   954
     * <code>control.newBundle</code>.
jtulach@1334
   955
     *
jtulach@1334
   956
     * <table style="width: 50%; text-align: left; margin-left: 40px;"
jtulach@1334
   957
     *  border="0" cellpadding="2" cellspacing="2">
jtulach@1334
   958
     * <tbody><code>
jtulach@1334
   959
     * <tr>
jtulach@1334
   960
     * <td
jtulach@1334
   961
     * style="vertical-align: top; text-align: left; font-weight: bold; width: 50%;">Locale<br>
jtulach@1334
   962
     * </td>
jtulach@1334
   963
     * <td
jtulach@1334
   964
     * style="vertical-align: top; text-align: left; font-weight: bold; width: 50%;">format<br>
jtulach@1334
   965
     * </td>
jtulach@1334
   966
     * </tr>
jtulach@1334
   967
     * <tr>
jtulach@1334
   968
     * <td style="vertical-align: top; width: 50%;">Locale("de", "DE")<br>
jtulach@1334
   969
     * </td>
jtulach@1334
   970
     * <td style="vertical-align: top; width: 50%;">java.class<br>
jtulach@1334
   971
     * </td>
jtulach@1334
   972
     * </tr>
jtulach@1334
   973
     * <tr>
jtulach@1334
   974
     * <td style="vertical-align: top; width: 50%;">Locale("de", "DE")</td>
jtulach@1334
   975
     * <td style="vertical-align: top; width: 50%;">java.properties<br>
jtulach@1334
   976
     * </td>
jtulach@1334
   977
     * </tr>
jtulach@1334
   978
     * <tr>
jtulach@1334
   979
     * <td style="vertical-align: top; width: 50%;">Locale("de")</td>
jtulach@1334
   980
     * <td style="vertical-align: top; width: 50%;">java.class</td>
jtulach@1334
   981
     * </tr>
jtulach@1334
   982
     * <tr>
jtulach@1334
   983
     * <td style="vertical-align: top; width: 50%;">Locale("de")</td>
jtulach@1334
   984
     * <td style="vertical-align: top; width: 50%;">java.properties</td>
jtulach@1334
   985
     * </tr>
jtulach@1334
   986
     * <tr>
jtulach@1334
   987
     * <td style="vertical-align: top; width: 50%;">Locale("")<br>
jtulach@1334
   988
     * </td>
jtulach@1334
   989
     * <td style="vertical-align: top; width: 50%;">java.class</td>
jtulach@1334
   990
     * </tr>
jtulach@1334
   991
     * <tr>
jtulach@1334
   992
     * <td style="vertical-align: top; width: 50%;">Locale("")</td>
jtulach@1334
   993
     * <td style="vertical-align: top; width: 50%;">java.properties</td>
jtulach@1334
   994
     * </tr>
jtulach@1334
   995
     * </code></tbody>
jtulach@1334
   996
     * </table>
jtulach@1334
   997
     * </li>
jtulach@1334
   998
     *
jtulach@1334
   999
     * <li>If the previous step has found no resource bundle, proceed to
jtulach@1334
  1000
     * Step 6. If a bundle has been found that is a base bundle (a bundle
jtulach@1334
  1001
     * for <code>Locale("")</code>), and the candidate locale list only contained
jtulach@1334
  1002
     * <code>Locale("")</code>, return the bundle to the caller. If a bundle
jtulach@1334
  1003
     * has been found that is a base bundle, but the candidate locale list
jtulach@1334
  1004
     * contained locales other than Locale(""), put the bundle on hold and
jtulach@1334
  1005
     * proceed to Step 6. If a bundle has been found that is not a base
jtulach@1334
  1006
     * bundle, proceed to Step 7.</li>
jtulach@1334
  1007
     *
jtulach@1334
  1008
     * <li>The {@link ResourceBundle.Control#getFallbackLocale(String,
jtulach@1334
  1009
     * Locale) control.getFallbackLocale} method is called to get a fallback
jtulach@1334
  1010
     * locale (alternative to the current target locale) to try further
jtulach@1334
  1011
     * finding a resource bundle. If the method returns a non-null locale,
jtulach@1334
  1012
     * it becomes the next target locale and the loading process starts over
jtulach@1334
  1013
     * from Step 3. Otherwise, if a base bundle was found and put on hold in
jtulach@1334
  1014
     * a previous Step 5, it is returned to the caller now. Otherwise, a
jtulach@1334
  1015
     * MissingResourceException is thrown.</li>
jtulach@1334
  1016
     *
jtulach@1334
  1017
     * <li>At this point, we have found a resource bundle that's not the
jtulach@1334
  1018
     * base bundle. If this bundle set its parent during its instantiation,
jtulach@1334
  1019
     * it is returned to the caller. Otherwise, its <a
jtulach@1334
  1020
     * href="./ResourceBundle.html#parent_chain">parent chain</a> is
jtulach@1334
  1021
     * instantiated based on the list of candidate locales from which it was
jtulach@1334
  1022
     * found. Finally, the bundle is returned to the caller.</li>
jtulach@1334
  1023
     * </ol>
jtulach@1334
  1024
     *
jtulach@1334
  1025
     * <p>During the resource bundle loading process above, this factory
jtulach@1334
  1026
     * method looks up the cache before calling the {@link
jtulach@1334
  1027
     * Control#newBundle(String, Locale, String, ClassLoader, boolean)
jtulach@1334
  1028
     * control.newBundle} method.  If the time-to-live period of the
jtulach@1334
  1029
     * resource bundle found in the cache has expired, the factory method
jtulach@1334
  1030
     * calls the {@link ResourceBundle.Control#needsReload(String, Locale,
jtulach@1334
  1031
     * String, ClassLoader, ResourceBundle, long) control.needsReload}
jtulach@1334
  1032
     * method to determine whether the resource bundle needs to be reloaded.
jtulach@1334
  1033
     * If reloading is required, the factory method calls
jtulach@1334
  1034
     * <code>control.newBundle</code> to reload the resource bundle.  If
jtulach@1334
  1035
     * <code>control.newBundle</code> returns <code>null</code>, the factory
jtulach@1334
  1036
     * method puts a dummy resource bundle in the cache as a mark of
jtulach@1334
  1037
     * nonexistent resource bundles in order to avoid lookup overhead for
jtulach@1334
  1038
     * subsequent requests. Such dummy resource bundles are under the same
jtulach@1334
  1039
     * expiration control as specified by <code>control</code>.
jtulach@1334
  1040
     *
jtulach@1334
  1041
     * <p>All resource bundles loaded are cached by default. Refer to
jtulach@1334
  1042
     * {@link Control#getTimeToLive(String,Locale)
jtulach@1334
  1043
     * control.getTimeToLive} for details.
jtulach@1334
  1044
     *
jtulach@1334
  1045
     * <p>The following is an example of the bundle loading process with the
jtulach@1334
  1046
     * default <code>ResourceBundle.Control</code> implementation.
jtulach@1334
  1047
     *
jtulach@1334
  1048
     * <p>Conditions:
jtulach@1334
  1049
     * <ul>
jtulach@1334
  1050
     * <li>Base bundle name: <code>foo.bar.Messages</code>
jtulach@1334
  1051
     * <li>Requested <code>Locale</code>: {@link Locale#ITALY}</li>
jtulach@1334
  1052
     * <li>Default <code>Locale</code>: {@link Locale#FRENCH}</li>
jtulach@1334
  1053
     * <li>Available resource bundles:
jtulach@1334
  1054
     * <code>foo/bar/Messages_fr.properties</code> and
jtulach@1334
  1055
     * <code>foo/bar/Messages.properties</code></li>
jtulach@1334
  1056
     * </ul>
jtulach@1334
  1057
     *
jtulach@1334
  1058
     * <p>First, <code>getBundle</code> tries loading a resource bundle in
jtulach@1334
  1059
     * the following sequence.
jtulach@1334
  1060
     *
jtulach@1334
  1061
     * <ul>
jtulach@1334
  1062
     * <li>class <code>foo.bar.Messages_it_IT</code>
jtulach@1334
  1063
     * <li>file <code>foo/bar/Messages_it_IT.properties</code>
jtulach@1334
  1064
     * <li>class <code>foo.bar.Messages_it</code></li>
jtulach@1334
  1065
     * <li>file <code>foo/bar/Messages_it.properties</code></li>
jtulach@1334
  1066
     * <li>class <code>foo.bar.Messages</code></li>
jtulach@1334
  1067
     * <li>file <code>foo/bar/Messages.properties</code></li>
jtulach@1334
  1068
     * </ul>
jtulach@1334
  1069
     *
jtulach@1334
  1070
     * <p>At this point, <code>getBundle</code> finds
jtulach@1334
  1071
     * <code>foo/bar/Messages.properties</code>, which is put on hold
jtulach@1334
  1072
     * because it's the base bundle.  <code>getBundle</code> calls {@link
jtulach@1334
  1073
     * Control#getFallbackLocale(String, Locale)
jtulach@1334
  1074
     * control.getFallbackLocale("foo.bar.Messages", Locale.ITALY)} which
jtulach@1334
  1075
     * returns <code>Locale.FRENCH</code>. Next, <code>getBundle</code>
jtulach@1334
  1076
     * tries loading a bundle in the following sequence.
jtulach@1334
  1077
     *
jtulach@1334
  1078
     * <ul>
jtulach@1334
  1079
     * <li>class <code>foo.bar.Messages_fr</code></li>
jtulach@1334
  1080
     * <li>file <code>foo/bar/Messages_fr.properties</code></li>
jtulach@1334
  1081
     * <li>class <code>foo.bar.Messages</code></li>
jtulach@1334
  1082
     * <li>file <code>foo/bar/Messages.properties</code></li>
jtulach@1334
  1083
     * </ul>
jtulach@1334
  1084
     *
jtulach@1334
  1085
     * <p><code>getBundle</code> finds
jtulach@1334
  1086
     * <code>foo/bar/Messages_fr.properties</code> and creates a
jtulach@1334
  1087
     * <code>ResourceBundle</code> instance. Then, <code>getBundle</code>
jtulach@1334
  1088
     * sets up its parent chain from the list of the candiate locales.  Only
jtulach@1334
  1089
     * <code>foo/bar/Messages.properties</code> is found in the list and
jtulach@1334
  1090
     * <code>getBundle</code> creates a <code>ResourceBundle</code> instance
jtulach@1334
  1091
     * that becomes the parent of the instance for
jtulach@1334
  1092
     * <code>foo/bar/Messages_fr.properties</code>.
jtulach@1334
  1093
     *
jtulach@1334
  1094
     * @param baseName
jtulach@1334
  1095
     *        the base name of the resource bundle, a fully qualified
jtulach@1334
  1096
     *        class name
jtulach@1334
  1097
     * @param targetLocale
jtulach@1334
  1098
     *        the locale for which a resource bundle is desired
jtulach@1334
  1099
     * @param loader
jtulach@1334
  1100
     *        the class loader from which to load the resource bundle
jtulach@1334
  1101
     * @param control
jtulach@1334
  1102
     *        the control which gives information for the resource
jtulach@1334
  1103
     *        bundle loading process
jtulach@1334
  1104
     * @return a resource bundle for the given base name and locale
jtulach@1334
  1105
     * @exception NullPointerException
jtulach@1334
  1106
     *        if <code>baseName</code>, <code>targetLocale</code>,
jtulach@1334
  1107
     *        <code>loader</code>, or <code>control</code> is
jtulach@1334
  1108
     *        <code>null</code>
jtulach@1334
  1109
     * @exception MissingResourceException
jtulach@1334
  1110
     *        if no resource bundle for the specified base name can be found
jtulach@1334
  1111
     * @exception IllegalArgumentException
jtulach@1334
  1112
     *        if the given <code>control</code> doesn't perform properly
jtulach@1334
  1113
     *        (e.g., <code>control.getCandidateLocales</code> returns null.)
jtulach@1334
  1114
     *        Note that validation of <code>control</code> is performed as
jtulach@1334
  1115
     *        needed.
jtulach@1334
  1116
     * @since 1.6
jtulach@1334
  1117
     */
jtulach@1334
  1118
    public static ResourceBundle getBundle(String baseName, Locale targetLocale,
jtulach@1334
  1119
                                           ClassLoader loader, Control control) {
jtulach@1334
  1120
        if (loader == null || control == null) {
jtulach@1334
  1121
            throw new NullPointerException();
jtulach@1334
  1122
        }
jaroslav@1337
  1123
        return getBundleImpl(baseName, targetLocale, control);
jtulach@1334
  1124
    }
jtulach@1334
  1125
jtulach@1334
  1126
    private static ResourceBundle getBundleImpl(String baseName, Locale locale,
jaroslav@1337
  1127
                                                Control control) {
jtulach@1334
  1128
        if (locale == null || control == null) {
jtulach@1334
  1129
            throw new NullPointerException();
jtulach@1334
  1130
        }
jtulach@1334
  1131
jtulach@1334
  1132
        // We create a CacheKey here for use by this call. The base
jtulach@1334
  1133
        // name and loader will never change during the bundle loading
jtulach@1334
  1134
        // process. We have to make sure that the locale is set before
jtulach@1334
  1135
        // using it as a cache key.
jaroslav@1337
  1136
        CacheKey cacheKey = new CacheKey(baseName, locale);
jtulach@1334
  1137
        ResourceBundle bundle = null;
jtulach@1334
  1138
jtulach@1334
  1139
        // Quick lookup of the cache.
jtulach@1334
  1140
        BundleReference bundleRef = cacheList.get(cacheKey);
jtulach@1334
  1141
        if (bundleRef != null) {
jtulach@1334
  1142
            bundle = bundleRef.get();
jtulach@1334
  1143
            bundleRef = null;
jtulach@1334
  1144
        }
jtulach@1334
  1145
jtulach@1334
  1146
        // If this bundle and all of its parents are valid (not expired),
jtulach@1334
  1147
        // then return this bundle. If any of the bundles is expired, we
jtulach@1334
  1148
        // don't call control.needsReload here but instead drop into the
jtulach@1334
  1149
        // complete loading process below.
jtulach@1334
  1150
        if (isValidBundle(bundle) && hasValidParentChain(bundle)) {
jtulach@1334
  1151
            return bundle;
jtulach@1334
  1152
        }
jtulach@1334
  1153
jtulach@1334
  1154
        // No valid bundle was found in the cache, so we need to load the
jtulach@1334
  1155
        // resource bundle and its parents.
jtulach@1334
  1156
jtulach@1334
  1157
        boolean isKnownControl = (control == Control.INSTANCE) ||
jtulach@1334
  1158
                                   (control instanceof SingleFormatControl);
jtulach@1334
  1159
        List<String> formats = control.getFormats(baseName);
jtulach@1334
  1160
        if (!isKnownControl && !checkList(formats)) {
jtulach@1334
  1161
            throw new IllegalArgumentException("Invalid Control: getFormats");
jtulach@1334
  1162
        }
jtulach@1334
  1163
jtulach@1334
  1164
        ResourceBundle baseBundle = null;
jtulach@1334
  1165
        for (Locale targetLocale = locale;
jtulach@1334
  1166
             targetLocale != null;
jtulach@1334
  1167
             targetLocale = control.getFallbackLocale(baseName, targetLocale)) {
jtulach@1334
  1168
            List<Locale> candidateLocales = control.getCandidateLocales(baseName, targetLocale);
jtulach@1334
  1169
            if (!isKnownControl && !checkList(candidateLocales)) {
jtulach@1334
  1170
                throw new IllegalArgumentException("Invalid Control: getCandidateLocales");
jtulach@1334
  1171
            }
jtulach@1334
  1172
jtulach@1334
  1173
            bundle = findBundle(cacheKey, candidateLocales, formats, 0, control, baseBundle);
jtulach@1334
  1174
jtulach@1334
  1175
            // If the loaded bundle is the base bundle and exactly for the
jtulach@1334
  1176
            // requested locale or the only candidate locale, then take the
jtulach@1334
  1177
            // bundle as the resulting one. If the loaded bundle is the base
jtulach@1334
  1178
            // bundle, it's put on hold until we finish processing all
jtulach@1334
  1179
            // fallback locales.
jtulach@1334
  1180
            if (isValidBundle(bundle)) {
jtulach@1334
  1181
                boolean isBaseBundle = Locale.ROOT.equals(bundle.locale);
jtulach@1334
  1182
                if (!isBaseBundle || bundle.locale.equals(locale)
jtulach@1334
  1183
                    || (candidateLocales.size() == 1
jtulach@1334
  1184
                        && bundle.locale.equals(candidateLocales.get(0)))) {
jtulach@1334
  1185
                    break;
jtulach@1334
  1186
                }
jtulach@1334
  1187
jtulach@1334
  1188
                // If the base bundle has been loaded, keep the reference in
jtulach@1334
  1189
                // baseBundle so that we can avoid any redundant loading in case
jtulach@1334
  1190
                // the control specify not to cache bundles.
jtulach@1334
  1191
                if (isBaseBundle && baseBundle == null) {
jtulach@1334
  1192
                    baseBundle = bundle;
jtulach@1334
  1193
                }
jtulach@1334
  1194
            }
jtulach@1334
  1195
        }
jtulach@1334
  1196
jtulach@1334
  1197
        if (bundle == null) {
jtulach@1334
  1198
            if (baseBundle == null) {
jtulach@1334
  1199
                throwMissingResourceException(baseName, locale, cacheKey.getCause());
jtulach@1334
  1200
            }
jtulach@1334
  1201
            bundle = baseBundle;
jtulach@1334
  1202
        }
jtulach@1334
  1203
jtulach@1334
  1204
        return bundle;
jtulach@1334
  1205
    }
jtulach@1334
  1206
jtulach@1334
  1207
    /**
jtulach@1334
  1208
     * Checks if the given <code>List</code> is not null, not empty,
jtulach@1334
  1209
     * not having null in its elements.
jtulach@1334
  1210
     */
jtulach@1334
  1211
    private static final boolean checkList(List a) {
jtulach@1334
  1212
        boolean valid = (a != null && a.size() != 0);
jtulach@1334
  1213
        if (valid) {
jtulach@1334
  1214
            int size = a.size();
jtulach@1334
  1215
            for (int i = 0; valid && i < size; i++) {
jtulach@1334
  1216
                valid = (a.get(i) != null);
jtulach@1334
  1217
            }
jtulach@1334
  1218
        }
jtulach@1334
  1219
        return valid;
jtulach@1334
  1220
    }
jtulach@1334
  1221
jtulach@1334
  1222
    private static final ResourceBundle findBundle(CacheKey cacheKey,
jtulach@1334
  1223
                                                   List<Locale> candidateLocales,
jtulach@1334
  1224
                                                   List<String> formats,
jtulach@1334
  1225
                                                   int index,
jtulach@1334
  1226
                                                   Control control,
jtulach@1334
  1227
                                                   ResourceBundle baseBundle) {
jtulach@1334
  1228
        Locale targetLocale = candidateLocales.get(index);
jtulach@1334
  1229
        ResourceBundle parent = null;
jtulach@1334
  1230
        if (index != candidateLocales.size() - 1) {
jtulach@1334
  1231
            parent = findBundle(cacheKey, candidateLocales, formats, index + 1,
jtulach@1334
  1232
                                control, baseBundle);
jtulach@1334
  1233
        } else if (baseBundle != null && Locale.ROOT.equals(targetLocale)) {
jtulach@1334
  1234
            return baseBundle;
jtulach@1334
  1235
        }
jtulach@1334
  1236
jtulach@1334
  1237
        // Before we do the real loading work, see whether we need to
jtulach@1334
  1238
        // do some housekeeping: If references to class loaders or
jtulach@1334
  1239
        // resource bundles have been nulled out, remove all related
jtulach@1334
  1240
        // information from the cache.
jtulach@1334
  1241
        Object ref;
jtulach@1334
  1242
        while ((ref = referenceQueue.poll()) != null) {
jtulach@1334
  1243
            cacheList.remove(((CacheKeyReference)ref).getCacheKey());
jtulach@1334
  1244
        }
jtulach@1334
  1245
jtulach@1334
  1246
        // flag indicating the resource bundle has expired in the cache
jtulach@1334
  1247
        boolean expiredBundle = false;
jtulach@1334
  1248
jtulach@1334
  1249
        // First, look up the cache to see if it's in the cache, without
jtulach@1334
  1250
        // attempting to load bundle.
jtulach@1334
  1251
        cacheKey.setLocale(targetLocale);
jtulach@1334
  1252
        ResourceBundle bundle = findBundleInCache(cacheKey, control);
jtulach@1334
  1253
        if (isValidBundle(bundle)) {
jtulach@1334
  1254
            expiredBundle = bundle.expired;
jtulach@1334
  1255
            if (!expiredBundle) {
jtulach@1334
  1256
                // If its parent is the one asked for by the candidate
jtulach@1334
  1257
                // locales (the runtime lookup path), we can take the cached
jtulach@1334
  1258
                // one. (If it's not identical, then we'd have to check the
jtulach@1334
  1259
                // parent's parents to be consistent with what's been
jtulach@1334
  1260
                // requested.)
jtulach@1334
  1261
                if (bundle.parent == parent) {
jtulach@1334
  1262
                    return bundle;
jtulach@1334
  1263
                }
jtulach@1334
  1264
                // Otherwise, remove the cached one since we can't keep
jtulach@1334
  1265
                // the same bundles having different parents.
jtulach@1334
  1266
                BundleReference bundleRef = cacheList.get(cacheKey);
jtulach@1334
  1267
                if (bundleRef != null && bundleRef.get() == bundle) {
jaroslav@1337
  1268
                    cacheList.remove(cacheKey);
jtulach@1334
  1269
                }
jtulach@1334
  1270
            }
jtulach@1334
  1271
        }
jtulach@1334
  1272
jtulach@1334
  1273
        if (bundle != NONEXISTENT_BUNDLE) {
jtulach@1334
  1274
            CacheKey constKey = (CacheKey) cacheKey.clone();
jtulach@1334
  1275
jtulach@1334
  1276
            try {
jtulach@1334
  1277
                bundle = loadBundle(cacheKey, formats, control, expiredBundle);
jtulach@1334
  1278
                if (bundle != null) {
jtulach@1334
  1279
                    if (bundle.parent == null) {
jtulach@1334
  1280
                        bundle.setParent(parent);
jtulach@1334
  1281
                    }
jtulach@1334
  1282
                    bundle.locale = targetLocale;
jtulach@1334
  1283
                    bundle = putBundleInCache(cacheKey, bundle, control);
jtulach@1334
  1284
                    return bundle;
jtulach@1334
  1285
                }
jtulach@1334
  1286
jtulach@1334
  1287
                // Put NONEXISTENT_BUNDLE in the cache as a mark that there's no bundle
jtulach@1334
  1288
                // instance for the locale.
jtulach@1334
  1289
                putBundleInCache(cacheKey, NONEXISTENT_BUNDLE, control);
jtulach@1334
  1290
            } finally {
jtulach@1334
  1291
                if (constKey.getCause() instanceof InterruptedException) {
jtulach@1334
  1292
                    Thread.currentThread().interrupt();
jtulach@1334
  1293
                }
jtulach@1334
  1294
            }
jtulach@1334
  1295
        }
jtulach@1334
  1296
        return parent;
jtulach@1334
  1297
    }
jtulach@1334
  1298
jtulach@1334
  1299
    private static final ResourceBundle loadBundle(CacheKey cacheKey,
jtulach@1334
  1300
                                                   List<String> formats,
jtulach@1334
  1301
                                                   Control control,
jtulach@1334
  1302
                                                   boolean reload) {
jtulach@1334
  1303
jtulach@1334
  1304
        // Here we actually load the bundle in the order of formats
jtulach@1334
  1305
        // specified by the getFormats() value.
jtulach@1334
  1306
        Locale targetLocale = cacheKey.getLocale();
jtulach@1334
  1307
jtulach@1334
  1308
        ResourceBundle bundle = null;
jtulach@1334
  1309
        int size = formats.size();
jtulach@1334
  1310
        for (int i = 0; i < size; i++) {
jtulach@1334
  1311
            String format = formats.get(i);
jtulach@1334
  1312
            try {
jtulach@1334
  1313
                bundle = control.newBundle(cacheKey.getName(), targetLocale, format,
jaroslav@1337
  1314
                                           null, reload);
jtulach@1334
  1315
            } catch (LinkageError error) {
jtulach@1334
  1316
                // We need to handle the LinkageError case due to
jtulach@1334
  1317
                // inconsistent case-sensitivity in ClassLoader.
jtulach@1334
  1318
                // See 6572242 for details.
jtulach@1334
  1319
                cacheKey.setCause(error);
jtulach@1334
  1320
            } catch (Exception cause) {
jtulach@1334
  1321
                cacheKey.setCause(cause);
jtulach@1334
  1322
            }
jtulach@1334
  1323
            if (bundle != null) {
jtulach@1334
  1324
                // Set the format in the cache key so that it can be
jtulach@1334
  1325
                // used when calling needsReload later.
jtulach@1334
  1326
                cacheKey.setFormat(format);
jtulach@1334
  1327
                bundle.name = cacheKey.getName();
jtulach@1334
  1328
                bundle.locale = targetLocale;
jtulach@1334
  1329
                // Bundle provider might reuse instances. So we should make
jtulach@1334
  1330
                // sure to clear the expired flag here.
jtulach@1334
  1331
                bundle.expired = false;
jtulach@1334
  1332
                break;
jtulach@1334
  1333
            }
jtulach@1334
  1334
        }
jtulach@1334
  1335
jtulach@1334
  1336
        return bundle;
jtulach@1334
  1337
    }
jtulach@1334
  1338
jtulach@1334
  1339
    private static final boolean isValidBundle(ResourceBundle bundle) {
jtulach@1334
  1340
        return bundle != null && bundle != NONEXISTENT_BUNDLE;
jtulach@1334
  1341
    }
jtulach@1334
  1342
jtulach@1334
  1343
    /**
jtulach@1334
  1344
     * Determines whether any of resource bundles in the parent chain,
jtulach@1334
  1345
     * including the leaf, have expired.
jtulach@1334
  1346
     */
jtulach@1334
  1347
    private static final boolean hasValidParentChain(ResourceBundle bundle) {
jtulach@1334
  1348
        long now = System.currentTimeMillis();
jtulach@1334
  1349
        while (bundle != null) {
jtulach@1334
  1350
            if (bundle.expired) {
jtulach@1334
  1351
                return false;
jtulach@1334
  1352
            }
jtulach@1334
  1353
            CacheKey key = bundle.cacheKey;
jtulach@1334
  1354
            if (key != null) {
jtulach@1334
  1355
                long expirationTime = key.expirationTime;
jtulach@1334
  1356
                if (expirationTime >= 0 && expirationTime <= now) {
jtulach@1334
  1357
                    return false;
jtulach@1334
  1358
                }
jtulach@1334
  1359
            }
jtulach@1334
  1360
            bundle = bundle.parent;
jtulach@1334
  1361
        }
jtulach@1334
  1362
        return true;
jtulach@1334
  1363
    }
jtulach@1334
  1364
jtulach@1334
  1365
    /**
jtulach@1334
  1366
     * Throw a MissingResourceException with proper message
jtulach@1334
  1367
     */
jtulach@1334
  1368
    private static final void throwMissingResourceException(String baseName,
jtulach@1334
  1369
                                                            Locale locale,
jtulach@1334
  1370
                                                            Throwable cause) {
jtulach@1334
  1371
        // If the cause is a MissingResourceException, avoid creating
jtulach@1334
  1372
        // a long chain. (6355009)
jtulach@1334
  1373
        if (cause instanceof MissingResourceException) {
jtulach@1334
  1374
            cause = null;
jtulach@1334
  1375
        }
jtulach@1334
  1376
        throw new MissingResourceException("Can't find bundle for base name "
jtulach@1334
  1377
                                           + baseName + ", locale " + locale,
jtulach@1334
  1378
                                           baseName + "_" + locale, // className
jtulach@1334
  1379
                                           "",                      // key
jtulach@1334
  1380
                                           cause);
jtulach@1334
  1381
    }
jtulach@1334
  1382
jtulach@1334
  1383
    /**
jtulach@1334
  1384
     * Finds a bundle in the cache. Any expired bundles are marked as
jtulach@1334
  1385
     * `expired' and removed from the cache upon return.
jtulach@1334
  1386
     *
jtulach@1334
  1387
     * @param cacheKey the key to look up the cache
jtulach@1334
  1388
     * @param control the Control to be used for the expiration control
jtulach@1334
  1389
     * @return the cached bundle, or null if the bundle is not found in the
jtulach@1334
  1390
     * cache or its parent has expired. <code>bundle.expire</code> is true
jtulach@1334
  1391
     * upon return if the bundle in the cache has expired.
jtulach@1334
  1392
     */
jtulach@1334
  1393
    private static final ResourceBundle findBundleInCache(CacheKey cacheKey,
jtulach@1334
  1394
                                                          Control control) {
jtulach@1334
  1395
        BundleReference bundleRef = cacheList.get(cacheKey);
jtulach@1334
  1396
        if (bundleRef == null) {
jtulach@1334
  1397
            return null;
jtulach@1334
  1398
        }
jtulach@1334
  1399
        ResourceBundle bundle = bundleRef.get();
jtulach@1334
  1400
        if (bundle == null) {
jtulach@1334
  1401
            return null;
jtulach@1334
  1402
        }
jtulach@1334
  1403
        ResourceBundle p = bundle.parent;
jtulach@1334
  1404
        assert p != NONEXISTENT_BUNDLE;
jtulach@1334
  1405
        // If the parent has expired, then this one must also expire. We
jtulach@1334
  1406
        // check only the immediate parent because the actual loading is
jtulach@1334
  1407
        // done from the root (base) to leaf (child) and the purpose of
jtulach@1334
  1408
        // checking is to propagate expiration towards the leaf. For
jtulach@1334
  1409
        // example, if the requested locale is ja_JP_JP and there are
jtulach@1334
  1410
        // bundles for all of the candidates in the cache, we have a list,
jtulach@1334
  1411
        //
jtulach@1334
  1412
        // base <- ja <- ja_JP <- ja_JP_JP
jtulach@1334
  1413
        //
jtulach@1334
  1414
        // If ja has expired, then it will reload ja and the list becomes a
jtulach@1334
  1415
        // tree.
jtulach@1334
  1416
        //
jtulach@1334
  1417
        // base <- ja (new)
jtulach@1334
  1418
        //  "   <- ja (expired) <- ja_JP <- ja_JP_JP
jtulach@1334
  1419
        //
jtulach@1334
  1420
        // When looking up ja_JP in the cache, it finds ja_JP in the cache
jtulach@1334
  1421
        // which references to the expired ja. Then, ja_JP is marked as
jtulach@1334
  1422
        // expired and removed from the cache. This will be propagated to
jtulach@1334
  1423
        // ja_JP_JP.
jtulach@1334
  1424
        //
jtulach@1334
  1425
        // Now, it's possible, for example, that while loading new ja_JP,
jtulach@1334
  1426
        // someone else has started loading the same bundle and finds the
jtulach@1334
  1427
        // base bundle has expired. Then, what we get from the first
jtulach@1334
  1428
        // getBundle call includes the expired base bundle. However, if
jtulach@1334
  1429
        // someone else didn't start its loading, we wouldn't know if the
jtulach@1334
  1430
        // base bundle has expired at the end of the loading process. The
jtulach@1334
  1431
        // expiration control doesn't guarantee that the returned bundle and
jtulach@1334
  1432
        // its parents haven't expired.
jtulach@1334
  1433
        //
jtulach@1334
  1434
        // We could check the entire parent chain to see if there's any in
jtulach@1334
  1435
        // the chain that has expired. But this process may never end. An
jtulach@1334
  1436
        // extreme case would be that getTimeToLive returns 0 and
jtulach@1334
  1437
        // needsReload always returns true.
jtulach@1334
  1438
        if (p != null && p.expired) {
jtulach@1334
  1439
            assert bundle != NONEXISTENT_BUNDLE;
jtulach@1334
  1440
            bundle.expired = true;
jtulach@1334
  1441
            bundle.cacheKey = null;
jaroslav@1337
  1442
            cacheList.remove(cacheKey);
jtulach@1334
  1443
            bundle = null;
jtulach@1334
  1444
        } else {
jtulach@1334
  1445
            CacheKey key = bundleRef.getCacheKey();
jtulach@1334
  1446
            long expirationTime = key.expirationTime;
jtulach@1334
  1447
            if (!bundle.expired && expirationTime >= 0 &&
jtulach@1334
  1448
                expirationTime <= System.currentTimeMillis()) {
jtulach@1334
  1449
                // its TTL period has expired.
jtulach@1334
  1450
                if (bundle != NONEXISTENT_BUNDLE) {
jtulach@1334
  1451
                    // Synchronize here to call needsReload to avoid
jtulach@1334
  1452
                    // redundant concurrent calls for the same bundle.
jtulach@1334
  1453
                    synchronized (bundle) {
jtulach@1334
  1454
                        expirationTime = key.expirationTime;
jtulach@1334
  1455
                        if (!bundle.expired && expirationTime >= 0 &&
jtulach@1334
  1456
                            expirationTime <= System.currentTimeMillis()) {
jtulach@1334
  1457
                            try {
jtulach@1334
  1458
                                bundle.expired = control.needsReload(key.getName(),
jtulach@1334
  1459
                                                                     key.getLocale(),
jtulach@1334
  1460
                                                                     key.getFormat(),
jaroslav@1337
  1461
                                                                     null,
jtulach@1334
  1462
                                                                     bundle,
jtulach@1334
  1463
                                                                     key.loadTime);
jtulach@1334
  1464
                            } catch (Exception e) {
jtulach@1334
  1465
                                cacheKey.setCause(e);
jtulach@1334
  1466
                            }
jtulach@1334
  1467
                            if (bundle.expired) {
jtulach@1334
  1468
                                // If the bundle needs to be reloaded, then
jtulach@1334
  1469
                                // remove the bundle from the cache, but
jtulach@1334
  1470
                                // return the bundle with the expired flag
jtulach@1334
  1471
                                // on.
jtulach@1334
  1472
                                bundle.cacheKey = null;
jaroslav@1337
  1473
                                cacheList.remove(cacheKey);
jtulach@1334
  1474
                            } else {
jtulach@1334
  1475
                                // Update the expiration control info. and reuse
jtulach@1334
  1476
                                // the same bundle instance
jtulach@1334
  1477
                                setExpirationTime(key, control);
jtulach@1334
  1478
                            }
jtulach@1334
  1479
                        }
jtulach@1334
  1480
                    }
jtulach@1334
  1481
                } else {
jtulach@1334
  1482
                    // We just remove NONEXISTENT_BUNDLE from the cache.
jaroslav@1337
  1483
                    cacheList.remove(cacheKey);
jtulach@1334
  1484
                    bundle = null;
jtulach@1334
  1485
                }
jtulach@1334
  1486
            }
jtulach@1334
  1487
        }
jtulach@1334
  1488
        return bundle;
jtulach@1334
  1489
    }
jtulach@1334
  1490
jtulach@1334
  1491
    /**
jtulach@1334
  1492
     * Put a new bundle in the cache.
jtulach@1334
  1493
     *
jtulach@1334
  1494
     * @param cacheKey the key for the resource bundle
jtulach@1334
  1495
     * @param bundle the resource bundle to be put in the cache
jtulach@1334
  1496
     * @return the ResourceBundle for the cacheKey; if someone has put
jtulach@1334
  1497
     * the bundle before this call, the one found in the cache is
jtulach@1334
  1498
     * returned.
jtulach@1334
  1499
     */
jtulach@1334
  1500
    private static final ResourceBundle putBundleInCache(CacheKey cacheKey,
jtulach@1334
  1501
                                                         ResourceBundle bundle,
jtulach@1334
  1502
                                                         Control control) {
jtulach@1334
  1503
        setExpirationTime(cacheKey, control);
jtulach@1334
  1504
        if (cacheKey.expirationTime != Control.TTL_DONT_CACHE) {
jtulach@1334
  1505
            CacheKey key = (CacheKey) cacheKey.clone();
jtulach@1334
  1506
            BundleReference bundleRef = new BundleReference(bundle, referenceQueue, key);
jtulach@1334
  1507
            bundle.cacheKey = key;
jtulach@1334
  1508
jtulach@1334
  1509
            // Put the bundle in the cache if it's not been in the cache.
jaroslav@1337
  1510
            BundleReference result = cacheList.put(key, bundleRef);
jtulach@1334
  1511
jtulach@1334
  1512
            // If someone else has put the same bundle in the cache before
jtulach@1334
  1513
            // us and it has not expired, we should use the one in the cache.
jtulach@1334
  1514
            if (result != null) {
jtulach@1334
  1515
                ResourceBundle rb = result.get();
jtulach@1334
  1516
                if (rb != null && !rb.expired) {
jtulach@1334
  1517
                    // Clear the back link to the cache key
jtulach@1334
  1518
                    bundle.cacheKey = null;
jtulach@1334
  1519
                    bundle = rb;
jtulach@1334
  1520
                    // Clear the reference in the BundleReference so that
jtulach@1334
  1521
                    // it won't be enqueued.
jtulach@1334
  1522
                    bundleRef.clear();
jtulach@1334
  1523
                } else {
jtulach@1334
  1524
                    // Replace the invalid (garbage collected or expired)
jtulach@1334
  1525
                    // instance with the valid one.
jtulach@1334
  1526
                    cacheList.put(key, bundleRef);
jtulach@1334
  1527
                }
jtulach@1334
  1528
            }
jtulach@1334
  1529
        }
jtulach@1334
  1530
        return bundle;
jtulach@1334
  1531
    }
jtulach@1334
  1532
jtulach@1334
  1533
    private static final void setExpirationTime(CacheKey cacheKey, Control control) {
jtulach@1334
  1534
        long ttl = control.getTimeToLive(cacheKey.getName(),
jtulach@1334
  1535
                                         cacheKey.getLocale());
jtulach@1334
  1536
        if (ttl >= 0) {
jtulach@1334
  1537
            // If any expiration time is specified, set the time to be
jtulach@1334
  1538
            // expired in the cache.
jtulach@1334
  1539
            long now = System.currentTimeMillis();
jtulach@1334
  1540
            cacheKey.loadTime = now;
jtulach@1334
  1541
            cacheKey.expirationTime = now + ttl;
jtulach@1334
  1542
        } else if (ttl >= Control.TTL_NO_EXPIRATION_CONTROL) {
jtulach@1334
  1543
            cacheKey.expirationTime = ttl;
jtulach@1334
  1544
        } else {
jtulach@1334
  1545
            throw new IllegalArgumentException("Invalid Control: TTL=" + ttl);
jtulach@1334
  1546
        }
jtulach@1334
  1547
    }
jtulach@1334
  1548
jtulach@1334
  1549
    /**
jtulach@1334
  1550
     * Removes all resource bundles from the cache that have been loaded
jtulach@1334
  1551
     * using the caller's class loader.
jtulach@1334
  1552
     *
jtulach@1334
  1553
     * @since 1.6
jtulach@1334
  1554
     * @see ResourceBundle.Control#getTimeToLive(String,Locale)
jtulach@1334
  1555
     */
jtulach@1334
  1556
    public static final void clearCache() {
jaroslav@1337
  1557
        clearCache(null);
jtulach@1334
  1558
    }
jtulach@1334
  1559
jtulach@1334
  1560
    /**
jtulach@1334
  1561
     * Removes all resource bundles from the cache that have been loaded
jtulach@1334
  1562
     * using the given class loader.
jtulach@1334
  1563
     *
jtulach@1334
  1564
     * @param loader the class loader
jtulach@1334
  1565
     * @exception NullPointerException if <code>loader</code> is null
jtulach@1334
  1566
     * @since 1.6
jtulach@1334
  1567
     * @see ResourceBundle.Control#getTimeToLive(String,Locale)
jtulach@1334
  1568
     */
jtulach@1334
  1569
    public static final void clearCache(ClassLoader loader) {
jtulach@1334
  1570
        if (loader == null) {
jtulach@1334
  1571
            throw new NullPointerException();
jtulach@1334
  1572
        }
jtulach@1334
  1573
        Set<CacheKey> set = cacheList.keySet();
jtulach@1334
  1574
        for (CacheKey key : set) {
jaroslav@1337
  1575
            set.remove(key);
jtulach@1334
  1576
        }
jtulach@1334
  1577
    }
jtulach@1334
  1578
jtulach@1334
  1579
    /**
jtulach@1334
  1580
     * Gets an object for the given key from this resource bundle.
jtulach@1334
  1581
     * Returns null if this resource bundle does not contain an
jtulach@1334
  1582
     * object for the given key.
jtulach@1334
  1583
     *
jtulach@1334
  1584
     * @param key the key for the desired object
jtulach@1334
  1585
     * @exception NullPointerException if <code>key</code> is <code>null</code>
jtulach@1334
  1586
     * @return the object for the given key, or null
jtulach@1334
  1587
     */
jtulach@1334
  1588
    protected abstract Object handleGetObject(String key);
jtulach@1334
  1589
jtulach@1334
  1590
    /**
jtulach@1334
  1591
     * Returns an enumeration of the keys.
jtulach@1334
  1592
     *
jtulach@1334
  1593
     * @return an <code>Enumeration</code> of the keys contained in
jtulach@1334
  1594
     *         this <code>ResourceBundle</code> and its parent bundles.
jtulach@1334
  1595
     */
jtulach@1334
  1596
    public abstract Enumeration<String> getKeys();
jtulach@1334
  1597
jtulach@1334
  1598
    /**
jtulach@1334
  1599
     * Determines whether the given <code>key</code> is contained in
jtulach@1334
  1600
     * this <code>ResourceBundle</code> or its parent bundles.
jtulach@1334
  1601
     *
jtulach@1334
  1602
     * @param key
jtulach@1334
  1603
     *        the resource <code>key</code>
jtulach@1334
  1604
     * @return <code>true</code> if the given <code>key</code> is
jtulach@1334
  1605
     *        contained in this <code>ResourceBundle</code> or its
jtulach@1334
  1606
     *        parent bundles; <code>false</code> otherwise.
jtulach@1334
  1607
     * @exception NullPointerException
jtulach@1334
  1608
     *         if <code>key</code> is <code>null</code>
jtulach@1334
  1609
     * @since 1.6
jtulach@1334
  1610
     */
jtulach@1334
  1611
    public boolean containsKey(String key) {
jtulach@1334
  1612
        if (key == null) {
jtulach@1334
  1613
            throw new NullPointerException();
jtulach@1334
  1614
        }
jtulach@1334
  1615
        for (ResourceBundle rb = this; rb != null; rb = rb.parent) {
jtulach@1334
  1616
            if (rb.handleKeySet().contains(key)) {
jtulach@1334
  1617
                return true;
jtulach@1334
  1618
            }
jtulach@1334
  1619
        }
jtulach@1334
  1620
        return false;
jtulach@1334
  1621
    }
jtulach@1334
  1622
jtulach@1334
  1623
    /**
jtulach@1334
  1624
     * Returns a <code>Set</code> of all keys contained in this
jtulach@1334
  1625
     * <code>ResourceBundle</code> and its parent bundles.
jtulach@1334
  1626
     *
jtulach@1334
  1627
     * @return a <code>Set</code> of all keys contained in this
jtulach@1334
  1628
     *         <code>ResourceBundle</code> and its parent bundles.
jtulach@1334
  1629
     * @since 1.6
jtulach@1334
  1630
     */
jtulach@1334
  1631
    public Set<String> keySet() {
jtulach@1334
  1632
        Set<String> keys = new HashSet<>();
jtulach@1334
  1633
        for (ResourceBundle rb = this; rb != null; rb = rb.parent) {
jtulach@1334
  1634
            keys.addAll(rb.handleKeySet());
jtulach@1334
  1635
        }
jtulach@1334
  1636
        return keys;
jtulach@1334
  1637
    }
jtulach@1334
  1638
jtulach@1334
  1639
    /**
jtulach@1334
  1640
     * Returns a <code>Set</code> of the keys contained <em>only</em>
jtulach@1334
  1641
     * in this <code>ResourceBundle</code>.
jtulach@1334
  1642
     *
jtulach@1334
  1643
     * <p>The default implementation returns a <code>Set</code> of the
jtulach@1334
  1644
     * keys returned by the {@link #getKeys() getKeys} method except
jtulach@1334
  1645
     * for the ones for which the {@link #handleGetObject(String)
jtulach@1334
  1646
     * handleGetObject} method returns <code>null</code>. Once the
jtulach@1334
  1647
     * <code>Set</code> has been created, the value is kept in this
jtulach@1334
  1648
     * <code>ResourceBundle</code> in order to avoid producing the
jtulach@1334
  1649
     * same <code>Set</code> in subsequent calls. Subclasses can
jtulach@1334
  1650
     * override this method for faster handling.
jtulach@1334
  1651
     *
jtulach@1334
  1652
     * @return a <code>Set</code> of the keys contained only in this
jtulach@1334
  1653
     *        <code>ResourceBundle</code>
jtulach@1334
  1654
     * @since 1.6
jtulach@1334
  1655
     */
jtulach@1334
  1656
    protected Set<String> handleKeySet() {
jtulach@1334
  1657
        if (keySet == null) {
jtulach@1334
  1658
            synchronized (this) {
jtulach@1334
  1659
                if (keySet == null) {
jtulach@1334
  1660
                    Set<String> keys = new HashSet<>();
jtulach@1334
  1661
                    Enumeration<String> enumKeys = getKeys();
jtulach@1334
  1662
                    while (enumKeys.hasMoreElements()) {
jtulach@1334
  1663
                        String key = enumKeys.nextElement();
jtulach@1334
  1664
                        if (handleGetObject(key) != null) {
jtulach@1334
  1665
                            keys.add(key);
jtulach@1334
  1666
                        }
jtulach@1334
  1667
                    }
jtulach@1334
  1668
                    keySet = keys;
jtulach@1334
  1669
                }
jtulach@1334
  1670
            }
jtulach@1334
  1671
        }
jtulach@1334
  1672
        return keySet;
jtulach@1334
  1673
    }
jtulach@1334
  1674
jtulach@1334
  1675
jtulach@1334
  1676
jtulach@1334
  1677
    /**
jtulach@1334
  1678
     * <code>ResourceBundle.Control</code> defines a set of callback methods
jtulach@1334
  1679
     * that are invoked by the {@link ResourceBundle#getBundle(String,
jtulach@1334
  1680
     * Locale, ClassLoader, Control) ResourceBundle.getBundle} factory
jtulach@1334
  1681
     * methods during the bundle loading process. In other words, a
jtulach@1334
  1682
     * <code>ResourceBundle.Control</code> collaborates with the factory
jtulach@1334
  1683
     * methods for loading resource bundles. The default implementation of
jtulach@1334
  1684
     * the callback methods provides the information necessary for the
jtulach@1334
  1685
     * factory methods to perform the <a
jtulach@1334
  1686
     * href="./ResourceBundle.html#default_behavior">default behavior</a>.
jtulach@1334
  1687
     *
jtulach@1334
  1688
     * <p>In addition to the callback methods, the {@link
jtulach@1334
  1689
     * #toBundleName(String, Locale) toBundleName} and {@link
jtulach@1334
  1690
     * #toResourceName(String, String) toResourceName} methods are defined
jtulach@1334
  1691
     * primarily for convenience in implementing the callback
jtulach@1334
  1692
     * methods. However, the <code>toBundleName</code> method could be
jtulach@1334
  1693
     * overridden to provide different conventions in the organization and
jtulach@1334
  1694
     * packaging of localized resources.  The <code>toResourceName</code>
jtulach@1334
  1695
     * method is <code>final</code> to avoid use of wrong resource and class
jtulach@1334
  1696
     * name separators.
jtulach@1334
  1697
     *
jtulach@1334
  1698
     * <p>Two factory methods, {@link #getControl(List)} and {@link
jtulach@1334
  1699
     * #getNoFallbackControl(List)}, provide
jtulach@1334
  1700
     * <code>ResourceBundle.Control</code> instances that implement common
jtulach@1334
  1701
     * variations of the default bundle loading process.
jtulach@1334
  1702
     *
jtulach@1334
  1703
     * <p>The formats returned by the {@link Control#getFormats(String)
jtulach@1334
  1704
     * getFormats} method and candidate locales returned by the {@link
jtulach@1334
  1705
     * ResourceBundle.Control#getCandidateLocales(String, Locale)
jtulach@1334
  1706
     * getCandidateLocales} method must be consistent in all
jtulach@1334
  1707
     * <code>ResourceBundle.getBundle</code> invocations for the same base
jtulach@1334
  1708
     * bundle. Otherwise, the <code>ResourceBundle.getBundle</code> methods
jtulach@1334
  1709
     * may return unintended bundles. For example, if only
jtulach@1334
  1710
     * <code>"java.class"</code> is returned by the <code>getFormats</code>
jtulach@1334
  1711
     * method for the first call to <code>ResourceBundle.getBundle</code>
jtulach@1334
  1712
     * and only <code>"java.properties"</code> for the second call, then the
jtulach@1334
  1713
     * second call will return the class-based one that has been cached
jtulach@1334
  1714
     * during the first call.
jtulach@1334
  1715
     *
jtulach@1334
  1716
     * <p>A <code>ResourceBundle.Control</code> instance must be thread-safe
jtulach@1334
  1717
     * if it's simultaneously used by multiple threads.
jtulach@1334
  1718
     * <code>ResourceBundle.getBundle</code> does not synchronize to call
jtulach@1334
  1719
     * the <code>ResourceBundle.Control</code> methods. The default
jtulach@1334
  1720
     * implementations of the methods are thread-safe.
jtulach@1334
  1721
     *
jtulach@1334
  1722
     * <p>Applications can specify <code>ResourceBundle.Control</code>
jtulach@1334
  1723
     * instances returned by the <code>getControl</code> factory methods or
jtulach@1334
  1724
     * created from a subclass of <code>ResourceBundle.Control</code> to
jtulach@1334
  1725
     * customize the bundle loading process. The following are examples of
jtulach@1334
  1726
     * changing the default bundle loading process.
jtulach@1334
  1727
     *
jtulach@1334
  1728
     * <p><b>Example 1</b>
jtulach@1334
  1729
     *
jtulach@1334
  1730
     * <p>The following code lets <code>ResourceBundle.getBundle</code> look
jtulach@1334
  1731
     * up only properties-based resources.
jtulach@1334
  1732
     *
jtulach@1334
  1733
     * <pre>
jtulach@1334
  1734
     * import java.util.*;
jtulach@1334
  1735
     * import static java.util.ResourceBundle.Control.*;
jtulach@1334
  1736
     * ...
jtulach@1334
  1737
     * ResourceBundle bundle =
jtulach@1334
  1738
     *   ResourceBundle.getBundle("MyResources", new Locale("fr", "CH"),
jtulach@1334
  1739
     *                            ResourceBundle.Control.getControl(FORMAT_PROPERTIES));
jtulach@1334
  1740
     * </pre>
jtulach@1334
  1741
     *
jtulach@1334
  1742
     * Given the resource bundles in the <a
jtulach@1334
  1743
     * href="./ResourceBundle.html#default_behavior_example">example</a> in
jtulach@1334
  1744
     * the <code>ResourceBundle.getBundle</code> description, this
jtulach@1334
  1745
     * <code>ResourceBundle.getBundle</code> call loads
jtulach@1334
  1746
     * <code>MyResources_fr_CH.properties</code> whose parent is
jtulach@1334
  1747
     * <code>MyResources_fr.properties</code> whose parent is
jtulach@1334
  1748
     * <code>MyResources.properties</code>. (<code>MyResources_fr_CH.properties</code>
jtulach@1334
  1749
     * is not hidden, but <code>MyResources_fr_CH.class</code> is.)
jtulach@1334
  1750
     *
jtulach@1334
  1751
     * <p><b>Example 2</b>
jtulach@1334
  1752
     *
jtulach@1334
  1753
     * <p>The following is an example of loading XML-based bundles
jtulach@1334
  1754
     * using {@link Properties#loadFromXML(java.io.InputStream)
jtulach@1334
  1755
     * Properties.loadFromXML}.
jtulach@1334
  1756
     *
jtulach@1334
  1757
     * <pre>
jtulach@1334
  1758
     * ResourceBundle rb = ResourceBundle.getBundle("Messages",
jtulach@1334
  1759
     *     new ResourceBundle.Control() {
jtulach@1334
  1760
     *         public List&lt;String&gt; getFormats(String baseName) {
jtulach@1334
  1761
     *             if (baseName == null)
jtulach@1334
  1762
     *                 throw new NullPointerException();
jtulach@1334
  1763
     *             return Arrays.asList("xml");
jtulach@1334
  1764
     *         }
jtulach@1334
  1765
     *         public ResourceBundle newBundle(String baseName,
jtulach@1334
  1766
     *                                         Locale locale,
jtulach@1334
  1767
     *                                         String format,
jtulach@1334
  1768
     *                                         ClassLoader loader,
jtulach@1334
  1769
     *                                         boolean reload)
jtulach@1334
  1770
     *                          throws IllegalAccessException,
jtulach@1334
  1771
     *                                 InstantiationException,
jtulach@1334
  1772
     *                                 IOException {
jtulach@1334
  1773
     *             if (baseName == null || locale == null
jtulach@1334
  1774
     *                   || format == null || loader == null)
jtulach@1334
  1775
     *                 throw new NullPointerException();
jtulach@1334
  1776
     *             ResourceBundle bundle = null;
jtulach@1334
  1777
     *             if (format.equals("xml")) {
jtulach@1334
  1778
     *                 String bundleName = toBundleName(baseName, locale);
jtulach@1334
  1779
     *                 String resourceName = toResourceName(bundleName, format);
jtulach@1334
  1780
     *                 InputStream stream = null;
jtulach@1334
  1781
     *                 if (reload) {
jtulach@1334
  1782
     *                     URL url = loader.getResource(resourceName);
jtulach@1334
  1783
     *                     if (url != null) {
jtulach@1334
  1784
     *                         URLConnection connection = url.openConnection();
jtulach@1334
  1785
     *                         if (connection != null) {
jtulach@1334
  1786
     *                             // Disable caches to get fresh data for
jtulach@1334
  1787
     *                             // reloading.
jtulach@1334
  1788
     *                             connection.setUseCaches(false);
jtulach@1334
  1789
     *                             stream = connection.getInputStream();
jtulach@1334
  1790
     *                         }
jtulach@1334
  1791
     *                     }
jtulach@1334
  1792
     *                 } else {
jtulach@1334
  1793
     *                     stream = loader.getResourceAsStream(resourceName);
jtulach@1334
  1794
     *                 }
jtulach@1334
  1795
     *                 if (stream != null) {
jtulach@1334
  1796
     *                     BufferedInputStream bis = new BufferedInputStream(stream);
jtulach@1334
  1797
     *                     bundle = new XMLResourceBundle(bis);
jtulach@1334
  1798
     *                     bis.close();
jtulach@1334
  1799
     *                 }
jtulach@1334
  1800
     *             }
jtulach@1334
  1801
     *             return bundle;
jtulach@1334
  1802
     *         }
jtulach@1334
  1803
     *     });
jtulach@1334
  1804
     *
jtulach@1334
  1805
     * ...
jtulach@1334
  1806
     *
jtulach@1334
  1807
     * private static class XMLResourceBundle extends ResourceBundle {
jtulach@1334
  1808
     *     private Properties props;
jtulach@1334
  1809
     *     XMLResourceBundle(InputStream stream) throws IOException {
jtulach@1334
  1810
     *         props = new Properties();
jtulach@1334
  1811
     *         props.loadFromXML(stream);
jtulach@1334
  1812
     *     }
jtulach@1334
  1813
     *     protected Object handleGetObject(String key) {
jtulach@1334
  1814
     *         return props.getProperty(key);
jtulach@1334
  1815
     *     }
jtulach@1334
  1816
     *     public Enumeration&lt;String&gt; getKeys() {
jtulach@1334
  1817
     *         ...
jtulach@1334
  1818
     *     }
jtulach@1334
  1819
     * }
jtulach@1334
  1820
     * </pre>
jtulach@1334
  1821
     *
jtulach@1334
  1822
     * @since 1.6
jtulach@1334
  1823
     */
jtulach@1334
  1824
    public static class Control {
jtulach@1334
  1825
        /**
jtulach@1334
  1826
         * The default format <code>List</code>, which contains the strings
jtulach@1334
  1827
         * <code>"java.class"</code> and <code>"java.properties"</code>, in
jtulach@1334
  1828
         * this order. This <code>List</code> is {@linkplain
jtulach@1334
  1829
         * Collections#unmodifiableList(List) unmodifiable}.
jtulach@1334
  1830
         *
jtulach@1334
  1831
         * @see #getFormats(String)
jtulach@1334
  1832
         */
jtulach@1334
  1833
        public static final List<String> FORMAT_DEFAULT
jtulach@1334
  1834
            = Collections.unmodifiableList(Arrays.asList("java.class",
jtulach@1334
  1835
                                                         "java.properties"));
jtulach@1334
  1836
jtulach@1334
  1837
        /**
jtulach@1334
  1838
         * The class-only format <code>List</code> containing
jtulach@1334
  1839
         * <code>"java.class"</code>. This <code>List</code> is {@linkplain
jtulach@1334
  1840
         * Collections#unmodifiableList(List) unmodifiable}.
jtulach@1334
  1841
         *
jtulach@1334
  1842
         * @see #getFormats(String)
jtulach@1334
  1843
         */
jtulach@1334
  1844
        public static final List<String> FORMAT_CLASS
jtulach@1334
  1845
            = Collections.unmodifiableList(Arrays.asList("java.class"));
jtulach@1334
  1846
jtulach@1334
  1847
        /**
jtulach@1334
  1848
         * The properties-only format <code>List</code> containing
jtulach@1334
  1849
         * <code>"java.properties"</code>. This <code>List</code> is
jtulach@1334
  1850
         * {@linkplain Collections#unmodifiableList(List) unmodifiable}.
jtulach@1334
  1851
         *
jtulach@1334
  1852
         * @see #getFormats(String)
jtulach@1334
  1853
         */
jtulach@1334
  1854
        public static final List<String> FORMAT_PROPERTIES
jtulach@1334
  1855
            = Collections.unmodifiableList(Arrays.asList("java.properties"));
jtulach@1334
  1856
jtulach@1334
  1857
        /**
jtulach@1334
  1858
         * The time-to-live constant for not caching loaded resource bundle
jtulach@1334
  1859
         * instances.
jtulach@1334
  1860
         *
jtulach@1334
  1861
         * @see #getTimeToLive(String, Locale)
jtulach@1334
  1862
         */
jtulach@1334
  1863
        public static final long TTL_DONT_CACHE = -1;
jtulach@1334
  1864
jtulach@1334
  1865
        /**
jtulach@1334
  1866
         * The time-to-live constant for disabling the expiration control
jtulach@1334
  1867
         * for loaded resource bundle instances in the cache.
jtulach@1334
  1868
         *
jtulach@1334
  1869
         * @see #getTimeToLive(String, Locale)
jtulach@1334
  1870
         */
jtulach@1334
  1871
        public static final long TTL_NO_EXPIRATION_CONTROL = -2;
jtulach@1334
  1872
jtulach@1334
  1873
        private static final Control INSTANCE = new Control();
jtulach@1334
  1874
jtulach@1334
  1875
        /**
jtulach@1334
  1876
         * Sole constructor. (For invocation by subclass constructors,
jtulach@1334
  1877
         * typically implicit.)
jtulach@1334
  1878
         */
jtulach@1334
  1879
        protected Control() {
jtulach@1334
  1880
        }
jtulach@1334
  1881
jtulach@1334
  1882
        /**
jtulach@1334
  1883
         * Returns a <code>ResourceBundle.Control</code> in which the {@link
jtulach@1334
  1884
         * #getFormats(String) getFormats} method returns the specified
jtulach@1334
  1885
         * <code>formats</code>. The <code>formats</code> must be equal to
jtulach@1334
  1886
         * one of {@link Control#FORMAT_PROPERTIES}, {@link
jtulach@1334
  1887
         * Control#FORMAT_CLASS} or {@link
jtulach@1334
  1888
         * Control#FORMAT_DEFAULT}. <code>ResourceBundle.Control</code>
jtulach@1334
  1889
         * instances returned by this method are singletons and thread-safe.
jtulach@1334
  1890
         *
jtulach@1334
  1891
         * <p>Specifying {@link Control#FORMAT_DEFAULT} is equivalent to
jtulach@1334
  1892
         * instantiating the <code>ResourceBundle.Control</code> class,
jtulach@1334
  1893
         * except that this method returns a singleton.
jtulach@1334
  1894
         *
jtulach@1334
  1895
         * @param formats
jtulach@1334
  1896
         *        the formats to be returned by the
jtulach@1334
  1897
         *        <code>ResourceBundle.Control.getFormats</code> method
jtulach@1334
  1898
         * @return a <code>ResourceBundle.Control</code> supporting the
jtulach@1334
  1899
         *        specified <code>formats</code>
jtulach@1334
  1900
         * @exception NullPointerException
jtulach@1334
  1901
         *        if <code>formats</code> is <code>null</code>
jtulach@1334
  1902
         * @exception IllegalArgumentException
jtulach@1334
  1903
         *        if <code>formats</code> is unknown
jtulach@1334
  1904
         */
jtulach@1334
  1905
        public static final Control getControl(List<String> formats) {
jtulach@1334
  1906
            if (formats.equals(Control.FORMAT_PROPERTIES)) {
jtulach@1334
  1907
                return SingleFormatControl.PROPERTIES_ONLY;
jtulach@1334
  1908
            }
jtulach@1334
  1909
            if (formats.equals(Control.FORMAT_CLASS)) {
jtulach@1334
  1910
                return SingleFormatControl.CLASS_ONLY;
jtulach@1334
  1911
            }
jtulach@1334
  1912
            if (formats.equals(Control.FORMAT_DEFAULT)) {
jtulach@1334
  1913
                return Control.INSTANCE;
jtulach@1334
  1914
            }
jtulach@1334
  1915
            throw new IllegalArgumentException();
jtulach@1334
  1916
        }
jtulach@1334
  1917
jtulach@1334
  1918
        /**
jtulach@1334
  1919
         * Returns a <code>ResourceBundle.Control</code> in which the {@link
jtulach@1334
  1920
         * #getFormats(String) getFormats} method returns the specified
jtulach@1334
  1921
         * <code>formats</code> and the {@link
jtulach@1334
  1922
         * Control#getFallbackLocale(String, Locale) getFallbackLocale}
jtulach@1334
  1923
         * method returns <code>null</code>. The <code>formats</code> must
jtulach@1334
  1924
         * be equal to one of {@link Control#FORMAT_PROPERTIES}, {@link
jtulach@1334
  1925
         * Control#FORMAT_CLASS} or {@link Control#FORMAT_DEFAULT}.
jtulach@1334
  1926
         * <code>ResourceBundle.Control</code> instances returned by this
jtulach@1334
  1927
         * method are singletons and thread-safe.
jtulach@1334
  1928
         *
jtulach@1334
  1929
         * @param formats
jtulach@1334
  1930
         *        the formats to be returned by the
jtulach@1334
  1931
         *        <code>ResourceBundle.Control.getFormats</code> method
jtulach@1334
  1932
         * @return a <code>ResourceBundle.Control</code> supporting the
jtulach@1334
  1933
         *        specified <code>formats</code> with no fallback
jtulach@1334
  1934
         *        <code>Locale</code> support
jtulach@1334
  1935
         * @exception NullPointerException
jtulach@1334
  1936
         *        if <code>formats</code> is <code>null</code>
jtulach@1334
  1937
         * @exception IllegalArgumentException
jtulach@1334
  1938
         *        if <code>formats</code> is unknown
jtulach@1334
  1939
         */
jtulach@1334
  1940
        public static final Control getNoFallbackControl(List<String> formats) {
jtulach@1334
  1941
            if (formats.equals(Control.FORMAT_DEFAULT)) {
jtulach@1334
  1942
                return NoFallbackControl.NO_FALLBACK;
jtulach@1334
  1943
            }
jtulach@1334
  1944
            if (formats.equals(Control.FORMAT_PROPERTIES)) {
jtulach@1334
  1945
                return NoFallbackControl.PROPERTIES_ONLY_NO_FALLBACK;
jtulach@1334
  1946
            }
jtulach@1334
  1947
            if (formats.equals(Control.FORMAT_CLASS)) {
jtulach@1334
  1948
                return NoFallbackControl.CLASS_ONLY_NO_FALLBACK;
jtulach@1334
  1949
            }
jtulach@1334
  1950
            throw new IllegalArgumentException();
jtulach@1334
  1951
        }
jtulach@1334
  1952
jtulach@1334
  1953
        /**
jtulach@1334
  1954
         * Returns a <code>List</code> of <code>String</code>s containing
jtulach@1334
  1955
         * formats to be used to load resource bundles for the given
jtulach@1334
  1956
         * <code>baseName</code>. The <code>ResourceBundle.getBundle</code>
jtulach@1334
  1957
         * factory method tries to load resource bundles with formats in the
jtulach@1334
  1958
         * order specified by the list. The list returned by this method
jtulach@1334
  1959
         * must have at least one <code>String</code>. The predefined
jtulach@1334
  1960
         * formats are <code>"java.class"</code> for class-based resource
jtulach@1334
  1961
         * bundles and <code>"java.properties"</code> for {@linkplain
jtulach@1334
  1962
         * PropertyResourceBundle properties-based} ones. Strings starting
jtulach@1334
  1963
         * with <code>"java."</code> are reserved for future extensions and
jtulach@1334
  1964
         * must not be used by application-defined formats.
jtulach@1334
  1965
         *
jtulach@1334
  1966
         * <p>It is not a requirement to return an immutable (unmodifiable)
jtulach@1334
  1967
         * <code>List</code>.  However, the returned <code>List</code> must
jtulach@1334
  1968
         * not be mutated after it has been returned by
jtulach@1334
  1969
         * <code>getFormats</code>.
jtulach@1334
  1970
         *
jtulach@1334
  1971
         * <p>The default implementation returns {@link #FORMAT_DEFAULT} so
jtulach@1334
  1972
         * that the <code>ResourceBundle.getBundle</code> factory method
jtulach@1334
  1973
         * looks up first class-based resource bundles, then
jtulach@1334
  1974
         * properties-based ones.
jtulach@1334
  1975
         *
jtulach@1334
  1976
         * @param baseName
jtulach@1334
  1977
         *        the base name of the resource bundle, a fully qualified class
jtulach@1334
  1978
         *        name
jtulach@1334
  1979
         * @return a <code>List</code> of <code>String</code>s containing
jtulach@1334
  1980
         *        formats for loading resource bundles.
jtulach@1334
  1981
         * @exception NullPointerException
jtulach@1334
  1982
         *        if <code>baseName</code> is null
jtulach@1334
  1983
         * @see #FORMAT_DEFAULT
jtulach@1334
  1984
         * @see #FORMAT_CLASS
jtulach@1334
  1985
         * @see #FORMAT_PROPERTIES
jtulach@1334
  1986
         */
jtulach@1334
  1987
        public List<String> getFormats(String baseName) {
jtulach@1334
  1988
            if (baseName == null) {
jtulach@1334
  1989
                throw new NullPointerException();
jtulach@1334
  1990
            }
jtulach@1334
  1991
            return FORMAT_DEFAULT;
jtulach@1334
  1992
        }
jtulach@1334
  1993
jtulach@1334
  1994
        /**
jtulach@1334
  1995
         * Returns a <code>List</code> of <code>Locale</code>s as candidate
jtulach@1334
  1996
         * locales for <code>baseName</code> and <code>locale</code>. This
jtulach@1334
  1997
         * method is called by the <code>ResourceBundle.getBundle</code>
jtulach@1334
  1998
         * factory method each time the factory method tries finding a
jtulach@1334
  1999
         * resource bundle for a target <code>Locale</code>.
jtulach@1334
  2000
         *
jtulach@1334
  2001
         * <p>The sequence of the candidate locales also corresponds to the
jtulach@1334
  2002
         * runtime resource lookup path (also known as the <I>parent
jtulach@1334
  2003
         * chain</I>), if the corresponding resource bundles for the
jtulach@1334
  2004
         * candidate locales exist and their parents are not defined by
jtulach@1334
  2005
         * loaded resource bundles themselves.  The last element of the list
jtulach@1334
  2006
         * must be a {@linkplain Locale#ROOT root locale} if it is desired to
jtulach@1334
  2007
         * have the base bundle as the terminal of the parent chain.
jtulach@1334
  2008
         *
jtulach@1334
  2009
         * <p>If the given locale is equal to <code>Locale.ROOT</code> (the
jtulach@1334
  2010
         * root locale), a <code>List</code> containing only the root
jtulach@1334
  2011
         * <code>Locale</code> must be returned. In this case, the
jtulach@1334
  2012
         * <code>ResourceBundle.getBundle</code> factory method loads only
jtulach@1334
  2013
         * the base bundle as the resulting resource bundle.
jtulach@1334
  2014
         *
jtulach@1334
  2015
         * <p>It is not a requirement to return an immutable (unmodifiable)
jtulach@1334
  2016
         * <code>List</code>. However, the returned <code>List</code> must not
jtulach@1334
  2017
         * be mutated after it has been returned by
jtulach@1334
  2018
         * <code>getCandidateLocales</code>.
jtulach@1334
  2019
         *
jtulach@1334
  2020
         * <p>The default implementation returns a <code>List</code> containing
jtulach@1334
  2021
         * <code>Locale</code>s using the rules described below.  In the
jtulach@1334
  2022
         * description below, <em>L</em>, <em>S</em>, <em>C</em> and <em>V</em>
jtulach@1334
  2023
         * respectively represent non-empty language, script, country, and
jtulach@1334
  2024
         * variant.  For example, [<em>L</em>, <em>C</em>] represents a
jtulach@1334
  2025
         * <code>Locale</code> that has non-empty values only for language and
jtulach@1334
  2026
         * country.  The form <em>L</em>("xx") represents the (non-empty)
jtulach@1334
  2027
         * language value is "xx".  For all cases, <code>Locale</code>s whose
jtulach@1334
  2028
         * final component values are empty strings are omitted.
jtulach@1334
  2029
         *
jtulach@1334
  2030
         * <ol><li>For an input <code>Locale</code> with an empty script value,
jtulach@1334
  2031
         * append candidate <code>Locale</code>s by omitting the final component
jtulach@1334
  2032
         * one by one as below:
jtulach@1334
  2033
         *
jtulach@1334
  2034
         * <ul>
jtulach@1334
  2035
         * <li> [<em>L</em>, <em>C</em>, <em>V</em>]
jtulach@1334
  2036
         * <li> [<em>L</em>, <em>C</em>]
jtulach@1334
  2037
         * <li> [<em>L</em>]
jtulach@1334
  2038
         * <li> <code>Locale.ROOT</code>
jtulach@1334
  2039
         * </ul>
jtulach@1334
  2040
         *
jtulach@1334
  2041
         * <li>For an input <code>Locale</code> with a non-empty script value,
jtulach@1334
  2042
         * append candidate <code>Locale</code>s by omitting the final component
jtulach@1334
  2043
         * up to language, then append candidates generated from the
jtulach@1334
  2044
         * <code>Locale</code> with country and variant restored:
jtulach@1334
  2045
         *
jtulach@1334
  2046
         * <ul>
jtulach@1334
  2047
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V</em>]
jtulach@1334
  2048
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>]
jtulach@1334
  2049
         * <li> [<em>L</em>, <em>S</em>]
jtulach@1334
  2050
         * <li> [<em>L</em>, <em>C</em>, <em>V</em>]
jtulach@1334
  2051
         * <li> [<em>L</em>, <em>C</em>]
jtulach@1334
  2052
         * <li> [<em>L</em>]
jtulach@1334
  2053
         * <li> <code>Locale.ROOT</code>
jtulach@1334
  2054
         * </ul>
jtulach@1334
  2055
         *
jtulach@1334
  2056
         * <li>For an input <code>Locale</code> with a variant value consisting
jtulach@1334
  2057
         * of multiple subtags separated by underscore, generate candidate
jtulach@1334
  2058
         * <code>Locale</code>s by omitting the variant subtags one by one, then
jtulach@1334
  2059
         * insert them after every occurence of <code> Locale</code>s with the
jtulach@1334
  2060
         * full variant value in the original list.  For example, if the
jtulach@1334
  2061
         * the variant consists of two subtags <em>V1</em> and <em>V2</em>:
jtulach@1334
  2062
         *
jtulach@1334
  2063
         * <ul>
jtulach@1334
  2064
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V1</em>, <em>V2</em>]
jtulach@1334
  2065
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V1</em>]
jtulach@1334
  2066
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>]
jtulach@1334
  2067
         * <li> [<em>L</em>, <em>S</em>]
jtulach@1334
  2068
         * <li> [<em>L</em>, <em>C</em>, <em>V1</em>, <em>V2</em>]
jtulach@1334
  2069
         * <li> [<em>L</em>, <em>C</em>, <em>V1</em>]
jtulach@1334
  2070
         * <li> [<em>L</em>, <em>C</em>]
jtulach@1334
  2071
         * <li> [<em>L</em>]
jtulach@1334
  2072
         * <li> <code>Locale.ROOT</code>
jtulach@1334
  2073
         * </ul>
jtulach@1334
  2074
         *
jtulach@1334
  2075
         * <li>Special cases for Chinese.  When an input <code>Locale</code> has the
jtulach@1334
  2076
         * language "zh" (Chinese) and an empty script value, either "Hans" (Simplified) or
jtulach@1334
  2077
         * "Hant" (Traditional) might be supplied, depending on the country.
jtulach@1334
  2078
         * When the country is "CN" (China) or "SG" (Singapore), "Hans" is supplied.
jtulach@1334
  2079
         * When the country is "HK" (Hong Kong SAR China), "MO" (Macau SAR China),
jtulach@1334
  2080
         * or "TW" (Taiwan), "Hant" is supplied.  For all other countries or when the country
jtulach@1334
  2081
         * is empty, no script is supplied.  For example, for <code>Locale("zh", "CN")
jtulach@1334
  2082
         * </code>, the candidate list will be:
jtulach@1334
  2083
         * <ul>
jtulach@1334
  2084
         * <li> [<em>L</em>("zh"), <em>S</em>("Hans"), <em>C</em>("CN")]
jtulach@1334
  2085
         * <li> [<em>L</em>("zh"), <em>S</em>("Hans")]
jtulach@1334
  2086
         * <li> [<em>L</em>("zh"), <em>C</em>("CN")]
jtulach@1334
  2087
         * <li> [<em>L</em>("zh")]
jtulach@1334
  2088
         * <li> <code>Locale.ROOT</code>
jtulach@1334
  2089
         * </ul>
jtulach@1334
  2090
         *
jtulach@1334
  2091
         * For <code>Locale("zh", "TW")</code>, the candidate list will be:
jtulach@1334
  2092
         * <ul>
jtulach@1334
  2093
         * <li> [<em>L</em>("zh"), <em>S</em>("Hant"), <em>C</em>("TW")]
jtulach@1334
  2094
         * <li> [<em>L</em>("zh"), <em>S</em>("Hant")]
jtulach@1334
  2095
         * <li> [<em>L</em>("zh"), <em>C</em>("TW")]
jtulach@1334
  2096
         * <li> [<em>L</em>("zh")]
jtulach@1334
  2097
         * <li> <code>Locale.ROOT</code>
jtulach@1334
  2098
         * </ul>
jtulach@1334
  2099
         *
jtulach@1334
  2100
         * <li>Special cases for Norwegian.  Both <code>Locale("no", "NO",
jtulach@1334
  2101
         * "NY")</code> and <code>Locale("nn", "NO")</code> represent Norwegian
jtulach@1334
  2102
         * Nynorsk.  When a locale's language is "nn", the standard candidate
jtulach@1334
  2103
         * list is generated up to [<em>L</em>("nn")], and then the following
jtulach@1334
  2104
         * candidates are added:
jtulach@1334
  2105
         *
jtulach@1334
  2106
         * <ul><li> [<em>L</em>("no"), <em>C</em>("NO"), <em>V</em>("NY")]
jtulach@1334
  2107
         * <li> [<em>L</em>("no"), <em>C</em>("NO")]
jtulach@1334
  2108
         * <li> [<em>L</em>("no")]
jtulach@1334
  2109
         * <li> <code>Locale.ROOT</code>
jtulach@1334
  2110
         * </ul>
jtulach@1334
  2111
         *
jtulach@1334
  2112
         * If the locale is exactly <code>Locale("no", "NO", "NY")</code>, it is first
jtulach@1334
  2113
         * converted to <code>Locale("nn", "NO")</code> and then the above procedure is
jtulach@1334
  2114
         * followed.
jtulach@1334
  2115
         *
jtulach@1334
  2116
         * <p>Also, Java treats the language "no" as a synonym of Norwegian
jtulach@1334
  2117
         * Bokm&#xE5;l "nb".  Except for the single case <code>Locale("no",
jtulach@1334
  2118
         * "NO", "NY")</code> (handled above), when an input <code>Locale</code>
jtulach@1334
  2119
         * has language "no" or "nb", candidate <code>Locale</code>s with
jtulach@1334
  2120
         * language code "no" and "nb" are interleaved, first using the
jtulach@1334
  2121
         * requested language, then using its synonym. For example,
jtulach@1334
  2122
         * <code>Locale("nb", "NO", "POSIX")</code> generates the following
jtulach@1334
  2123
         * candidate list:
jtulach@1334
  2124
         *
jtulach@1334
  2125
         * <ul>
jtulach@1334
  2126
         * <li> [<em>L</em>("nb"), <em>C</em>("NO"), <em>V</em>("POSIX")]
jtulach@1334
  2127
         * <li> [<em>L</em>("no"), <em>C</em>("NO"), <em>V</em>("POSIX")]
jtulach@1334
  2128
         * <li> [<em>L</em>("nb"), <em>C</em>("NO")]
jtulach@1334
  2129
         * <li> [<em>L</em>("no"), <em>C</em>("NO")]
jtulach@1334
  2130
         * <li> [<em>L</em>("nb")]
jtulach@1334
  2131
         * <li> [<em>L</em>("no")]
jtulach@1334
  2132
         * <li> <code>Locale.ROOT</code>
jtulach@1334
  2133
         * </ul>
jtulach@1334
  2134
         *
jtulach@1334
  2135
         * <code>Locale("no", "NO", "POSIX")</code> would generate the same list
jtulach@1334
  2136
         * except that locales with "no" would appear before the corresponding
jtulach@1334
  2137
         * locales with "nb".</li>
jtulach@1334
  2138
         *
jtulach@1334
  2139
         * </li>
jtulach@1334
  2140
         * </ol>
jtulach@1334
  2141
         *
jtulach@1334
  2142
         * <p>The default implementation uses an {@link ArrayList} that
jtulach@1334
  2143
         * overriding implementations may modify before returning it to the
jtulach@1334
  2144
         * caller. However, a subclass must not modify it after it has
jtulach@1334
  2145
         * been returned by <code>getCandidateLocales</code>.
jtulach@1334
  2146
         *
jtulach@1334
  2147
         * <p>For example, if the given <code>baseName</code> is "Messages"
jtulach@1334
  2148
         * and the given <code>locale</code> is
jtulach@1334
  2149
         * <code>Locale("ja",&nbsp;"",&nbsp;"XX")</code>, then a
jtulach@1334
  2150
         * <code>List</code> of <code>Locale</code>s:
jtulach@1334
  2151
         * <pre>
jtulach@1334
  2152
         *     Locale("ja", "", "XX")
jtulach@1334
  2153
         *     Locale("ja")
jtulach@1334
  2154
         *     Locale.ROOT
jtulach@1334
  2155
         * </pre>
jtulach@1334
  2156
         * is returned. And if the resource bundles for the "ja" and
jtulach@1334
  2157
         * "" <code>Locale</code>s are found, then the runtime resource
jtulach@1334
  2158
         * lookup path (parent chain) is:
jtulach@1334
  2159
         * <pre>
jtulach@1334
  2160
         *     Messages_ja -> Messages
jtulach@1334
  2161
         * </pre>
jtulach@1334
  2162
         *
jtulach@1334
  2163
         * @param baseName
jtulach@1334
  2164
         *        the base name of the resource bundle, a fully
jtulach@1334
  2165
         *        qualified class name
jtulach@1334
  2166
         * @param locale
jtulach@1334
  2167
         *        the locale for which a resource bundle is desired
jtulach@1334
  2168
         * @return a <code>List</code> of candidate
jtulach@1334
  2169
         *        <code>Locale</code>s for the given <code>locale</code>
jtulach@1334
  2170
         * @exception NullPointerException
jtulach@1334
  2171
         *        if <code>baseName</code> or <code>locale</code> is
jtulach@1334
  2172
         *        <code>null</code>
jtulach@1334
  2173
         */
jtulach@1334
  2174
        public List<Locale> getCandidateLocales(String baseName, Locale locale) {
jtulach@1334
  2175
            if (baseName == null) {
jtulach@1334
  2176
                throw new NullPointerException();
jtulach@1334
  2177
            }
jaroslav@1337
  2178
            return new ArrayList<>(CANDIDATES_CACHE.get(locale));
jtulach@1334
  2179
        }
jtulach@1334
  2180
jtulach@1334
  2181
        private static final CandidateListCache CANDIDATES_CACHE = new CandidateListCache();
jtulach@1334
  2182
jaroslav@1337
  2183
        private static class CandidateListCache {
jaroslav@1337
  2184
            private Locale prevQuery;
jaroslav@1337
  2185
            private List<Locale> prevResult;
jaroslav@1337
  2186
            
jaroslav@1337
  2187
            public List<Locale> get(Locale l) {
jaroslav@1337
  2188
                if (prevQuery == l) {
jaroslav@1337
  2189
                    return prevResult;
jaroslav@1337
  2190
                }
jaroslav@1337
  2191
                prevResult = createObject(l);
jaroslav@1337
  2192
                prevQuery = l;
jaroslav@1337
  2193
                return prevResult;
jaroslav@1337
  2194
            }
jaroslav@1337
  2195
            
jaroslav@1337
  2196
            protected List<Locale> createObject(Locale base) {
jtulach@1334
  2197
                String language = base.getLanguage();
jtulach@1334
  2198
                String script = base.getScript();
jtulach@1334
  2199
                String region = base.getRegion();
jtulach@1334
  2200
                String variant = base.getVariant();
jtulach@1334
  2201
jtulach@1334
  2202
                // Special handling for Norwegian
jtulach@1334
  2203
                boolean isNorwegianBokmal = false;
jtulach@1334
  2204
                boolean isNorwegianNynorsk = false;
jtulach@1334
  2205
                if (language.equals("no")) {
jtulach@1334
  2206
                    if (region.equals("NO") && variant.equals("NY")) {
jtulach@1334
  2207
                        variant = "";
jtulach@1334
  2208
                        isNorwegianNynorsk = true;
jtulach@1334
  2209
                    } else {
jtulach@1334
  2210
                        isNorwegianBokmal = true;
jtulach@1334
  2211
                    }
jtulach@1334
  2212
                }
jtulach@1334
  2213
                if (language.equals("nb") || isNorwegianBokmal) {
jtulach@1334
  2214
                    List<Locale> tmpList = getDefaultList("nb", script, region, variant);
jtulach@1334
  2215
                    // Insert a locale replacing "nb" with "no" for every list entry
jtulach@1334
  2216
                    List<Locale> bokmalList = new LinkedList<>();
jtulach@1334
  2217
                    for (Locale l : tmpList) {
jtulach@1334
  2218
                        bokmalList.add(l);
jtulach@1334
  2219
                        if (l.getLanguage().length() == 0) {
jtulach@1334
  2220
                            break;
jtulach@1334
  2221
                        }
jtulach@1334
  2222
                        bokmalList.add(Locale.getInstance("no", l.getScript(), l.getCountry(),
jtulach@1334
  2223
                                l.getVariant(), null));
jtulach@1334
  2224
                    }
jtulach@1334
  2225
                    return bokmalList;
jtulach@1334
  2226
                } else if (language.equals("nn") || isNorwegianNynorsk) {
jtulach@1334
  2227
                    // Insert no_NO_NY, no_NO, no after nn
jtulach@1334
  2228
                    List<Locale> nynorskList = getDefaultList("nn", script, region, variant);
jtulach@1334
  2229
                    int idx = nynorskList.size() - 1;
jtulach@1334
  2230
                    nynorskList.add(idx++, Locale.getInstance("no", "NO", "NY"));
jtulach@1334
  2231
                    nynorskList.add(idx++, Locale.getInstance("no", "NO", ""));
jtulach@1334
  2232
                    nynorskList.add(idx++, Locale.getInstance("no", "", ""));
jtulach@1334
  2233
                    return nynorskList;
jtulach@1334
  2234
                }
jtulach@1334
  2235
                // Special handling for Chinese
jtulach@1334
  2236
                else if (language.equals("zh")) {
jtulach@1334
  2237
                    if (script.length() == 0 && region.length() > 0) {
jtulach@1334
  2238
                        // Supply script for users who want to use zh_Hans/zh_Hant
jtulach@1334
  2239
                        // as bundle names (recommended for Java7+)
jtulach@1334
  2240
                        if (region.equals("TW") || region.equals("HK") || region.equals("MO")) {
jtulach@1334
  2241
                            script = "Hant";
jtulach@1334
  2242
                        } else if (region.equals("CN") || region.equals("SG")) {
jtulach@1334
  2243
                            script = "Hans";
jtulach@1334
  2244
                        }
jtulach@1334
  2245
                    } else if (script.length() > 0 && region.length() == 0) {
jtulach@1334
  2246
                        // Supply region(country) for users who still package Chinese
jtulach@1334
  2247
                        // bundles using old convension.
jtulach@1334
  2248
                        if (script.equals("Hans")) {
jtulach@1334
  2249
                            region = "CN";
jtulach@1334
  2250
                        } else if (script.equals("Hant")) {
jtulach@1334
  2251
                            region = "TW";
jtulach@1334
  2252
                        }
jtulach@1334
  2253
                    }
jtulach@1334
  2254
                }
jtulach@1334
  2255
jtulach@1334
  2256
                return getDefaultList(language, script, region, variant);
jtulach@1334
  2257
            }
jtulach@1334
  2258
jtulach@1334
  2259
            private static List<Locale> getDefaultList(String language, String script, String region, String variant) {
jtulach@1334
  2260
                List<String> variants = null;
jtulach@1334
  2261
jtulach@1334
  2262
                if (variant.length() > 0) {
jtulach@1334
  2263
                    variants = new LinkedList<>();
jtulach@1334
  2264
                    int idx = variant.length();
jtulach@1334
  2265
                    while (idx != -1) {
jtulach@1334
  2266
                        variants.add(variant.substring(0, idx));
jtulach@1334
  2267
                        idx = variant.lastIndexOf('_', --idx);
jtulach@1334
  2268
                    }
jtulach@1334
  2269
                }
jtulach@1334
  2270
jtulach@1334
  2271
                List<Locale> list = new LinkedList<>();
jtulach@1334
  2272
jtulach@1334
  2273
                if (variants != null) {
jtulach@1334
  2274
                    for (String v : variants) {
jtulach@1334
  2275
                        list.add(Locale.getInstance(language, script, region, v, null));
jtulach@1334
  2276
                    }
jtulach@1334
  2277
                }
jtulach@1334
  2278
                if (region.length() > 0) {
jtulach@1334
  2279
                    list.add(Locale.getInstance(language, script, region, "", null));
jtulach@1334
  2280
                }
jtulach@1334
  2281
                if (script.length() > 0) {
jtulach@1334
  2282
                    list.add(Locale.getInstance(language, script, "", "", null));
jtulach@1334
  2283
jtulach@1334
  2284
                    // With script, after truncating variant, region and script,
jtulach@1334
  2285
                    // start over without script.
jtulach@1334
  2286
                    if (variants != null) {
jtulach@1334
  2287
                        for (String v : variants) {
jtulach@1334
  2288
                            list.add(Locale.getInstance(language, "", region, v, null));
jtulach@1334
  2289
                        }
jtulach@1334
  2290
                    }
jtulach@1334
  2291
                    if (region.length() > 0) {
jtulach@1334
  2292
                        list.add(Locale.getInstance(language, "", region, "", null));
jtulach@1334
  2293
                    }
jtulach@1334
  2294
                }
jtulach@1334
  2295
                if (language.length() > 0) {
jtulach@1334
  2296
                    list.add(Locale.getInstance(language, "", "", "", null));
jtulach@1334
  2297
                }
jtulach@1334
  2298
                // Add root locale at the end
jtulach@1334
  2299
                list.add(Locale.ROOT);
jtulach@1334
  2300
jtulach@1334
  2301
                return list;
jtulach@1334
  2302
            }
jtulach@1334
  2303
        }
jtulach@1334
  2304
jtulach@1334
  2305
        /**
jtulach@1334
  2306
         * Returns a <code>Locale</code> to be used as a fallback locale for
jtulach@1334
  2307
         * further resource bundle searches by the
jtulach@1334
  2308
         * <code>ResourceBundle.getBundle</code> factory method. This method
jtulach@1334
  2309
         * is called from the factory method every time when no resulting
jtulach@1334
  2310
         * resource bundle has been found for <code>baseName</code> and
jtulach@1334
  2311
         * <code>locale</code>, where locale is either the parameter for
jtulach@1334
  2312
         * <code>ResourceBundle.getBundle</code> or the previous fallback
jtulach@1334
  2313
         * locale returned by this method.
jtulach@1334
  2314
         *
jtulach@1334
  2315
         * <p>The method returns <code>null</code> if no further fallback
jtulach@1334
  2316
         * search is desired.
jtulach@1334
  2317
         *
jtulach@1334
  2318
         * <p>The default implementation returns the {@linkplain
jtulach@1334
  2319
         * Locale#getDefault() default <code>Locale</code>} if the given
jtulach@1334
  2320
         * <code>locale</code> isn't the default one.  Otherwise,
jtulach@1334
  2321
         * <code>null</code> is returned.
jtulach@1334
  2322
         *
jtulach@1334
  2323
         * @param baseName
jtulach@1334
  2324
         *        the base name of the resource bundle, a fully
jtulach@1334
  2325
         *        qualified class name for which
jtulach@1334
  2326
         *        <code>ResourceBundle.getBundle</code> has been
jtulach@1334
  2327
         *        unable to find any resource bundles (except for the
jtulach@1334
  2328
         *        base bundle)
jtulach@1334
  2329
         * @param locale
jtulach@1334
  2330
         *        the <code>Locale</code> for which
jtulach@1334
  2331
         *        <code>ResourceBundle.getBundle</code> has been
jtulach@1334
  2332
         *        unable to find any resource bundles (except for the
jtulach@1334
  2333
         *        base bundle)
jtulach@1334
  2334
         * @return a <code>Locale</code> for the fallback search,
jtulach@1334
  2335
         *        or <code>null</code> if no further fallback search
jtulach@1334
  2336
         *        is desired.
jtulach@1334
  2337
         * @exception NullPointerException
jtulach@1334
  2338
         *        if <code>baseName</code> or <code>locale</code>
jtulach@1334
  2339
         *        is <code>null</code>
jtulach@1334
  2340
         */
jtulach@1334
  2341
        public Locale getFallbackLocale(String baseName, Locale locale) {
jtulach@1334
  2342
            if (baseName == null) {
jtulach@1334
  2343
                throw new NullPointerException();
jtulach@1334
  2344
            }
jtulach@1334
  2345
            Locale defaultLocale = Locale.getDefault();
jtulach@1334
  2346
            return locale.equals(defaultLocale) ? null : defaultLocale;
jtulach@1334
  2347
        }
jtulach@1334
  2348
jtulach@1334
  2349
        /**
jtulach@1334
  2350
         * Instantiates a resource bundle for the given bundle name of the
jtulach@1334
  2351
         * given format and locale, using the given class loader if
jtulach@1334
  2352
         * necessary. This method returns <code>null</code> if there is no
jtulach@1334
  2353
         * resource bundle available for the given parameters. If a resource
jtulach@1334
  2354
         * bundle can't be instantiated due to an unexpected error, the
jtulach@1334
  2355
         * error must be reported by throwing an <code>Error</code> or
jtulach@1334
  2356
         * <code>Exception</code> rather than simply returning
jtulach@1334
  2357
         * <code>null</code>.
jtulach@1334
  2358
         *
jtulach@1334
  2359
         * <p>If the <code>reload</code> flag is <code>true</code>, it
jtulach@1334
  2360
         * indicates that this method is being called because the previously
jtulach@1334
  2361
         * loaded resource bundle has expired.
jtulach@1334
  2362
         *
jtulach@1334
  2363
         * <p>The default implementation instantiates a
jtulach@1334
  2364
         * <code>ResourceBundle</code> as follows.
jtulach@1334
  2365
         *
jtulach@1334
  2366
         * <ul>
jtulach@1334
  2367
         *
jtulach@1334
  2368
         * <li>The bundle name is obtained by calling {@link
jtulach@1334
  2369
         * #toBundleName(String, Locale) toBundleName(baseName,
jtulach@1334
  2370
         * locale)}.</li>
jtulach@1334
  2371
         *
jtulach@1334
  2372
         * <li>If <code>format</code> is <code>"java.class"</code>, the
jtulach@1334
  2373
         * {@link Class} specified by the bundle name is loaded by calling
jtulach@1334
  2374
         * {@link ClassLoader#loadClass(String)}. Then, a
jtulach@1334
  2375
         * <code>ResourceBundle</code> is instantiated by calling {@link
jtulach@1334
  2376
         * Class#newInstance()}.  Note that the <code>reload</code> flag is
jtulach@1334
  2377
         * ignored for loading class-based resource bundles in this default
jtulach@1334
  2378
         * implementation.</li>
jtulach@1334
  2379
         *
jtulach@1334
  2380
         * <li>If <code>format</code> is <code>"java.properties"</code>,
jtulach@1334
  2381
         * {@link #toResourceName(String, String) toResourceName(bundlename,
jtulach@1334
  2382
         * "properties")} is called to get the resource name.
jtulach@1334
  2383
         * If <code>reload</code> is <code>true</code>, {@link
jtulach@1334
  2384
         * ClassLoader#getResource(String) load.getResource} is called
jtulach@1334
  2385
         * to get a {@link URL} for creating a {@link
jtulach@1334
  2386
         * URLConnection}. This <code>URLConnection</code> is used to
jtulach@1334
  2387
         * {@linkplain URLConnection#setUseCaches(boolean) disable the
jtulach@1334
  2388
         * caches} of the underlying resource loading layers,
jtulach@1334
  2389
         * and to {@linkplain URLConnection#getInputStream() get an
jtulach@1334
  2390
         * <code>InputStream</code>}.
jtulach@1334
  2391
         * Otherwise, {@link ClassLoader#getResourceAsStream(String)
jtulach@1334
  2392
         * loader.getResourceAsStream} is called to get an {@link
jtulach@1334
  2393
         * InputStream}. Then, a {@link
jtulach@1334
  2394
         * PropertyResourceBundle} is constructed with the
jtulach@1334
  2395
         * <code>InputStream</code>.</li>
jtulach@1334
  2396
         *
jtulach@1334
  2397
         * <li>If <code>format</code> is neither <code>"java.class"</code>
jtulach@1334
  2398
         * nor <code>"java.properties"</code>, an
jtulach@1334
  2399
         * <code>IllegalArgumentException</code> is thrown.</li>
jtulach@1334
  2400
         *
jtulach@1334
  2401
         * </ul>
jtulach@1334
  2402
         *
jtulach@1334
  2403
         * @param baseName
jtulach@1334
  2404
         *        the base bundle name of the resource bundle, a fully
jtulach@1334
  2405
         *        qualified class name
jtulach@1334
  2406
         * @param locale
jtulach@1334
  2407
         *        the locale for which the resource bundle should be
jtulach@1334
  2408
         *        instantiated
jtulach@1334
  2409
         * @param format
jtulach@1334
  2410
         *        the resource bundle format to be loaded
jtulach@1334
  2411
         * @param loader
jtulach@1334
  2412
         *        the <code>ClassLoader</code> to use to load the bundle
jtulach@1334
  2413
         * @param reload
jtulach@1334
  2414
         *        the flag to indicate bundle reloading; <code>true</code>
jtulach@1334
  2415
         *        if reloading an expired resource bundle,
jtulach@1334
  2416
         *        <code>false</code> otherwise
jtulach@1334
  2417
         * @return the resource bundle instance,
jtulach@1334
  2418
         *        or <code>null</code> if none could be found.
jtulach@1334
  2419
         * @exception NullPointerException
jtulach@1334
  2420
         *        if <code>bundleName</code>, <code>locale</code>,
jtulach@1334
  2421
         *        <code>format</code>, or <code>loader</code> is
jtulach@1334
  2422
         *        <code>null</code>, or if <code>null</code> is returned by
jtulach@1334
  2423
         *        {@link #toBundleName(String, Locale) toBundleName}
jtulach@1334
  2424
         * @exception IllegalArgumentException
jtulach@1334
  2425
         *        if <code>format</code> is unknown, or if the resource
jtulach@1334
  2426
         *        found for the given parameters contains malformed data.
jtulach@1334
  2427
         * @exception ClassCastException
jtulach@1334
  2428
         *        if the loaded class cannot be cast to <code>ResourceBundle</code>
jtulach@1334
  2429
         * @exception IllegalAccessException
jtulach@1334
  2430
         *        if the class or its nullary constructor is not
jtulach@1334
  2431
         *        accessible.
jtulach@1334
  2432
         * @exception InstantiationException
jtulach@1334
  2433
         *        if the instantiation of a class fails for some other
jtulach@1334
  2434
         *        reason.
jtulach@1334
  2435
         * @exception ExceptionInInitializerError
jtulach@1334
  2436
         *        if the initialization provoked by this method fails.
jtulach@1334
  2437
         * @exception SecurityException
jtulach@1334
  2438
         *        If a security manager is present and creation of new
jtulach@1334
  2439
         *        instances is denied. See {@link Class#newInstance()}
jtulach@1334
  2440
         *        for details.
jtulach@1334
  2441
         * @exception IOException
jtulach@1334
  2442
         *        if an error occurred when reading resources using
jtulach@1334
  2443
         *        any I/O operations
jtulach@1334
  2444
         */
jtulach@1334
  2445
        public ResourceBundle newBundle(String baseName, Locale locale, String format,
jtulach@1334
  2446
                                        ClassLoader loader, boolean reload)
jtulach@1334
  2447
                    throws IllegalAccessException, InstantiationException, IOException {
jtulach@1334
  2448
            String bundleName = toBundleName(baseName, locale);
jtulach@1334
  2449
            ResourceBundle bundle = null;
jtulach@1334
  2450
            if (format.equals("java.class")) {
jtulach@1334
  2451
                try {
jtulach@1334
  2452
                    Class<? extends ResourceBundle> bundleClass
jaroslav@1337
  2453
                        = (Class<? extends ResourceBundle>)(loader != null ? 
jaroslav@1337
  2454
                        loader.loadClass(bundleName) :
jaroslav@1337
  2455
                        Class.forName(bundleName));
jtulach@1334
  2456
jtulach@1334
  2457
                    // If the class isn't a ResourceBundle subclass, throw a
jtulach@1334
  2458
                    // ClassCastException.
jtulach@1334
  2459
                    if (ResourceBundle.class.isAssignableFrom(bundleClass)) {
jtulach@1334
  2460
                        bundle = bundleClass.newInstance();
jtulach@1334
  2461
                    } else {
jtulach@1334
  2462
                        throw new ClassCastException(bundleClass.getName()
jtulach@1334
  2463
                                     + " cannot be cast to ResourceBundle");
jtulach@1334
  2464
                    }
jtulach@1334
  2465
                } catch (ClassNotFoundException e) {
jtulach@1334
  2466
                }
jtulach@1334
  2467
            } else if (format.equals("java.properties")) {
jtulach@1334
  2468
                final String resourceName = toResourceName(bundleName, "properties");
jtulach@1334
  2469
                final ClassLoader classLoader = loader;
jtulach@1334
  2470
                final boolean reloadFlag = reload;
jaroslav@1337
  2471
                InputStream stream = classLoader != null ? classLoader.getResourceAsStream(resourceName) :
jaroslav@1337
  2472
                    ResourceBundle.class.getResourceAsStream("/" + resourceName);
jtulach@1334
  2473
                if (stream != null) {
jtulach@1334
  2474
                    try {
jtulach@1334
  2475
                        bundle = new PropertyResourceBundle(stream);
jtulach@1334
  2476
                    } finally {
jtulach@1334
  2477
                        stream.close();
jtulach@1334
  2478
                    }
jaroslav@1337
  2479
                }   
jtulach@1334
  2480
            } else {
jtulach@1334
  2481
                throw new IllegalArgumentException("unknown format: " + format);
jtulach@1334
  2482
            }
jtulach@1334
  2483
            return bundle;
jtulach@1334
  2484
        }
jtulach@1334
  2485
jtulach@1334
  2486
        /**
jtulach@1334
  2487
         * Returns the time-to-live (TTL) value for resource bundles that
jtulach@1334
  2488
         * are loaded under this
jtulach@1334
  2489
         * <code>ResourceBundle.Control</code>. Positive time-to-live values
jtulach@1334
  2490
         * specify the number of milliseconds a bundle can remain in the
jtulach@1334
  2491
         * cache without being validated against the source data from which
jtulach@1334
  2492
         * it was constructed. The value 0 indicates that a bundle must be
jtulach@1334
  2493
         * validated each time it is retrieved from the cache. {@link
jtulach@1334
  2494
         * #TTL_DONT_CACHE} specifies that loaded resource bundles are not
jtulach@1334
  2495
         * put in the cache. {@link #TTL_NO_EXPIRATION_CONTROL} specifies
jtulach@1334
  2496
         * that loaded resource bundles are put in the cache with no
jtulach@1334
  2497
         * expiration control.
jtulach@1334
  2498
         *
jtulach@1334
  2499
         * <p>The expiration affects only the bundle loading process by the
jtulach@1334
  2500
         * <code>ResourceBundle.getBundle</code> factory method.  That is,
jtulach@1334
  2501
         * if the factory method finds a resource bundle in the cache that
jtulach@1334
  2502
         * has expired, the factory method calls the {@link
jtulach@1334
  2503
         * #needsReload(String, Locale, String, ClassLoader, ResourceBundle,
jtulach@1334
  2504
         * long) needsReload} method to determine whether the resource
jtulach@1334
  2505
         * bundle needs to be reloaded. If <code>needsReload</code> returns
jtulach@1334
  2506
         * <code>true</code>, the cached resource bundle instance is removed
jtulach@1334
  2507
         * from the cache. Otherwise, the instance stays in the cache,
jtulach@1334
  2508
         * updated with the new TTL value returned by this method.
jtulach@1334
  2509
         *
jtulach@1334
  2510
         * <p>All cached resource bundles are subject to removal from the
jtulach@1334
  2511
         * cache due to memory constraints of the runtime environment.
jtulach@1334
  2512
         * Returning a large positive value doesn't mean to lock loaded
jtulach@1334
  2513
         * resource bundles in the cache.
jtulach@1334
  2514
         *
jtulach@1334
  2515
         * <p>The default implementation returns {@link #TTL_NO_EXPIRATION_CONTROL}.
jtulach@1334
  2516
         *
jtulach@1334
  2517
         * @param baseName
jtulach@1334
  2518
         *        the base name of the resource bundle for which the
jtulach@1334
  2519
         *        expiration value is specified.
jtulach@1334
  2520
         * @param locale
jtulach@1334
  2521
         *        the locale of the resource bundle for which the
jtulach@1334
  2522
         *        expiration value is specified.
jtulach@1334
  2523
         * @return the time (0 or a positive millisecond offset from the
jtulach@1334
  2524
         *        cached time) to get loaded bundles expired in the cache,
jtulach@1334
  2525
         *        {@link #TTL_NO_EXPIRATION_CONTROL} to disable the
jtulach@1334
  2526
         *        expiration control, or {@link #TTL_DONT_CACHE} to disable
jtulach@1334
  2527
         *        caching.
jtulach@1334
  2528
         * @exception NullPointerException
jtulach@1334
  2529
         *        if <code>baseName</code> or <code>locale</code> is
jtulach@1334
  2530
         *        <code>null</code>
jtulach@1334
  2531
         */
jtulach@1334
  2532
        public long getTimeToLive(String baseName, Locale locale) {
jtulach@1334
  2533
            if (baseName == null || locale == null) {
jtulach@1334
  2534
                throw new NullPointerException();
jtulach@1334
  2535
            }
jtulach@1334
  2536
            return TTL_NO_EXPIRATION_CONTROL;
jtulach@1334
  2537
        }
jtulach@1334
  2538
jtulach@1334
  2539
        /**
jtulach@1334
  2540
         * Determines if the expired <code>bundle</code> in the cache needs
jtulach@1334
  2541
         * to be reloaded based on the loading time given by
jtulach@1334
  2542
         * <code>loadTime</code> or some other criteria. The method returns
jtulach@1334
  2543
         * <code>true</code> if reloading is required; <code>false</code>
jtulach@1334
  2544
         * otherwise. <code>loadTime</code> is a millisecond offset since
jtulach@1334
  2545
         * the <a href="Calendar.html#Epoch"> <code>Calendar</code>
jtulach@1334
  2546
         * Epoch</a>.
jtulach@1334
  2547
         *
jtulach@1334
  2548
         * The calling <code>ResourceBundle.getBundle</code> factory method
jtulach@1334
  2549
         * calls this method on the <code>ResourceBundle.Control</code>
jtulach@1334
  2550
         * instance used for its current invocation, not on the instance
jtulach@1334
  2551
         * used in the invocation that originally loaded the resource
jtulach@1334
  2552
         * bundle.
jtulach@1334
  2553
         *
jtulach@1334
  2554
         * <p>The default implementation compares <code>loadTime</code> and
jtulach@1334
  2555
         * the last modified time of the source data of the resource
jtulach@1334
  2556
         * bundle. If it's determined that the source data has been modified
jtulach@1334
  2557
         * since <code>loadTime</code>, <code>true</code> is
jtulach@1334
  2558
         * returned. Otherwise, <code>false</code> is returned. This
jtulach@1334
  2559
         * implementation assumes that the given <code>format</code> is the
jtulach@1334
  2560
         * same string as its file suffix if it's not one of the default
jtulach@1334
  2561
         * formats, <code>"java.class"</code> or
jtulach@1334
  2562
         * <code>"java.properties"</code>.
jtulach@1334
  2563
         *
jtulach@1334
  2564
         * @param baseName
jtulach@1334
  2565
         *        the base bundle name of the resource bundle, a
jtulach@1334
  2566
         *        fully qualified class name
jtulach@1334
  2567
         * @param locale
jtulach@1334
  2568
         *        the locale for which the resource bundle
jtulach@1334
  2569
         *        should be instantiated
jtulach@1334
  2570
         * @param format
jtulach@1334
  2571
         *        the resource bundle format to be loaded
jtulach@1334
  2572
         * @param loader
jtulach@1334
  2573
         *        the <code>ClassLoader</code> to use to load the bundle
jtulach@1334
  2574
         * @param bundle
jtulach@1334
  2575
         *        the resource bundle instance that has been expired
jtulach@1334
  2576
         *        in the cache
jtulach@1334
  2577
         * @param loadTime
jtulach@1334
  2578
         *        the time when <code>bundle</code> was loaded and put
jtulach@1334
  2579
         *        in the cache
jtulach@1334
  2580
         * @return <code>true</code> if the expired bundle needs to be
jtulach@1334
  2581
         *        reloaded; <code>false</code> otherwise.
jtulach@1334
  2582
         * @exception NullPointerException
jtulach@1334
  2583
         *        if <code>baseName</code>, <code>locale</code>,
jtulach@1334
  2584
         *        <code>format</code>, <code>loader</code>, or
jtulach@1334
  2585
         *        <code>bundle</code> is <code>null</code>
jtulach@1334
  2586
         */
jtulach@1334
  2587
        public boolean needsReload(String baseName, Locale locale,
jtulach@1334
  2588
                                   String format, ClassLoader loader,
jtulach@1334
  2589
                                   ResourceBundle bundle, long loadTime) {
jtulach@1334
  2590
            if (bundle == null) {
jtulach@1334
  2591
                throw new NullPointerException();
jtulach@1334
  2592
            }
jtulach@1334
  2593
            if (format.equals("java.class") || format.equals("java.properties")) {
jtulach@1334
  2594
                format = format.substring(5);
jtulach@1334
  2595
            }
jtulach@1334
  2596
            boolean result = false;
jtulach@1334
  2597
            try {
jaroslav@1337
  2598
/*
jtulach@1334
  2599
                String resourceName = toResourceName(toBundleName(baseName, locale), format);
jtulach@1334
  2600
                URL url = loader.getResource(resourceName);
jtulach@1334
  2601
                if (url != null) {
jtulach@1334
  2602
                    long lastModified = 0;
jtulach@1334
  2603
                    URLConnection connection = url.openConnection();
jtulach@1334
  2604
                    if (connection != null) {
jtulach@1334
  2605
                        // disable caches to get the correct data
jtulach@1334
  2606
                        connection.setUseCaches(false);
jtulach@1334
  2607
                        if (connection instanceof JarURLConnection) {
jtulach@1334
  2608
                            JarEntry ent = ((JarURLConnection)connection).getJarEntry();
jtulach@1334
  2609
                            if (ent != null) {
jtulach@1334
  2610
                                lastModified = ent.getTime();
jtulach@1334
  2611
                                if (lastModified == -1) {
jtulach@1334
  2612
                                    lastModified = 0;
jtulach@1334
  2613
                                }
jtulach@1334
  2614
                            }
jtulach@1334
  2615
                        } else {
jtulach@1334
  2616
                            lastModified = connection.getLastModified();
jtulach@1334
  2617
                        }
jtulach@1334
  2618
                    }
jtulach@1334
  2619
                    result = lastModified >= loadTime;
jtulach@1334
  2620
                }
jaroslav@1337
  2621
                */
jtulach@1334
  2622
            } catch (NullPointerException npe) {
jtulach@1334
  2623
                throw npe;
jtulach@1334
  2624
            } catch (Exception e) {
jtulach@1334
  2625
                // ignore other exceptions
jtulach@1334
  2626
            }
jtulach@1334
  2627
            return result;
jtulach@1334
  2628
        }
jtulach@1334
  2629
jtulach@1334
  2630
        /**
jtulach@1334
  2631
         * Converts the given <code>baseName</code> and <code>locale</code>
jtulach@1334
  2632
         * to the bundle name. This method is called from the default
jtulach@1334
  2633
         * implementation of the {@link #newBundle(String, Locale, String,
jtulach@1334
  2634
         * ClassLoader, boolean) newBundle} and {@link #needsReload(String,
jtulach@1334
  2635
         * Locale, String, ClassLoader, ResourceBundle, long) needsReload}
jtulach@1334
  2636
         * methods.
jtulach@1334
  2637
         *
jtulach@1334
  2638
         * <p>This implementation returns the following value:
jtulach@1334
  2639
         * <pre>
jtulach@1334
  2640
         *     baseName + "_" + language + "_" + script + "_" + country + "_" + variant
jtulach@1334
  2641
         * </pre>
jtulach@1334
  2642
         * where <code>language</code>, <code>script</code>, <code>country</code>,
jtulach@1334
  2643
         * and <code>variant</code> are the language, script, country, and variant
jtulach@1334
  2644
         * values of <code>locale</code>, respectively. Final component values that
jtulach@1334
  2645
         * are empty Strings are omitted along with the preceding '_'.  When the
jtulach@1334
  2646
         * script is empty, the script value is ommitted along with the preceding '_'.
jtulach@1334
  2647
         * If all of the values are empty strings, then <code>baseName</code>
jtulach@1334
  2648
         * is returned.
jtulach@1334
  2649
         *
jtulach@1334
  2650
         * <p>For example, if <code>baseName</code> is
jtulach@1334
  2651
         * <code>"baseName"</code> and <code>locale</code> is
jtulach@1334
  2652
         * <code>Locale("ja",&nbsp;"",&nbsp;"XX")</code>, then
jtulach@1334
  2653
         * <code>"baseName_ja_&thinsp;_XX"</code> is returned. If the given
jtulach@1334
  2654
         * locale is <code>Locale("en")</code>, then
jtulach@1334
  2655
         * <code>"baseName_en"</code> is returned.
jtulach@1334
  2656
         *
jtulach@1334
  2657
         * <p>Overriding this method allows applications to use different
jtulach@1334
  2658
         * conventions in the organization and packaging of localized
jtulach@1334
  2659
         * resources.
jtulach@1334
  2660
         *
jtulach@1334
  2661
         * @param baseName
jtulach@1334
  2662
         *        the base name of the resource bundle, a fully
jtulach@1334
  2663
         *        qualified class name
jtulach@1334
  2664
         * @param locale
jtulach@1334
  2665
         *        the locale for which a resource bundle should be
jtulach@1334
  2666
         *        loaded
jtulach@1334
  2667
         * @return the bundle name for the resource bundle
jtulach@1334
  2668
         * @exception NullPointerException
jtulach@1334
  2669
         *        if <code>baseName</code> or <code>locale</code>
jtulach@1334
  2670
         *        is <code>null</code>
jtulach@1334
  2671
         */
jtulach@1334
  2672
        public String toBundleName(String baseName, Locale locale) {
jtulach@1334
  2673
            if (locale == Locale.ROOT) {
jtulach@1334
  2674
                return baseName;
jtulach@1334
  2675
            }
jtulach@1334
  2676
jtulach@1334
  2677
            String language = locale.getLanguage();
jtulach@1334
  2678
            String script = locale.getScript();
jtulach@1334
  2679
            String country = locale.getCountry();
jtulach@1334
  2680
            String variant = locale.getVariant();
jtulach@1334
  2681
jtulach@1334
  2682
            if (language == "" && country == "" && variant == "") {
jtulach@1334
  2683
                return baseName;
jtulach@1334
  2684
            }
jtulach@1334
  2685
jtulach@1334
  2686
            StringBuilder sb = new StringBuilder(baseName);
jtulach@1334
  2687
            sb.append('_');
jtulach@1334
  2688
            if (script != "") {
jtulach@1334
  2689
                if (variant != "") {
jtulach@1334
  2690
                    sb.append(language).append('_').append(script).append('_').append(country).append('_').append(variant);
jtulach@1334
  2691
                } else if (country != "") {
jtulach@1334
  2692
                    sb.append(language).append('_').append(script).append('_').append(country);
jtulach@1334
  2693
                } else {
jtulach@1334
  2694
                    sb.append(language).append('_').append(script);
jtulach@1334
  2695
                }
jtulach@1334
  2696
            } else {
jtulach@1334
  2697
                if (variant != "") {
jtulach@1334
  2698
                    sb.append(language).append('_').append(country).append('_').append(variant);
jtulach@1334
  2699
                } else if (country != "") {
jtulach@1334
  2700
                    sb.append(language).append('_').append(country);
jtulach@1334
  2701
                } else {
jtulach@1334
  2702
                    sb.append(language);
jtulach@1334
  2703
                }
jtulach@1334
  2704
            }
jtulach@1334
  2705
            return sb.toString();
jtulach@1334
  2706
jtulach@1334
  2707
        }
jtulach@1334
  2708
jtulach@1334
  2709
        /**
jtulach@1334
  2710
         * Converts the given <code>bundleName</code> to the form required
jtulach@1334
  2711
         * by the {@link ClassLoader#getResource ClassLoader.getResource}
jtulach@1334
  2712
         * method by replacing all occurrences of <code>'.'</code> in
jtulach@1334
  2713
         * <code>bundleName</code> with <code>'/'</code> and appending a
jtulach@1334
  2714
         * <code>'.'</code> and the given file <code>suffix</code>. For
jtulach@1334
  2715
         * example, if <code>bundleName</code> is
jtulach@1334
  2716
         * <code>"foo.bar.MyResources_ja_JP"</code> and <code>suffix</code>
jtulach@1334
  2717
         * is <code>"properties"</code>, then
jtulach@1334
  2718
         * <code>"foo/bar/MyResources_ja_JP.properties"</code> is returned.
jtulach@1334
  2719
         *
jtulach@1334
  2720
         * @param bundleName
jtulach@1334
  2721
         *        the bundle name
jtulach@1334
  2722
         * @param suffix
jtulach@1334
  2723
         *        the file type suffix
jtulach@1334
  2724
         * @return the converted resource name
jtulach@1334
  2725
         * @exception NullPointerException
jtulach@1334
  2726
         *         if <code>bundleName</code> or <code>suffix</code>
jtulach@1334
  2727
         *         is <code>null</code>
jtulach@1334
  2728
         */
jtulach@1334
  2729
        public final String toResourceName(String bundleName, String suffix) {
jtulach@1334
  2730
            StringBuilder sb = new StringBuilder(bundleName.length() + 1 + suffix.length());
jtulach@1334
  2731
            sb.append(bundleName.replace('.', '/')).append('.').append(suffix);
jtulach@1334
  2732
            return sb.toString();
jtulach@1334
  2733
        }
jtulach@1334
  2734
    }
jtulach@1334
  2735
jtulach@1334
  2736
    private static class SingleFormatControl extends Control {
jtulach@1334
  2737
        private static final Control PROPERTIES_ONLY
jtulach@1334
  2738
            = new SingleFormatControl(FORMAT_PROPERTIES);
jtulach@1334
  2739
jtulach@1334
  2740
        private static final Control CLASS_ONLY
jtulach@1334
  2741
            = new SingleFormatControl(FORMAT_CLASS);
jtulach@1334
  2742
jtulach@1334
  2743
        private final List<String> formats;
jtulach@1334
  2744
jtulach@1334
  2745
        protected SingleFormatControl(List<String> formats) {
jtulach@1334
  2746
            this.formats = formats;
jtulach@1334
  2747
        }
jtulach@1334
  2748
jtulach@1334
  2749
        public List<String> getFormats(String baseName) {
jtulach@1334
  2750
            if (baseName == null) {
jtulach@1334
  2751
                throw new NullPointerException();
jtulach@1334
  2752
            }
jtulach@1334
  2753
            return formats;
jtulach@1334
  2754
        }
jtulach@1334
  2755
    }
jtulach@1334
  2756
jtulach@1334
  2757
    private static final class NoFallbackControl extends SingleFormatControl {
jtulach@1334
  2758
        private static final Control NO_FALLBACK
jtulach@1334
  2759
            = new NoFallbackControl(FORMAT_DEFAULT);
jtulach@1334
  2760
jtulach@1334
  2761
        private static final Control PROPERTIES_ONLY_NO_FALLBACK
jtulach@1334
  2762
            = new NoFallbackControl(FORMAT_PROPERTIES);
jtulach@1334
  2763
jtulach@1334
  2764
        private static final Control CLASS_ONLY_NO_FALLBACK
jtulach@1334
  2765
            = new NoFallbackControl(FORMAT_CLASS);
jtulach@1334
  2766
jtulach@1334
  2767
        protected NoFallbackControl(List<String> formats) {
jtulach@1334
  2768
            super(formats);
jtulach@1334
  2769
        }
jtulach@1334
  2770
jtulach@1334
  2771
        public Locale getFallbackLocale(String baseName, Locale locale) {
jtulach@1334
  2772
            if (baseName == null || locale == null) {
jtulach@1334
  2773
                throw new NullPointerException();
jtulach@1334
  2774
            }
jtulach@1334
  2775
            return null;
jtulach@1334
  2776
        }
jtulach@1334
  2777
    }
jtulach@1334
  2778
}