resourcebundle.java

来自「java源代码 请看看啊 提点宝贵的意见」· Java 代码 · 共 1,129 行 · 第 1/4 页

JAVA
1,129
字号
/* * @(#)ResourceBundle.java	1.71 04/08/16 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved * * The original version of this source code and documentation * is copyrighted and owned by Taligent, Inc., a wholly-owned * subsidiary of IBM. These materials are provided under terms * of a License Agreement between Taligent and Sun. This technology * is protected by multiple US and International patents. * * This notice and attribution to Taligent may not be removed. * Taligent is a registered trademark of Taligent, Inc. * */package java.util;import java.io.InputStream;import java.lang.ref.Reference;import java.lang.ref.ReferenceQueue;import java.lang.ref.WeakReference;import sun.misc.SoftCache;/** * * Resource bundles contain locale-specific objects. * When your program needs a locale-specific resource, * a <code>String</code> for example, your program can load it * from the resource bundle that is appropriate for the * current user's locale. In this way, you can write * program code that is largely independent of the user's * locale isolating most, if not all, of the locale-specific * information in resource bundles. * * <p> * This allows you to write programs that can: * <UL type=SQUARE> * <LI> be easily localized, or translated, into different languages * <LI> handle multiple locales at once * <LI> be easily modified later to support even more locales * </UL> * * <P> * Resource bundles belong to families whose members share a common base  * name, but whose names also have additional components that identify  * their locales. For example, the base name of a family of resource  * bundles might be "MyResources". The family should have a default  * resource bundle which simply has the same name as its family -  * "MyResources" - and will be used as the bundle of last resort if a * specific locale is not supported. The family can then provide as * many locale-specific members as needed, for example a German one * named "MyResources_de". * * <P> * Each resource bundle in a family contains the same items, but the items have * been translated for the locale represented by that resource bundle. * For example, both "MyResources" and "MyResources_de" may have a * <code>String</code> that's used on a button for canceling operations. * In "MyResources" the <code>String</code> may contain "Cancel" and in * "MyResources_de" it may contain "Abbrechen". * * <P> * If there are different resources for different countries, you * can make specializations: for example, "MyResources_de_CH" contains objects for * the German language (de) in Switzerland (CH). If you want to only * modify some of the resources * in the specialization, you can do so. * * <P> * When your program needs a locale-specific object, it loads * the <code>ResourceBundle</code> class using the * {@link #getBundle(java.lang.String, java.util.Locale) getBundle} * method: * <blockquote> * <pre> * ResourceBundle myResources = *      ResourceBundle.getBundle("MyResources", currentLocale); * </pre> * </blockquote> * * <P> * Resource bundles contain key/value pairs. The keys uniquely * identify a locale-specific object in the bundle. Here's an * example of a <code>ListResourceBundle</code> that contains * two key/value pairs: * <blockquote> * <pre> * public class MyResources extends ListResourceBundle { *      public Object[][] getContents() { *              return contents; *      } *      static final Object[][] contents = { *      // LOCALIZE THIS *              {"OkKey", "OK"}, *              {"CancelKey", "Cancel"}, *      // END OF MATERIAL TO LOCALIZE *      }; * } * </pre> * </blockquote> * Keys are always <code>String</code>s. * In this example, the keys are "OkKey" and "CancelKey". * In the above example, the values * are also <code>String</code>s--"OK" and "Cancel"--but * they don't have to be. The values can be any type of object. * * <P> * You retrieve an object from resource bundle using the appropriate * getter method. Because "OkKey" and "CancelKey" * are both strings, you would use <code>getString</code> to retrieve them: * <blockquote> * <pre> * button1 = new Button(myResources.getString("OkKey")); * button2 = new Button(myResources.getString("CancelKey")); * </pre> * </blockquote> * The getter methods all require the key as an argument and return * the object if found. If the object is not found, the getter method * throws a <code>MissingResourceException</code>. * * <P> * Besides <code>getString</code>, ResourceBundle also provides * a method for getting string arrays, <code>getStringArray</code>, * as well as a generic <code>getObject</code> method for any other * type of object. When using <code>getObject</code>, you'll * have to cast the result to the appropriate type. For example: * <blockquote> * <pre> * int[] myIntegers = (int[]) myResources.getObject("intList"); * </pre> * </blockquote> * * <P> * The Java 2 platform provides two subclasses of <code>ResourceBundle</code>, * <code>ListResourceBundle</code> and <code>PropertyResourceBundle</code>, * that provide a fairly simple way to create resources. * As you saw briefly in a previous example, <code>ListResourceBundle</code> * manages its resource as a List of key/value pairs. * <code>PropertyResourceBundle</code> uses a properties file to manage * its resources. * * <p> * If <code>ListResourceBundle</code> or <code>PropertyResourceBundle</code> * do not suit your needs, you can write your own <code>ResourceBundle</code> * subclass.  Your subclasses must override two methods: <code>handleGetObject</code> * and <code>getKeys()</code>. * * <P> * The following is a very simple example of a <code>ResourceBundle</code> * subclass, MyResources, that manages two resources (for a larger number of * resources you would probably use a <code>Hashtable</code>). * Notice that you don't need to supply a value if  * a "parent-level" <code>ResourceBundle</code> handles the same * key with the same value (as for the okKey below). * <p><strong>Example:</strong> * <blockquote> * <pre> * // default (English language, United States) * public class MyResources extends ResourceBundle { *     public Object handleGetObject(String key) { *         if (key.equals("okKey")) return "Ok"; *         if (key.equals("cancelKey")) return "Cancel"; *         return null; *     } * } * * // German language * public class MyResources_de extends MyResources { *     public Object handleGetObject(String key) { *         // don't need okKey, since parent level handles it. *         if (key.equals("cancelKey")) return "Abbrechen"; *         return null; *     } * } * </pre> * </blockquote> * You do not have to restrict yourself to using a single family of * <code>ResourceBundle</code>s. For example, you could have a set of bundles for * exception messages, <code>ExceptionResources</code> * (<code>ExceptionResources_fr</code>, <code>ExceptionResources_de</code>, ...), * and one for widgets, <code>WidgetResource</code> (<code>WidgetResources_fr</code>, * <code>WidgetResources_de</code>, ...); breaking up the resources however you like. * * @see ListResourceBundle * @see PropertyResourceBundle * @see MissingResourceException * @since JDK1.1 */abstract public class ResourceBundle {    /**     * Static key used for resource lookups. Concurrent     * access to this object is controlled by synchronizing cacheList,     * not cacheKey.  A static object is used to do cache lookups     * for performance reasons - the assumption being that synchronization     * has a lower overhead than object allocation and subsequent     * garbage collection.     */    private static final ResourceCacheKey cacheKey = new ResourceCacheKey();    /** initial size of the bundle cache */    private static final int INITIAL_CACHE_SIZE = 25;    /** capacity of cache consumed before it should grow */    private static final float CACHE_LOAD_FACTOR = (float)1.0;    /**     * Maximum length of one branch of the resource search path tree.     * Used in getBundle.     */    private static final int MAX_BUNDLES_SEARCHED = 3;    /**     * This Hashtable is used to keep multiple threads from loading the     * same bundle concurrently.  The table entries are (cacheKey, thread)     * where cacheKey is the key for the bundle that is under construction     * and thread is the thread that is constructing the bundle.     * This list is manipulated in findBundle and putBundleInCache.     * Synchronization of this object is done through cacheList, not on     * this object.     */    private static final Hashtable underConstruction = new Hashtable(MAX_BUNDLES_SEARCHED, CACHE_LOAD_FACTOR);    /** constant indicating that no resource bundle was found */    private static final Object NOT_FOUND = new Object();    /**     * The cache is a map from cache keys (with bundle base name,     * locale, and class loader) to either a resource bundle     * (if one has been found) or NOT_FOUND (if no bundle has     * been found).     * The cache is a SoftCache, allowing bundles to be     * removed from the cache if they are no longer     * needed.  This will also allow the cache keys     * to be reclaimed along with the ClassLoaders     * they reference.     * This variable would be better named "cache", but we keep the old     * name for compatibility with some workarounds for bug 4212439.     */    private static SoftCache cacheList = new SoftCache(INITIAL_CACHE_SIZE, CACHE_LOAD_FACTOR);    /**     * Queue for reference objects referring to class loaders.     */    private static ReferenceQueue referenceQueue = new ReferenceQueue();    /**     * The parent bundle of this bundle.     * The parent bundle is searched by {@link #getObject getObject}     * when this bundle does not contain a particular resource.     */    protected ResourceBundle parent = null;    /**     * The locale for this bundle.     */    private Locale locale = null;    /**     * Sole constructor.  (For invocation by subclass constructors, typically     * implicit.)     */    public ResourceBundle() {    }    /**     * Gets a string for the given key from this resource bundle or one of its parents.     * Calling this method is equivalent to calling     * <blockquote>     * <code>(String) {@link #getObject(java.lang.String) getObject}(key)</code>.     * </blockquote>     *     * @param key the key for the desired string     * @exception NullPointerException if <code>key</code> is <code>null</code>     * @exception MissingResourceException if no object for the given key can be found     * @exception ClassCastException if the object found for the given key is not a string     * @return the string for the given key

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?