Udostępnij za pośrednictwem


ResourceBundle.Control Class

Definition

ResourceBundle.Control defines a set of callback methods that are invoked by the ResourceBundle#getBundle(String, Locale, ClassLoader, Control) ResourceBundle.getBundle factory methods during the bundle loading process.

[Android.Runtime.Register("java/util/ResourceBundle$Control", DoNotGenerateAcw=true)]
public class ResourceBundle.Control : Java.Lang.Object
[<Android.Runtime.Register("java/util/ResourceBundle$Control", DoNotGenerateAcw=true)>]
type ResourceBundle.Control = class
    inherit Object
Inheritance
ResourceBundle.Control
Attributes

Remarks

ResourceBundle.Control defines a set of callback methods that are invoked by the ResourceBundle#getBundle(String, Locale, ClassLoader, Control) ResourceBundle.getBundle factory methods during the bundle loading process. In other words, a ResourceBundle.Control collaborates with the factory methods for loading resource bundles. The default implementation of the callback methods provides the information necessary for the factory methods to perform the default behavior.

In addition to the callback methods, the #toBundleName(String, Locale) toBundleName and #toResourceName(String, String) toResourceName methods are defined primarily for convenience in implementing the callback methods. However, the toBundleName method could be overridden to provide different conventions in the organization and packaging of localized resources. The toResourceName method is final to avoid use of wrong resource and class name separators.

Two factory methods, #getControl(List) and #getNoFallbackControl(List), provide ResourceBundle.Control instances that implement common variations of the default bundle loading process.

The formats returned by the Control#getFormats(String) getFormats method and candidate locales returned by the ResourceBundle.Control#getCandidateLocales(String, Locale) getCandidateLocales method must be consistent in all ResourceBundle.getBundle invocations for the same base bundle. Otherwise, the ResourceBundle.getBundle methods may return unintended bundles. For example, if only "java.class" is returned by the getFormats method for the first call to ResourceBundle.getBundle and only "java.properties" for the second call, then the second call will return the class-based one that has been cached during the first call.

A ResourceBundle.Control instance must be thread-safe if it's simultaneously used by multiple threads. ResourceBundle.getBundle does not synchronize to call the ResourceBundle.Control methods. The default implementations of the methods are thread-safe.

Applications can specify ResourceBundle.Control instances returned by the getControl factory methods or created from a subclass of ResourceBundle.Control to customize the bundle loading process. The following are examples of changing the default bundle loading process.

<b>Example 1</b>

The following code lets ResourceBundle.getBundle look up only properties-based resources.

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.)

<b>Example 2</b>

The following is an example of loading XML-based bundles using Properties#loadFromXML(java.io.InputStream) Properties.loadFromXML.

ResourceBundle rb = ResourceBundle.getBundle("Messages",
                new ResourceBundle.Control() {
                    public List&lt;String&gt; 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&lt;String&gt; getKeys() {
                    ...
                }
            }

Added in 1.6.

Java documentation for java.util.ResourceBundle.Control.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

ResourceBundle.Control()

Sole constructor.

ResourceBundle.Control(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Fields

TtlDontCache

The time-to-live constant for not caching loaded resource bundle instances.

TtlNoExpirationControl

The time-to-live constant for disabling the expiration control for loaded resource bundle instances in the cache.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
FormatClass

The class-only format List containing "java.class".

FormatDefault

The default format List, which contains the strings "java.class" and "java.properties", in this order.

FormatProperties

The properties-only format List containing "java.properties".

Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetCandidateLocales(String, Locale)

Returns a List of Locales as candidate locales for baseName and locale.

GetControl(IList<String>)
GetFallbackLocale(String, Locale)

Returns a Locale to be used as a fallback locale for further resource bundle searches by the ResourceBundle.getBundle factory method.

GetFormats(String)

Returns a List of Strings containing formats to be used to load resource bundles for the given baseName.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetNoFallbackControl(IList<String>)
GetTimeToLive(String, Locale)

Returns the time-to-live (TTL) value for resource bundles that are loaded under this ResourceBundle.Control.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
NeedsReload(String, Locale, String, ClassLoader, ResourceBundle, Int64)

Determines if the expired bundle in the cache needs to be reloaded based on the loading time given by loadTime or some other criteria.

NewBundle(String, Locale, String, ClassLoader, Boolean)

Instantiates a resource bundle for the given bundle name of the given format and locale, using the given class loader if necessary.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ToArray<T>() (Inherited from Object)
ToBundleName(String, Locale)

Converts the given baseName and locale to the bundle name.

ToResourceName(String, String)

Converts the given bundleName to the form required by the ClassLoader#getResource ClassLoader.getResource method by replacing all occurrences of '.' in bundleName with '/' and appending a '.' and the given file suffix.

ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to