resourcebundle.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,150 行 · 第 1/4 页
JAVA
1,150 行
*/ 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 */ public final String getString(String key) { return (String) getObject(key); } /** * Gets a string array 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 array * @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 array * @return the string array for the given key */ public final String[] getStringArray(String key) { return (String[]) getObject(key); } /** * Gets an object for the given key from this resource bundle or one of its parents. * This method first tries to obtain the object from this resource bundle using * {@link #handleGetObject(java.lang.String) handleGetObject}. * If not successful, and the parent resource bundle is not null, * it calls the parent's <code>getObject</code> method. * If still not successful, it throws a MissingResourceException. * * @param key the key for the desired object * @exception NullPointerException if <code>key</code> is <code>null</code> * @exception MissingResourceException if no object for the given key can be found * @return the object for the given key */ public final Object getObject(String key) { Object obj = handleGetObject(key); if (obj == null) { if (parent != null) { obj = parent.getObject(key); } if (obj == null) throw new MissingResourceException("Can't find resource for bundle " +this.getClass().getName() +", key "+key, this.getClass().getName(), key); } return obj; } /** * Returns the locale of this resource bundle. This method can be used after a * call to getBundle() to determine whether the resource bundle returned really * corresponds to the requested locale or is a fallback. * * @return the locale of this resource bundle */ public Locale getLocale() { return locale; } /** * Sets the locale for this bundle. This is the locale that this * bundle actually represents and does not depend on how the * bundle was found by getBundle. Ex. if the user was looking * for fr_FR and getBundle found en_US, the bundle's locale would * be en_US, NOT fr_FR * @param baseName the bundle's base name * @param bundleName the complete bundle name including locale * extension. */ private void setLocale(String baseName, String bundleName) { if (baseName.length() == bundleName.length()) { locale = new Locale("", ""); } else if (baseName.length() < bundleName.length()) { int pos = baseName.length(); String temp = bundleName.substring(pos + 1); pos = temp.indexOf('_'); if (pos == -1) { locale = new Locale(temp, "", ""); return; } String language = temp.substring(0, pos); temp = temp.substring(pos + 1); pos = temp.indexOf('_'); if (pos == -1) { locale = new Locale(language, temp, ""); return; } String country = temp.substring(0, pos); temp = temp.substring(pos + 1); locale = new Locale(language, country, temp); } else { //The base name is longer than the bundle name. Something is very wrong //with the calling code. throw new IllegalArgumentException(); } } /* * Automatic determination of the ClassLoader to be used to load * resources on behalf of the client. N.B. The client is getLoader's * caller's caller. */ private static ClassLoader getLoader() { Class[] stack = getClassContext(); /* Magic number 2 identifies our caller's caller */ Class c = stack[2]; ClassLoader cl = (c == null) ? null : c.getClassLoader(); if (cl == null) { cl = ClassLoader.getSystemClassLoader(); } return cl; } private static native Class[] getClassContext(); /** * Sets the parent bundle of this bundle. * The parent bundle is searched by {@link #getObject getObject} * when this bundle does not contain a particular resource. * * @param parent this bundle's parent bundle. */ protected void setParent( ResourceBundle parent ) { this.parent = parent; } /** * Key used for cached resource bundles. The key checks * the resource name, the class loader, and the default * locale to determine if the resource is a match to the * requested one. The loader may be null, but the * searchName and the default locale must have a non-null value. * Note that the default locale may change over time, and * lookup should always be based on the current default * locale (if at all). */ private static final class ResourceCacheKey implements Cloneable { private LoaderReference loaderRef; private String searchName; private Locale defaultLocale; private int hashCodeCache; public boolean equals(Object other) { if (this == other) { return true; } try { final ResourceCacheKey otherEntry = (ResourceCacheKey)other; //quick check to see if they are not equal if (hashCodeCache != otherEntry.hashCodeCache) { return false; } //are the names the same? if (!searchName.equals(otherEntry.searchName)) { return false; } // are the default locales the same? if (defaultLocale == null) { if (otherEntry.defaultLocale != null) { return false; } } else { if (!defaultLocale.equals(otherEntry.defaultLocale)) { return false; } } //are refs (both non-null) or (both null)? if (loaderRef == null) { return otherEntry.loaderRef == null; } else { Object loaderRefValue = loaderRef.get(); return (otherEntry.loaderRef != null) // with a null reference we can no longer find // out which class loader was referenced; so // treat it as unequal && (loaderRefValue != null) && (loaderRefValue == otherEntry.loaderRef.get()); } } catch (NullPointerException e) { return false; } catch (ClassCastException e) { return false; } } public int hashCode() { return hashCodeCache; } public Object clone() { try { ResourceCacheKey clone = (ResourceCacheKey) super.clone(); if (loaderRef != null) { clone.loaderRef = new LoaderReference(loaderRef.get(), referenceQueue, clone); } return clone; } catch (CloneNotSupportedException e) { //this should never happen throw new InternalError(); } } public void setKeyValues(ClassLoader loader, String searchName, Locale defaultLocale) { this.searchName = searchName; hashCodeCache = searchName.hashCode(); this.defaultLocale = defaultLocale; if (defaultLocale != null) { hashCodeCache ^= defaultLocale.hashCode(); } if (loader == null) { this.loaderRef = null; } else { loaderRef = new LoaderReference(loader, referenceQueue, this); hashCodeCache ^= loader.hashCode(); } } public void clear() { setKeyValues(null, "", null); } } /** * References to class loaders are weak references, so that they can be * garbage collected when nobody else is using them. The ResourceBundle * class has no reason to keep class loaders alive. */ private static final class LoaderReference extends WeakReference { private ResourceCacheKey cacheKey; LoaderReference(Object referent, ReferenceQueue q, ResourceCacheKey key) { super(referent, q); cacheKey = key; } ResourceCacheKey getCacheKey() { return cacheKey; } } /** * Gets a resource bundle using the specified base name, the default locale, * and the caller's class loader. Calling this method is equivalent to calling * <blockquote> * <code>getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader())</code>, * </blockquote> * except that <code>getClassLoader()</code> is run with the security * privileges of <code>ResourceBundle</code>. * See {@link #getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) getBundle} * for a complete description of the search and instantiation strategy. * * @param baseName the base name of the resource bundle, a fully qualified class name * @exception java.lang.NullPointerException * if <code>baseName</code> is <code>null</code> * @exception MissingResourceException * if no resource bundle for the specified base name can be found * @return a resource bundle for the given base name and the default locale */ public static final ResourceBundle getBundle(String baseName) { return getBundleImpl(baseName, Locale.getDefault(), /* must determine loader here, else we break stack invariant */ getLoader()); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?