Most visited

Recently visited

Added in API level 9

ResourceBundle.Control

public static class ResourceBundle.Control
extends Object

java.lang.Object
   ↳ java.util.ResourceBundle.Control


ResourceBundle.Control定义了一组回调方法,这些回调方法在捆绑加载过程中由ResourceBundle.getBundle工厂方法调用。 换句话说, ResourceBundle.Control与加载资源包的工厂方法协作。 回调方法的默认实现提供了工厂方法执行default behavior所需的信息。

除了回调方法之外, toBundleNametoResourceName方法的定义主要是为了方便实现回调方法。 但是,可以重写toBundleName方法,以在组织和打包本地化资源时提供不同的约定。 toResourceName方法是final以避免使用错误的资源和类名称分隔符。

两种工厂方法( getControl(List)getNoFallbackControl(List) )提供了 ResourceBundle.Control实例,用于实现默认捆绑加载过程的常见变体。

在返回的格式getFormats由返回的方法和候选语言环境getCandidateLocales方法必须全部一致ResourceBundle.getBundle调用了同样的基本包。 否则, ResourceBundle.getBundle方法可能会返回意外的捆绑包。 例如,如果只"java.class"由返回getFormats方法第一次调用ResourceBundle.getBundle ,只"java.properties"第二个呼叫,则第二次调用将返回已在第一呼叫期间被缓存基于类的一个。

如果ResourceBundle.Control实例同时被多个线程使用,则该实例必须是线程安全的。 ResourceBundle.getBundle不同步调用ResourceBundle.Control方法。 这些方法的默认实现是线程安全的。

应用程序可以指定由getControl工厂方法返回的ResourceBundle.Control实例或从ResourceBundle.Control的子类创建的实例来自定义包加载过程。 以下是更改默认捆绑包加载过程的示例。

例1

以下代码让 ResourceBundle.getBundle只查找基于属性的资源。

 import java.util.*;
 import static java.util.ResourceBundle.Control.*;
 ...
 ResourceBundle bundle =
   ResourceBundle.getBundle("MyResources", new Locale("fr", "CH"),
                            ResourceBundle.Control.getControl(FORMAT_PROPERTIES));
 
Given the resource bundles in the example in the ResourceBundle.getBundle description, this ResourceBundle.getBundle call loads MyResources_fr_CH.properties whose parent is MyResources_fr.properties whose parent is MyResources.properties. ( MyResources_fr_CH.properties is not hidden, but MyResources_fr_CH.class is.)

例2

以下是使用 Properties.loadFromXML加载基于XML的软件包的 Properties.loadFromXML

 ResourceBundle rb = ResourceBundle.getBundle("Messages",
     new ResourceBundle.Control() {
         public List<String> getFormats(String baseName) {
             if (baseName == null)
                 throw new NullPointerException();
             return Arrays.asList("xml");
         }
         public ResourceBundle newBundle(String baseName,
                                         Locale locale,
                                         String format,
                                         ClassLoader loader,
                                         boolean reload)
                          throws IllegalAccessException,
                                 InstantiationException,
                                 IOException {
             if (baseName == null || locale == null
                   || format == null || loader == null)
                 throw new NullPointerException();
             ResourceBundle bundle = null;
             if (format.equals("xml")) {
                 String bundleName = toBundleName(baseName, locale);
                 String resourceName = toResourceName(bundleName, format);
                 InputStream stream = null;
                 if (reload) {
                     URL url = loader.getResource(resourceName);
                     if (url != null) {
                         URLConnection connection = url.openConnection();
                         if (connection != null) {
                             // Disable caches to get fresh data for
                             // reloading.
                             connection.setUseCaches(false);
                             stream = connection.getInputStream();
                         }
                     }
                 } else {
                     stream = loader.getResourceAsStream(resourceName);
                 }
                 if (stream != null) {
                     BufferedInputStream bis = new BufferedInputStream(stream);
                     bundle = new XMLResourceBundle(bis);
                     bis.close();
                 }
             }
             return bundle;
         }
     });

 ...

 private static class XMLResourceBundle extends ResourceBundle {
     private Properties props;
     XMLResourceBundle(InputStream stream) throws IOException {
         props = new Properties();
         props.loadFromXML(stream);
     }
     protected Object handleGetObject(String key) {
         return props.getProperty(key);
     }
     public Enumeration<String> getKeys() {
         ...
     }
 }
 

Summary

Constants

long TTL_DONT_CACHE

不缓存已加载资源包实例的生存时间常量。

long TTL_NO_EXPIRATION_CONTROL

用于禁用高速缓存中已加载资源包实例的到期控制的生存时间常量。

Fields

public static final List<String> FORMAT_CLASS

类专用格式 List含有 "java.class"

public static final List<String> FORMAT_DEFAULT

默认格式为 List ,其中包含字符串 "java.class""java.properties" ,按此顺序。

public static final List<String> FORMAT_PROPERTIES

属性专用格式 List含有 "java.properties"

Protected constructors

ResourceBundle.Control()

唯一的构造函数。

Public methods

List<Locale> getCandidateLocales(String baseName, Locale locale)

返回 ListLocale S作为候选语言环境为 baseNamelocale

static final ResourceBundle.Control getControl(List<String> formats)

返回 ResourceBundle.Control ,其中 getFormats方法返回指定的 formats

Locale getFallbackLocale(String baseName, Locale locale)

返回一个 Locale作为备用区域设置,用于 ResourceBundle.getBundle工厂方法的更多资源包搜索。

List<String> getFormats(String baseName)

返回 ListString s,其中包含用于加载给定 baseName资源包的 baseName

static final ResourceBundle.Control getNoFallbackControl(List<String> formats)

返回 ResourceBundle.Control ,其中 getFormats方法返回指定的 formats ,并且 getFallbackLocale方法返回 null

long getTimeToLive(String baseName, Locale locale)

返回在此 ResourceBundle.Control下加载的资源包的生存时间(TTL)值。

boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime)

确定缓存中的过期 bundle是否需要根据 loadTime或其他条件给出的加载时间重新加载。

ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)

为给定格式和语言环境的给定包名实例化资源包,如有必要,使用给定的类加载器。

String toBundleName(String baseName, Locale locale)

将给定的 baseNamelocale转换为软件包名称。

final String toResourceName(String bundleName, String suffix)

所述给定转换 bundleName由所要求的形式 ClassLoader.getResource通过更换所有出现的方法 '.'bundleName'/'和追加 '.'和给定的文件 suffix

Inherited methods

From class java.lang.Object

Constants

TTL_DONT_CACHE

Added in API level 9
long TTL_DONT_CACHE

不缓存已加载资源包实例的生存时间常量。

也可以看看:

常量值:-1(0xffffffffffffffff)

TTL_NO_EXPIRATION_CONTROL

Added in API level 9
long TTL_NO_EXPIRATION_CONTROL

用于禁用高速缓存中已加载资源包实例的到期控制的生存时间常量。

也可以看看:

常量值:-2(0xfffffffffffffffe)

Fields

FORMAT_CLASS

Added in API level 9
List<String> FORMAT_CLASS

类专用格式List含有"java.class" Listunmodifiable

也可以看看:

FORMAT_DEFAULT

Added in API level 9
List<String> FORMAT_DEFAULT

默认格式List ,其中包含字符串"java.class""java.properties" ,按此顺序。 Listunmodifiable

也可以看看:

FORMAT_PROPERTIES

Added in API level 9
List<String> FORMAT_PROPERTIES

属性专用格式List含有"java.properties" Listunmodifiable

也可以看看:

Protected constructors

ResourceBundle.Control

Added in API level 9
ResourceBundle.Control ()

唯一的构造函数。 (对于子类构造函数的调用,通常是隐式的。)

Public methods

getCandidateLocales

Added in API level 9
List<Locale> getCandidateLocales (String baseName, 
                Locale locale)

返回List ,其中LocalebaseNamelocale候选语言环境。 每当工厂方法尝试为目标Locale查找资源包时,该方法将由ResourceBundle.getBundle工厂方法Locale

如果候选语言环境的相应资源包存在并且它们的父代不是由加载的资源包本身定义的,则候选语言环境的序列也对应于运行时资源查找路径(也称为父链 )。 如果希望将基础包作为父链的终端,则列表的最后一个元素必须是root locale

如果给定的区域设置等于Locale.ROOT (根区域设置), Locale必须返回仅包含根区域ListLocale 在这种情况下, ResourceBundle.getBundle工厂方法仅加载基础包作为结果资源包。

这不是要求返回一个不可变(不可修改)的List 但是,返回的List在由getCandidateLocales返回后不能发生变异。

缺省的实现返回一个List含有Locale使用下述的规则秒。 在下面的描述中, LSCV分别表示非空的语言,脚本,国家和变体。 例如,[ LC ]表示仅对语言和国家/地区具有非空值的Locale 形式L (“xx”)表示(非空)语言值是“xx”。 对于所有情况,其最终组件值为空字符串的Locale都被忽略。

  1. For an input Locale with an empty script value, append candidate Locales by omitting the final component one by one as below:
    • [L, C, V]
    • [L, C]
    • [L]
    • Locale.ROOT
  2. For an input Locale with a non-empty script value, append candidate Locales by omitting the final component up to language, then append candidates generated from the Locale with country and variant restored:
    • [L, S, C, V]
    • [L, S, C]
    • [L, S]
    • [L, C, V]
    • [L, C]
    • [L]
    • Locale.ROOT
  3. For an input Locale with a variant value consisting of multiple subtags separated by underscore, generate candidate Locales by omitting the variant subtags one by one, then insert them after every occurence of Locales with the full variant value in the original list. For example, if the the variant consists of two subtags V1 and V2:
    • [L, S, C, V1, V2]
    • [L, S, C, V1]
    • [L, S, C]
    • [L, S]
    • [L, C, V1, V2]
    • [L, C, V1]
    • [L, C]
    • [L]
    • Locale.ROOT
  4. Special cases for Chinese. When an input Locale has the language "zh" (Chinese) and an empty script value, either "Hans" (Simplified) or "Hant" (Traditional) might be supplied, depending on the country. When the country is "CN" (China) or "SG" (Singapore), "Hans" is supplied. When the country is "HK" (Hong Kong SAR China), "MO" (Macau SAR China), or "TW" (Taiwan), "Hant" is supplied. For all other countries or when the country is empty, no script is supplied. For example, for Locale("zh", "CN") , the candidate list will be:
    • [L("zh"), S("Hans"), C("CN")]
    • [L("zh"), S("Hans")]
    • [L("zh"), C("CN")]
    • [L("zh")]
    • Locale.ROOT
    For Locale("zh", "TW"), the candidate list will be:
    • [L("zh"), S("Hant"), C("TW")]
    • [L("zh"), S("Hant")]
    • [L("zh"), C("TW")]
    • [L("zh")]
    • Locale.ROOT
  5. Special cases for Norwegian. Both Locale("no", "NO", "NY") and Locale("nn", "NO") represent Norwegian Nynorsk. When a locale's language is "nn", the standard candidate list is generated up to [L("nn")], and then the following candidates are added:
    • [L("no"), C("NO"), V("NY")]
    • [L("no"), C("NO")]
    • [L("no")]
    • Locale.ROOT
    If the locale is exactly Locale("no", "NO", "NY"), it is first converted to Locale("nn", "NO") and then the above procedure is followed.

    此外,Java将语言“no”视为挪威语BokmÓl”nb的同义词。 除了单个案例Locale("no", "NO", "NY") (以上处理)之外,当输入Locale具有语言“否”或“nb”时,具有语言代码“no”和“nb”的候选Locale被交错,首先使用所请求的语言,代名词。 例如, Locale("nb", "NO", "POSIX")生成以下候选列表:

    • [L("nb"), C("NO"), V("POSIX")]
    • [L("no"), C("NO"), V("POSIX")]
    • [L("nb"), C("NO")]
    • [L("no"), C("NO")]
    • [L("nb")]
    • [L("no")]
    • Locale.ROOT
    Locale("no", "NO", "POSIX") would generate the same list except that locales with "no" would appear before the corresponding locales with "nb".

默认实现使用ArrayList ,覆盖实现可能会在将其返回给调用者之前进行修改。 但是,子类在getCandidateLocales返回后不能修改它。

例如,如果给定的 baseName是“消息”并且给定的 localeLocale("ja", "", "XX") ,则 ListLocale s:

     Locale("ja", "", "XX")
     Locale("ja")
     Locale.ROOT
 
is returned. And if the resource bundles for the "ja" and "" Locales are found, then the runtime resource lookup path (parent chain) is:
     Messages_ja -> Messages
 

Parameters
baseName String: the base name of the resource bundle, a fully qualified class name
locale Locale: the locale for which a resource bundle is desired
Returns
List<Locale> a List of candidate Locales for the given locale
Throws
NullPointerException if baseName or locale is null

getControl

Added in API level 9
ResourceBundle.Control getControl (List<String> formats)

返回ResourceBundle.Control ,其中getFormats方法返回指定的formats 所述formats必须等于之一FORMAT_PROPERTIESFORMAT_CLASSFORMAT_DEFAULT 此方法返回的ResourceBundle.Control实例是单例并且是线程安全的。

指定 FORMAT_DEFAULT等同于实例化 ResourceBundle.Control类,不同之 ResourceBundle.Control于此方法返回一个单例。

Parameters
formats List: the formats to be returned by the ResourceBundle.Control.getFormats method
Returns
ResourceBundle.Control a ResourceBundle.Control supporting the specified formats
Throws
NullPointerException if formats is null
IllegalArgumentException if formats is unknown

getFallbackLocale

Added in API level 9
Locale getFallbackLocale (String baseName, 
                Locale locale)

返回Locale作为备用区域设置,供ResourceBundle.getBundle工厂方法进一步资源包搜索使用。 每当未找到baseNamelocale结果资源束时,都会从工厂方法调用此方法,其中locale是ResourceBundle.getBundle的参数或此方法返回的上一个回退语言环境。

如果不需要进一步的回退搜索,该方法返回 null

如果给定的locale不是默认实现,则默认实现将返回default Locale 否则,返回null

Parameters
baseName String: the base name of the resource bundle, a fully qualified class name for which ResourceBundle.getBundle has been unable to find any resource bundles (except for the base bundle)
locale Locale: the Locale for which ResourceBundle.getBundle has been unable to find any resource bundles (except for the base bundle)
Returns
Locale a Locale for the fallback search, or null if no further fallback search is desired.
Throws
NullPointerException if baseName or locale is null

getFormats

Added in API level 9
List<String> getFormats (String baseName)

返回包含ListString包含格式,用于为给定的baseName加载资源包。 ResourceBundle.getBundle工厂方法尝试按列表中指定的顺序加载格式的资源包。 该方法返回的列表必须至少有一个String 预定义格式为"java.class"用于基于类的资源包, "java.properties"用于properties-based "java."开始的字符串被保留用于将来的扩展,并且不能被应用程序定义的格式使用。

这不是要求返回一个不可修改的(不可修改的) List 但是,返回的List在由getFormats返回后不能发生变异。

默认实现返回 FORMAT_DEFAULT以便 ResourceBundle.getBundle工厂方法查找第一个基于类的资源包,然后查找基于属性的资源包。

Parameters
baseName String: the base name of the resource bundle, a fully qualified class name
Returns
List<String> a List of Strings containing formats for loading resource bundles.
Throws
NullPointerException if baseName is null

也可以看看:

getNoFallbackControl

Added in API level 9
ResourceBundle.Control getNoFallbackControl (List<String> formats)

返回ResourceBundle.Control ,其中getFormats方法返回指定的formats ,并且getFallbackLocale方法返回null 所述formats必须等于之一FORMAT_PROPERTIESFORMAT_CLASSFORMAT_DEFAULT 此方法返回的ResourceBundle.Control实例是单例并且是线程安全的。

Parameters
formats List: the formats to be returned by the ResourceBundle.Control.getFormats method
Returns
ResourceBundle.Control a ResourceBundle.Control supporting the specified formats with no fallback Locale support
Throws
NullPointerException if formats is null
IllegalArgumentException if formats is unknown

getTimeToLive

Added in API level 9
long getTimeToLive (String baseName, 
                Locale locale)

返回在此ResourceBundle.Control下加载的资源包的生存时间(TTL)值。 正值生存时间值指定了数据包可以保留在高速缓存中的毫秒数,而无需对其构建的源数据进行验证。 值0表示每次从高速缓存中检索数据包时都必须验证数据包。 TTL_DONT_CACHE指定加载的资源包未放入缓存中。 TTL_NO_EXPIRATION_CONTROL指定将加载的资源包放入高速缓存中,且没有到期控制。

到期仅影响ResourceBundle.getBundle工厂方法的捆绑装载过程。 也就是说,如果工厂方法在高速缓存中发现资源包已过期,则工厂方法将调用needsReload方法来确定是否需要重新加载资源包。 如果needsReload返回true ,则缓存资源包实例将从缓存中删除。 否则,实例将保留在缓存中,并使用此方法返回的新TTL值进行更新。

由于运行时环境的内存限制,所有缓存的资源包都可能会从缓存中删除。 返回大的正值并不意味着将已加载的资源包锁定在缓存中。

默认实现返回 TTL_NO_EXPIRATION_CONTROL

Parameters
baseName String: the base name of the resource bundle for which the expiration value is specified.
locale Locale: the locale of the resource bundle for which the expiration value is specified.
Returns
long the time (0 or a positive millisecond offset from the cached time) to get loaded bundles expired in the cache, TTL_NO_EXPIRATION_CONTROL to disable the expiration control, or TTL_DONT_CACHE to disable caching.
Throws
NullPointerException if baseName or locale is null

needsReload

Added in API level 9
boolean needsReload (String baseName, 
                Locale locale, 
                String format, 
                ClassLoader loader, 
                ResourceBundle bundle, 
                long loadTime)

根据loadTime或其他条件给出的加载时间确定缓存中的过期bundle是否需要重新加载。 如果需要重新加载,该方法返回true ; 否则为false loadTime是自Calendar Epoch以来的毫秒偏移量。 调用ResourceBundle.getBundle工厂方法在用于当前调用的ResourceBundle.Control实例上调用此方法,而不是调用最初加载资源包时使用的实例。

默认实现比较loadTime和资源束的源数据的上次修改时间。 如果确定源数据自loadTime以来已被修改,则返回true 否则,返回false 此实现假定给定的format与其文件后缀相同的字符串,如果它不是默认格式"java.class""java.properties"

Parameters
baseName String: the base bundle name of the resource bundle, a fully qualified class name
locale Locale: the locale for which the resource bundle should be instantiated
format String: the resource bundle format to be loaded
loader ClassLoader: the ClassLoader to use to load the bundle
bundle ResourceBundle: the resource bundle instance that has been expired in the cache
loadTime long: the time when bundle was loaded and put in the cache
Returns
boolean true if the expired bundle needs to be reloaded; false otherwise.
Throws
NullPointerException if baseName, locale, format, loader, or bundle is null

newBundle

Added in API level 9
ResourceBundle newBundle (String baseName, 
                Locale locale, 
                String format, 
                ClassLoader loader, 
                boolean reload)

为给定格式和语言环境的给定包名实例化资源包,如有必要,使用给定的类加载器。 如果没有可用于给定参数的资源束,则此方法返回null 如果资源包由于意外错误而无法实例化,则必须通过抛出Error异常来报告错误,而不是简单地返回null

如果 reload标志为 true ,则表示此方法正在被调用,因为之前加载的资源包已过期。

默认实现如下实例化 ResourceBundle

Parameters
baseName String: the base bundle name of the resource bundle, a fully qualified class name
locale Locale: the locale for which the resource bundle should be instantiated
format String: the resource bundle format to be loaded
loader ClassLoader: the ClassLoader to use to load the bundle
reload boolean: the flag to indicate bundle reloading; true if reloading an expired resource bundle, false otherwise
Returns
ResourceBundle the resource bundle instance, or null if none could be found.
Throws
NullPointerException if bundleName, locale, format, or loader is null, or if null is returned by toBundleName
IllegalArgumentException if format is unknown, or if the resource found for the given parameters contains malformed data.
ClassCastException if the loaded class cannot be cast to ResourceBundle
IllegalAccessException if the class or its nullary constructor is not accessible.
InstantiationException if the instantiation of a class fails for some other reason.
ExceptionInInitializerError if the initialization provoked by this method fails.
SecurityException If a security manager is present and creation of new instances is denied. See newInstance() for details.
IOException if an error occurred when reading resources using any I/O operations

toBundleName

Added in API level 9
String toBundleName (String baseName, 
                Locale locale)

将给定的baseNamelocale转换为软件包名称。 该方法从newBundleneedsReload方法的默认实现中调用。

该实现返回以下值:

     baseName + "_" + language + "_" + script + "_" + country + "_" + variant
 
where language, script, country, and variant are the language, script, country, and variant values of locale, respectively. Final component values that are empty Strings are omitted along with the preceding '_'. When the script is empty, the script value is ommitted along with the preceding '_'. If all of the values are empty strings, then baseName is returned.

例如,如果baseName"baseName"localeLocale("ja", "", "XX") ,则返回"baseName_ja_ _XX" 如果给定的语言环境是Locale("en") ,则返回"baseName_en"

重写此方法允许应用程序在本地化资源的组织和打包中使用不同的约定。

Parameters
baseName String: the base name of the resource bundle, a fully qualified class name
locale Locale: the locale for which a resource bundle should be loaded
Returns
String the bundle name for the resource bundle
Throws
NullPointerException if baseName or locale is null

toResourceName

Added in API level 9
String toResourceName (String bundleName, 
                String suffix)

所述给定转换bundleName由所要求的形式ClassLoader.getResource通过更换所有出现的方法'.'bundleName'/'和追加'.'和给定的文件suffix 例如,如果bundleName"foo.bar.MyResources_ja_JP"suffix"properties" ,则返回"foo/bar/MyResources_ja_JP.properties"

Parameters
bundleName String: the bundle name
suffix String: the file type suffix
Returns
String the converted resource name
Throws
NullPointerException if bundleName or suffix is null

Hooray!