FileDocCategorySizeDatePackage
ResourceManager.javaAPI DocphoneME MR2 API (J2ME)15791Wed May 02 18:00:46 BST 2007com.sun.j2me.global

ResourceManager

public class ResourceManager extends Object
Resource manager functions implemetation. Provides a functionality of {@link javax.microedition.global.ResourceManager}. This class is required due to requirement do not have public/protected constructor in javax.microedition.global.ResourceManager. More detailed comments see for javax.microedition.global.ResourceManager.

Fields Summary
private static com.sun.j2me.global.ResourceAbstractionLayer
abstractionLayer
The instance of ResourceAbstractionLayer.
public static final String
DEVICE
Constant to indicate device resources.
private String
baseName
The base name.
private String
locale
The locale identifier.
Constructors Summary
protected ResourceManager()
Creates a new instance of ResourceManager.

 
Methods Summary
public java.lang.StringgetBaseName()
Gets the base name of this resource manager.

return
the base name (DEVICE if this resource manager is retrieving device-specific resources)

        return baseName;
    
public byte[]getData(int id)
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.

param
id the resource ID
return
byte array containing the data
throws
ResourceException if the resource does not exist, or if the resource is not the correct type
throws
IllegalArgumentException if the resource ID is invalid

        throw new ResourceException(ResourceException.UNKNOWN_ERROR, "");
    
public java.lang.StringgetLocale()
Gets current locale of this ResourceManager.

return
the locale identifier

        return locale;
    
public static final com.sun.j2me.global.ResourceManagergetManager(java.lang.String baseName, java.lang.String locale)
Gets a resource manager for the specified base name and locale.

If the base name is {@link #DEVICE} (the empty string), the resource manager can only retrieve device resources. Other base names retrieve application resources. The base name must not be 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.

param
baseName the base name, non-empty for application resources, DEVICE (the empty string) for device resources
param
locale the locale to use either as is or as a starting point for hierarchical matching
return
the manager instance for the base name and the specified or matched locale
throws
ResourceException if no resources for the combination of base name and locale are found, or the resource file is invalid
see
#DEVICE


                                                                                                                                                                                                                                                                                                                                                             
         
                
        // DevResourceManager
        if (baseName.equals(DEVICE)) {
            ResourceManagerFactory devMFactory =
                    abstractionLayer.getDevResourceManagerFactory();
            return devMFactory.getManager(DEVICE, locale);
        }
        // appResourceManager
        else {
            ResourceManagerFactory appMFactory =
                    abstractionLayer.getAppResourceManagerFactory();
            return appMFactory.getManager(baseName, locale);
        }
    
public static final com.sun.j2me.global.ResourceManagergetManager(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.

If the base name is {@link #DEVICE} (the empty string), the resource manager can only retrieve device resources. Other base names retrieve application resources. The base name MUST NOT be 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. 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.

param
baseName the base name, non-empty for application resources, or DEVICE (the empty string) for device resources
param
locales the array of locale identifiers to try
return
the manager instance for the base name and the matched locale
see
#DEVICE


        String[] norm_locs = new String[locales.length];
        for (int i = 0; i < locales.length; i++) {
            norm_locs[i] = LocaleHelpers.normalizeLocale(locales[i]);
        }

        // DevResourceManager
        if (baseName.equals(DEVICE)) {
            ResourceManagerFactory devMFactory =
                    abstractionLayer.getDevResourceManagerFactory();
            return devMFactory.getManager(DEVICE, norm_locs);
        }
        // appResourceManager
        else {
            ResourceManagerFactory appMFactory =
                    abstractionLayer.getAppResourceManagerFactory();
            return appMFactory.getManager(baseName, norm_locs);
        }

    
public java.lang.ObjectgetResource(int id)
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.

param
id the resource ID
return
the resource object
throws
ResourceException if the resource is not found, or if an application resource has an unknown type
throws
IllegalArgumentException if the resource ID is invalid
see
#getString(int)
see
#getData(int)

        throw new ResourceException(ResourceException.UNKNOWN_ERROR, "");
    
public java.lang.StringgetString(int id)
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.

param
id the resource ID
return
the string
throws
ResourceException if the resource does not exist, or if the resource is not the correct type
throws
IllegalArgumentException if the resource ID is invalid

        throw new ResourceException(ResourceException.UNKNOWN_ERROR, "");
    
public 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 {@link #DEVICE} (the empty string), the set of supported device locales is returned instead.

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.

param
baseName the base name, non-empty for application resources, DEVICE (the empty string) for device resources
return
the list of supported locales for the specified base name, possibly empty
throws
NullPointerException if the base name is null
throws
ResourceException if the meta-information file is not found or it is not of the specified format
see
javax.microedition.global.Formatter#getSupportedLocales()
see
javax.microedition.global.StringComparator#getSupportedLocales()
see
javax.microedition.global.ResourceException

        if (baseName == null) {
            throw new NullPointerException("Base name is null");
        }

        // DevResourceManager
        if (baseName.equals(DEVICE)) {
            ResourceManagerFactory devMFactory =
                    abstractionLayer.getDevResourceManagerFactory();
            return devMFactory.getSupportedLocales(baseName);
        }
        // appResourceManager
        else {
            ResourceManagerFactory appMFactory =
                    abstractionLayer.getAppResourceManagerFactory();
            return appMFactory.getSupportedLocales(baseName);
        }
    
public booleanisCaching()
Returns the caching status of this resource manager.

return
true if caching, false if not

        return false;
    
public booleanisValidResourceID(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.

param
id the resource ID
return
true if the resource ID is valid, false if not
throws
IllegalArgumentException if the resource ID is not in the defined range
see
#getResource(int)
see
#getString(int)
see
#getData(int)

        return false;
    
protected voidsetBaseName(java.lang.String baseName)
Sets base name for resource files used by this ResourceManager.

param
baseName the base name, non-empty for application

        this.baseName = baseName;
    
protected voidsetLocale(java.lang.String locale)
Sets locale code for this ResourceManager.

param
locale the locale code

        this.locale = locale;