|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.microedition.global.ResourceManager
Resource manager for applications. Provides an interface to the resources for a specified base name and the specified locale. The locale is typically the system default locale, but another locale can be specified. This specification does not mandate any partitioning of the base names and applications, so each application can use as many base names as necessary.
Resource managers are constructed using some variant of the static
factory method getManager
. One resource manager MUST dispense
either application resources or device resources, but not both.
If the base name is "
" (defined as the empty string),
the manager MUST only retrieve device resources. Non-empty base names
MUST be used for application resources.DEVICE
Application resources and device
resources are retrieved using the same method, getResource
.
Resources retrieved by the getResource(int id)
method
MUST be returned as Java objects.
The resource IDs used by the manager MUST be integers in the range 0...0x7FFFFFFF
.
The application resource IDs MUST be unique to each base name, but there
can be several resources with the same ID in the system, as long as they are associated with
different base names. Device resource IDs MUST be unique across resource managers, i.e. each
manager MUST return the same device resource for the same ID.
Application resources are specific to a base name, whereas each manager returns
device resources from a common pool. For application resources, the application programmer keeps track
of the actual type of each resource. For device resources, the device
manufacturer MUST publish this information along with the valid resource IDs.
Since most of the retrieved resources are strings, the getString(int id)
method
MUST return the string resource associated with the ID, to avoid typecasting in the application.
For a discussion of the resource file format refer to the "Resource files" section in the specification overview.
To save storage, resources that are exactly the same for two or more locales can be stored
in a common locale. For example, the strings "OK" and "Cancel" would typically be used
in both U.S. English and British English (locale identifiers "en-US"
and "en-GB"
, respectively). Instead of duplicating these strings
in both locales, they can be stored in the "en"
locale. It can be said
that the "en-US"
and "en-GB"
locales "inherit" the strings
from the "en"
locale.
The resource retrieval mechanism MUST support resource inheritance by implementing hierarchical matching as described in the Resource matching section.
A resource manager is always constructed using a base name. The
supported locales of a resource manager are
those for which resources exist for the manager's base name. This is reflected by
the static getSupportedLocales
method.
The type of construction of the resource manager determines the matching rules used for resource lookup. The simplest form specifies only the base name and uses the system's default locale. A resource manager can also be constructed for a specific locale by passing a locale identifier. If resources for the system default locale or the passed locale are not found, hierachical matching is performed.
Hierarchical matching successively tries more generic locales
derived from the specified one by removing first the variant, then the country, and
finally the language component from the locale. If no resources are found even in the
common locale, a ResourceException
MUST be thrown.
If an array of locale identifiers is passed, all the locales in the array MUST be tried successively until there is a match, or the array is exhausted.
Valid locale identifiers consist of all valid MIDP 2.0 locale identifiers and the empty string. The empty string MUST be used as a shortcut for resources with the same base name that are common to all locales.
The locale resolved by matching can end up different than the one specified.
The base name of the resource manager and the actual resolved locale can be
retrieved using the getBaseName
and getLocale
methods,
respectively.
To avoid repeated reading of resources from possibly slow media, a
resource manager SHOULD support the caching of resources in memory.
If caching is supported, then when a resource is read for the first time,
it SHOULD be stored in memory and retrieved
from there. The isCaching
method MUST return the caching status.
The actual caching algorithm is implementation specific.
The following conditions MUST cause a ResourceException
to be
thrown with the appropriate error code and detail message:
null
.getData
).
ResourceException
Field Summary | |
static java.lang.String |
DEVICE
Constant to indicate device resources. |
Method Summary | |
java.lang.String |
getBaseName()
Gets the base name of this resource manager. |
byte[] |
getData(int id)
Gets a resource with the specified resource ID as a byte array containing arbitrary binary data. |
java.lang.String |
getLocale()
Gets the locale of this resource manager. |
static ResourceManager |
getManager(java.lang.String baseName)
Gets a resource manager for the specified base name and the system's default locale. |
static ResourceManager |
getManager(java.lang.String baseName,
java.lang.String locale)
Gets a resource manager for the specified base name and locale. |
static ResourceManager |
getManager(java.lang.String baseName,
java.lang.String[] locales)
Gets a resource manager for the specified base name and the first matching locale in the supplied array. |
java.lang.Object |
getResource(int id)
Gets a resource with the specified ID. |
java.lang.String |
getString(int id)
Gets a string with the specified resource ID. |
static java.lang.String[] |
getSupportedLocales(java.lang.String baseName)
Gets the locales supported by this resource manager for the given base name. |
boolean |
isCaching()
Returns the caching status of this resource manager. |
boolean |
isValidResourceID(int id)
Determines if the given resource ID is valid in this implementation. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final java.lang.String DEVICE
Constant to indicate device resources.
Method Detail |
public java.lang.String getBaseName()
Gets the base name of this resource manager.
DEVICE
if this
resource manager is retrieving device-specific resources)public byte[] getData(int id) throws ResourceException
Gets a resource with the specified resource ID as a byte array containing
arbitrary binary data. If the resource with the specified ID is not
binary data, or the resource does not exist,
a ResourceException
is thrown with the appropriate error code.
This method performs a hierarchical lookup of the resource.
id
- the resource ID
ResourceException
- if the resource does not exist, or if the
resource is not the correct type
java.lang.IllegalArgumentException
- if the resource ID is invalidpublic java.lang.String getLocale()
Gets the locale of this resource manager.
public static final ResourceManager getManager(java.lang.String baseName) throws ResourceException, UnsupportedLocaleException
Gets a resource manager for the specified base name and the system's default locale.
If the base name is
, the resource manager can only retrieve
device-specific resources. Other base names retrieve application-specific resources. The base name must
not be DEVICE
null
.
If the system's default locale is null
, a ResourceException
is thrown. If it is non-null
but no resources are found,
this method performs the hierarchical matching of locales.
If no resources are found for the combination of base name and locale,
a ResourceException
is thrown with the appropriate error code
and detail message.
baseName
- the base name, DEVICE
to retrieve device-specific resources
java.lang.NullPointerException
- if the base name is null
ResourceException
- if the system default locale is null
,
or the resource file is invalid
UnsupportedLocaleException
- if the system's default locale (when non-null)
is not listed in the meta-information file of this base name (for application
resources only), or when device resources are not supported for the system's default localegetManager(String, String)
,
DEVICE
public static final ResourceManager getManager(java.lang.String baseName, java.lang.String locale) throws ResourceException, UnsupportedLocaleException
Gets a resource manager for the specified base name and locale.
If the base name is
(the empty string),
the resource manager can only retrieve device resources.
Other base names retrieve application resources. The base name must
not be DEVICE
null
.
If resources for the specified locale are not found, this method attempts to find resources by hierarchical matching. The matching proceeds from language-country-variant to language-country, then on to language, and finally to the empty string. An empty string can also be used as the locale. It is a shortcut to using common resources without going through the matching process.
If resources for the combination of the base name and the matching locale
are found, this method returns a manager for those resources. If no resources
are found for the combination, a ResourceException
is thrown.
baseName
- the base name, non-empty for application resources,
DEVICE
(the empty string) for device resourceslocale
- the locale to use either as is or as a starting point
for hierarchical matching
java.lang.NullPointerException
- if the base name is null
or if the locale is
null
java.lang.IllegalArgumentException
- if the locale identifier is not valid according to
the MIDP 2.0 specification
ResourceException
- if no resources for the combination of
base name and locale are found, or the resource file is invalid
UnsupportedLocaleException
- if locale
is not listed in the
meta-information file of this base name (application resources only)DEVICE
public static final ResourceManager getManager(java.lang.String baseName, java.lang.String[] locales) throws ResourceException, UnsupportedLocaleException
Gets a resource manager for the specified base name and the first matching locale in the supplied array.
If the base name is "
" (the empty string),
the resource manager can only retrieve
device resources. Other base names retrieve application resources.
The base name MUST NOT be DEVICE
null
.
This method attempts to get a resource manager for a combination of
the base name and one of the locales in the array. The locales are
tried successively, stopping at the first match.
This method MUST NOT perform hierarchical matching. If none of the locales in the array result in a match,
a ResourceException
MUST be thrown with the appropriate error code.
For example, if the array of locales contains { "de-DE", "fr", "en" }
,
and resources are not found for "de-DE"
or "fr"
,
but are found for "en"
,
then this method gets a manager for "en"
. However, if resources are not
found for "de-DE"
, but are found for "fr"
, then this method gets a manager
for "fr"
and disregards the "en"
locale. In this case
if resources are not found for "de-DE"
, the locale "de"
MUST NOT be tried; instead the matching proceeds to the next locale in the array.
The locales
array MUST NOT be null
or empty. Each of the locale
identifiers in the array MUST be a valid locale identifier, otherwise an IllegalArgumentException
is thrown. As an extension, the empty string can be used to use resources common to all locales.
baseName
- the base name, non-empty for application resources, or
DEVICE
(the empty string) for device resourceslocales
- the array of locale identifiers to try
java.lang.NullPointerException
- if the locales array is null
, or if
any of the locales in the array is null
, or the base name is null
java.lang.IllegalArgumentException
- if any of the locale identifiers in the array
is invalid according to the MIDP 2.0 specification, or if the array is empty
ResourceException
- if no resources for the base name and any of the
locales in the array are found
UnsupportedLocaleException
- if none of the locales in the array
are listed in the meta-information file of this base name (for application
resources), or when device resources are not supported for any of the localesDEVICE
public java.lang.Object getResource(int id) throws ResourceException
Gets a resource with the specified ID. The resource is either an application-specific resource or a device resource, depending on the base name of the resource manager.
This method performs a hierarchical lookup of the resource.
id
- the resource ID
ResourceException
- if the resource is not found, or if
an application resource has an unknown type
java.lang.IllegalArgumentException
- if the resource ID is invalidgetString(int)
,
getData(int)
public java.lang.String getString(int id) throws ResourceException
Gets a string with the specified resource ID. This is a convenience
method to avoid typecasting in applications. If the resource with
the specified ID is not a string, or the resource does not exist,
a ResourceException
is thrown with the appropriate error code.
This method performs a hierarchical lookup of the resource.
id
- the resource ID
ResourceException
- if the resource does not exist, or if the
resource is not the correct type
java.lang.IllegalArgumentException
- if the resource ID is invalidpublic static java.lang.String[] getSupportedLocales(java.lang.String baseName)
Gets the locales supported by this resource manager for the given
base name. Returns an
array of valid microedition.locale
values. If there
are no supported locales, returns an empty array (not null
).
The base name is used to distinguish between application
and device resources. If the base name is non-empty, this
method returns the list of locales for which application-specific
resources are defined. If the base name is "
"
(the empty string), the
set of supported device locales is returned instead.DEVICE
The returned array MAY be empty if no locales are supported for the
specified base name. It MUST NOT be null
.
Values in the array MUST be unique. The value
null
MUST NOT appear in the returned array. However,
if there are resources for this base name which are common to all locales,
the array MUST contain the empty string. It SHOULD be first element in the array.
The supported locales for an application resource manager require
a base name specific meta-information file described in the "Enumerating
supported locales" section of the
specification overview. If the meta-information file is not found, this
method MUST throw a ResourceException
with the error
code METAFILE_NOT_FOUND
.
If the content of the meta-information file is not syntactically
valid, this method MUST throw a ResourceException with the error code
DATA_ERROR
.
The list of locales supported by a resource manager for a base name MAY be different than in a formatter, i.e. the implementation MAY support formatting rules for a certain set of locales, but an application can supply resources for an entirely different (or overlapping) set of locales.
baseName
- the base name, non-empty for application resources,
DEVICE
(the empty string) for device resources
java.lang.NullPointerException
- if the base name is null
ResourceException
- if the meta-information file is not found or it is
not of the specified formatFormatter.getSupportedLocales()
,
StringComparator.getSupportedLocales()
,
ResourceException
public boolean isCaching()
Returns the caching status of this resource manager.
true
if caching, false
if notpublic boolean isValidResourceID(int id)
Determines if the given resource ID is valid in this implementation.
In this context "valid" means that a resource exists with the given
resource ID, and calling getResource
with that ID
returns a non-null
object instance.
The method does not discriminate between application and device resources, since one resource manager can only handle one type of resource.
id
- the resource ID
true
if the resource ID is valid,
false
if not
java.lang.IllegalArgumentException
- if the resource ID is not in the defined rangegetResource(int)
,
getString(int)
,
getData(int)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |