📄 resourcebundle.java
字号:
/* ResourceBundle -- aids in loading resource bundles Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.util;import gnu.classpath.VMStackWalker;import java.io.IOException;import java.io.InputStream;/** * A resource bundle contains locale-specific data. If you need localized * data, you can load a resource bundle that matches the locale with * <code>getBundle</code>. Now you can get your object by calling * <code>getObject</code> or <code>getString</code> on that bundle. * * <p>When a bundle is demanded for a specific locale, the ResourceBundle * is searched in following order (<i>def. language</i> stands for the * two letter ISO language code of the default locale (see * <code>Locale.getDefault()</code>). *<pre>baseName_<i>language code</i>_<i>country code</i>_<i>variant</i>baseName_<i>language code</i>_<i>country code</i>baseName_<i>language code</i>baseName_<i>def. language</i>_<i>def. country</i>_<i>def. variant</i>baseName_<i>def. language</i>_<i>def. country</i>baseName_<i>def. language</i>baseName</pre> * * <p>A bundle is backed up by less specific bundles (omitting variant, country * or language). But it is not backed up by the default language locale. * * <p>If you provide a bundle for a given locale, say * <code>Bundle_en_UK_POSIX</code>, you must also provide a bundle for * all sub locales, ie. <code>Bundle_en_UK</code>, <code>Bundle_en</code>, and * <code>Bundle</code>. * * <p>When a bundle is searched, we look first for a class with the given * name, then for a file with <code>.properties</code> extension in the * classpath. The name must be a fully qualified classname (with dots as * path separators). * * <p>(Note: This implementation always backs up the class with a properties * file if that is existing, but you shouldn't rely on this, if you want to * be compatible to the standard JDK.) * * @author Jochen Hoenicke * @author Eric Blake (ebb9@email.byu.edu) * @see Locale * @see ListResourceBundle * @see PropertyResourceBundle * @since 1.1 * @status updated to 1.4 */public abstract class ResourceBundle{ /** * The parent bundle. This is consulted when you call getObject and there * is no such resource in the current bundle. This field may be null. */ protected ResourceBundle parent; /** * The locale of this resource bundle. You can read this with * <code>getLocale</code> and it is automatically set in * <code>getBundle</code>. */ private Locale locale; /** * The resource bundle cache. */ private static Map bundleCache; /** * The last default Locale we saw. If this ever changes then we have to * reset our caches. */ private static Locale lastDefaultLocale; /** * The `empty' locale is created once in order to optimize * tryBundle(). */ private static final Locale emptyLocale = new Locale(""); /** * The constructor. It does nothing special. */ public ResourceBundle() { } /** * Get a String from this resource bundle. Since most localized Objects * are Strings, this method provides a convenient way to get them without * casting. * * @param key the name of the resource * @throws MissingResourceException if the resource can't be found * @throws NullPointerException if key is null * @throws ClassCastException if resource is not a string */ public final String getString(String key) { return (String) getObject(key); } /** * Get an array of Strings from this resource bundle. This method * provides a convenient way to get it without casting. * * @param key the name of the resource * @throws MissingResourceException if the resource can't be found * @throws NullPointerException if key is null * @throws ClassCastException if resource is not a string */ public final String[] getStringArray(String key) { return (String[]) getObject(key); } /** * Get an object from this resource bundle. This will call * <code>handleGetObject</code> for this resource and all of its parents, * until it finds a non-null resource. * * @param key the name of the resource * @throws MissingResourceException if the resource can't be found * @throws NullPointerException if key is null */ public final Object getObject(String key) { for (ResourceBundle bundle = this; bundle != null; bundle = bundle.parent) { Object o = bundle.handleGetObject(key); if (o != null) return o; } String className = getClass().getName(); throw new MissingResourceException("Key '" + key + "'not found in Bundle: " + className, className, key); } /** * Return the actual locale of this bundle. You can use it after calling * getBundle, to know if the bundle for the desired locale was loaded or * if the fall back was used. * * @return the bundle's locale */ public Locale getLocale() { return locale; } /** * Set the parent of this bundle. The parent is consulted when you call * getObject and there is no such resource in the current bundle. * * @param parent the parent of this bundle */ protected void setParent(ResourceBundle parent) { this.parent = parent; } /** * Get the appropriate ResourceBundle for the default locale. This is like * calling <code>getBundle(baseName, Locale.getDefault(), * getClass().getClassLoader()</code>, except that any security check of * getClassLoader won't fail. * * @param baseName the name of the ResourceBundle * @return the desired resource bundle * @throws MissingResourceException if the resource bundle can't be found * @throws NullPointerException if baseName is null */ public static ResourceBundle getBundle(String baseName) { ClassLoader cl = VMStackWalker.getCallingClassLoader(); if (cl == null) cl = ClassLoader.getSystemClassLoader(); return getBundle(baseName, Locale.getDefault(), cl); } /** * Get the appropriate ResourceBundle for the given locale. This is like * calling <code>getBundle(baseName, locale, * getClass().getClassLoader()</code>, except that any security check of * getClassLoader won't fail. * * @param baseName the name of the ResourceBundle * @param locale A locale * @return the desired resource bundle * @throws MissingResourceException if the resource bundle can't be found * @throws NullPointerException if baseName or locale is null */ public static ResourceBundle getBundle(String baseName, Locale locale) { ClassLoader cl = VMStackWalker.getCallingClassLoader(); if (cl == null) cl = ClassLoader.getSystemClassLoader(); return getBundle(baseName, locale, cl); } /** Cache key for the ResourceBundle cache. Resource bundles are keyed by the combination of bundle name, locale, and class loader. */ private static class BundleKey { String baseName; Locale locale; ClassLoader classLoader; int hashcode; BundleKey() {} BundleKey(String s, Locale l, ClassLoader cl) { set(s, l, cl); } void set(String s, Locale l, ClassLoader cl) { baseName = s; locale = l; classLoader = cl; hashcode = baseName.hashCode() ^ locale.hashCode() ^ classLoader.hashCode(); } public int hashCode() { return hashcode; } public boolean equals(Object o) { if (! (o instanceof BundleKey)) return false; BundleKey key = (BundleKey) o; return hashcode == key.hashcode && baseName.equals(key.baseName) && locale.equals(key.locale) && classLoader.equals(key.classLoader); } } /** A cache lookup key. This avoids having to a new one for every * getBundle() call. */ private static BundleKey lookupKey = new BundleKey(); /** Singleton cache entry to represent previous failed lookups. */ private static Object nullEntry = new Object();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -