org.deegree_impl.model.resources
Class ResourceBundle

java.lang.Object
  extended byjava.util.ResourceBundle
      extended byorg.deegree_impl.model.resources.ResourceBundle
Direct Known Subclasses:
Resources, Resources

public class ResourceBundle
extends java.util.ResourceBundle

{link java.util.ResourceBundle} implementation using integers instead of strings for resource keys. Because it doesn't use strings, this implementation avoid adding all those string constants to .class files and runtime images. Developpers still have meaningful labels in their code (e.g. DIMENSION_MISMATCH) through a set of constants defined in interfaces. This approach furthermore give the benefict of compile-time safety. Because integer constants are inlined right into class files at compile time, the declarative interface is never loaded at run time.

This class also provides facilities for string formatting using MessageFormat.

Version:
1.0
Author:
Martin Desruisseaux

Nested Class Summary
 
Nested classes inherited from class java.util.ResourceBundle
 
Field Summary
private  java.lang.String filename
          The resource name of the binary file containing resources.
private  java.text.MessageFormat format
          The object to use for formatting messages.
private  int lastKey
          The key of the last resource requested.
private static int MAX_STRING_LENGTH
          Maximal string length for text inserted into an other text.
protected  java.lang.String[] values
          The array of resources.
 
Fields inherited from class java.util.ResourceBundle
parent
 
Constructor Summary
protected ResourceBundle(java.lang.String filename)
          Construct a new resource bundle.
 
Method Summary
private  void ensureLoaded(java.lang.String key)
          Ensure that resource values are loaded.
 java.util.Enumeration getKeys()
          Returns an enumeration of the keys.
 java.lang.String getLabel(int keyID)
          Gets a string for the given key and append ": " to it.
protected  java.lang.String getLoggerName()
          Returns the name of the logger to use.
 java.lang.String getMenuLabel(int keyID)
          Gets a string for the given key and append "..." to it.
 java.lang.String getString(int keyID)
          Gets a string for the given key from this resource bundle or one of its parents.
 java.lang.String getString(int keyID, java.lang.Object arg0)
          Gets a string for the given key and format it with the specified argument.
 java.lang.String getString(int keyID, java.lang.Object arg0, java.lang.Object arg1)
          Gets a string for the given key are replace all occurence of "{0}", "{1}", with values of arg0, arg1, etc.
 java.lang.String getString(int keyID, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2)
          Gets a string for the given key are replace all occurence of "{0}", "{1}", with values of arg0, arg1, etc.
 java.lang.String getString(int keyID, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)
          Gets a string for the given key are replace all occurence of "{0}", "{1}", with values of arg0, arg1, etc.
 java.lang.String getString(int keyID, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4)
          Gets a string for the given key are replace all occurence of "{0}", "{1}", with values of arg0, arg1, etc.
protected  java.lang.Object handleGetObject(java.lang.String key)
          Gets an object for the given key from this resource bundle.
 void list(java.io.Writer out)
          List resources to the specified stream.
private  void list(java.io.Writer out, int lower, int upper)
          List resources to the specified stream.
private static java.lang.String summarize(java.lang.String text, int maxLength)
          Make sure that the text string is no longer than maxLength characters.
private static java.lang.Object[] toArray(java.lang.Object arguments)
          Returns arguments as an array.
 java.lang.String toString()
          Returns a string representation of this object.
 
Methods inherited from class java.util.ResourceBundle
getBundle, getBundle, getBundle, getLocale, getObject, getString, getStringArray, setParent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_STRING_LENGTH

private static final int MAX_STRING_LENGTH
Maximal string length for text inserted into an other text. This parameter is used by summarize(java.lang.String, int). Resource strings are never cut to this length. However, text replacing "{0}" in a string like "Parameter name is {0}." will be cut to this length.

See Also:
Constant Field Values

filename

private final java.lang.String filename
The resource name of the binary file containing resources. It is usually a file name, but may also be the name of an entry in a JAR file.


values

protected java.lang.String[] values
The array of resources. Keys are array index. For example the value for key "14" is values[14]. This array will be loaded only when first needed. We should not load it at construction time, because some ResourceBundle objects will never ask for values. This is case especially for ancestor classes of Resources_fr_CA, Resources_en, Resources_de, etc., which will be used only if a key has not been found in the subclasse.


format

private transient java.text.MessageFormat format
The object to use for formatting messages. This object will be constructed only when first needed.


lastKey

private transient int lastKey
The key of the last resource requested. If the same resource is requested many consecutive time, knowing this fact allows to avoid invoking the costly MessageFormat.applyPattern(java.lang.String) method.

Constructor Detail

ResourceBundle

protected ResourceBundle(java.lang.String filename)
Construct a new resource bundle.

Parameters:
filename - The resource name containing resources. It is usually a filename, but may also be an entry in a JAR file.
Method Detail

getLoggerName

protected java.lang.String getLoggerName()
Returns the name of the logger to use. Default implementation returns the package name.


list

public final void list(java.io.Writer out)
                throws java.io.IOException
List resources to the specified stream. If a resource has more than one line, only the first line will be written. This method is used mostly for debugging purpose.

Parameters:
out - The destination stream.
Throws:
java.io.IOException - if an output operation failed.

list

private void list(java.io.Writer out,
                  int lower,
                  int upper)
           throws java.io.IOException
List resources to the specified stream. If a resource has more than one line, only the first line will be written. This method is used mostly for debugging purpose.

Parameters:
out - The destination stream.
lower - The beginning index (inclusive).
upper - The ending index (exclusive).
Throws:
java.io.IOException - if an output operation failed.

ensureLoaded

private void ensureLoaded(java.lang.String key)
                   throws java.util.MissingResourceException
Ensure that resource values are loaded. If they are not, load them immediately.

Parameters:
key - Key for the requested resource, or null if all resources are requested. This key is used mostly for constructing messages.
Throws:
java.util.MissingResourceException - if this method failed to load resources.

getKeys

public final java.util.Enumeration getKeys()
Returns an enumeration of the keys.


handleGetObject

protected final java.lang.Object handleGetObject(java.lang.String key)
Gets an object for the given key from this resource bundle. Returns null if this resource bundle does not contain an object for the given key.

Parameters:
key - the key for the desired object
Returns:
the object for the given key, or null
Throws:
java.lang.NullPointerException - if key is null

summarize

private static java.lang.String summarize(java.lang.String text,
                                          int maxLength)
Make sure that the text string is no longer than maxLength characters. If text is not longer, it is returned unchanged (except for trailing blancks, which are removed). If text is longer, it will be cut somewhere in the middle. This method try to cut between two words and replace the missing words with "(...)". For example, the following string:
"This sentence given as an example is way too long to be included in a message."
May be "summarized" by something like this:
"This sentence given (...) included in a message."

Parameters:
text - The sentence to summarize if it is too long.
maxLength - The maximal length allowed for text. If text is longer, it will summarized.
Returns:
A sentence not longer than text.

toArray

private static java.lang.Object[] toArray(java.lang.Object arguments)
Returns arguments as an array. If arguments is already an array, this array or a copy of this array will be returned. If arguments is not an array, it will be wrapped in an array of length 1. In all case, all array's elements will be checked for String objects. Any strings of length greater than MAX_STRING_LENGTH will be reduced using the summarize(java.lang.String, int) method.

Parameters:
arguments - The object to check.
Returns:
arguments as an array.

getMenuLabel

public final java.lang.String getMenuLabel(int keyID)
                                    throws java.util.MissingResourceException
Gets a string for the given key and append "..." to it. This is method is typically used for creating menu label.

Parameters:
keyID - The key for the desired string.
Returns:
The string for the given key.
Throws:
java.util.MissingResourceException - If no object for the given key can be found.

getLabel

public final java.lang.String getLabel(int keyID)
                                throws java.util.MissingResourceException
Gets a string for the given key and append ": " to it. This is method is typically used for creating menu label.

Parameters:
keyID - The key for the desired string.
Returns:
The string for the given key.
Throws:
java.util.MissingResourceException - If no object for the given key can be found.

getString

public final java.lang.String getString(int keyID)
                                 throws java.util.MissingResourceException
Gets a string for the given key from this resource bundle or one of its parents.

Parameters:
keyID - The key for the desired string.
Returns:
The string for the given key.
Throws:
java.util.MissingResourceException - If no object for the given key can be found.

getString

public final java.lang.String getString(int keyID,
                                        java.lang.Object arg0)
                                 throws java.util.MissingResourceException
Gets a string for the given key and format it with the specified argument. The message if formatted using MessageFormat. Calling his method is approximatively equivalent to calling:
     String pattern = getString(key);
     Format f = new MessageFormat(pattern);
     return f.format(arg0);
 
If arg0 is not already an array, it will be wrapped into an array of length 1. Using MessageFormat, all occurence of "{0}", "{1}", "{2}" in the resource string will be replaced by arg0[0], arg0[1], arg0[2], etc.

Parameters:
keyID - The key for the desired string.
arg0 - A single object or an array of objects to be formatted and substituted.
Returns:
The string for the given key.
Throws:
java.util.MissingResourceException - If no object for the given key can be found.
See Also:
MessageFormat

getString

public final java.lang.String getString(int keyID,
                                        java.lang.Object arg0,
                                        java.lang.Object arg1)
                                 throws java.util.MissingResourceException
Gets a string for the given key are replace all occurence of "{0}", "{1}", with values of arg0, arg1, etc.

Parameters:
keyID - The key for the desired string.
arg0 - Value to substitute to "{0}".
arg1 - Value to substitute to "{1}".
Returns:
The formatted string for the given key.
Throws:
java.util.MissingResourceException - If no object for the given key can be found.

getString

public final java.lang.String getString(int keyID,
                                        java.lang.Object arg0,
                                        java.lang.Object arg1,
                                        java.lang.Object arg2)
                                 throws java.util.MissingResourceException
Gets a string for the given key are replace all occurence of "{0}", "{1}", with values of arg0, arg1, etc.

Parameters:
keyID - The key for the desired string.
arg0 - Value to substitute to "{0}".
arg1 - Value to substitute to "{1}".
arg2 - Value to substitute to "{2}".
Returns:
The formatted string for the given key.
Throws:
java.util.MissingResourceException - If no object for the given key can be found.

getString

public final java.lang.String getString(int keyID,
                                        java.lang.Object arg0,
                                        java.lang.Object arg1,
                                        java.lang.Object arg2,
                                        java.lang.Object arg3)
                                 throws java.util.MissingResourceException
Gets a string for the given key are replace all occurence of "{0}", "{1}", with values of arg0, arg1, etc.

Parameters:
keyID - The key for the desired string.
arg0 - Value to substitute to "{0}".
arg1 - Value to substitute to "{1}".
arg2 - Value to substitute to "{2}".
arg3 - Value to substitute to "{3}".
Returns:
The formatted string for the given key.
Throws:
java.util.MissingResourceException - If no object for the given key can be found.

getString

public final java.lang.String getString(int keyID,
                                        java.lang.Object arg0,
                                        java.lang.Object arg1,
                                        java.lang.Object arg2,
                                        java.lang.Object arg3,
                                        java.lang.Object arg4)
                                 throws java.util.MissingResourceException
Gets a string for the given key are replace all occurence of "{0}", "{1}", with values of arg0, arg1, etc.

Parameters:
keyID - The key for the desired string.
arg0 - Value to substitute to "{0}".
arg1 - Value to substitute to "{1}".
arg2 - Value to substitute to "{2}".
arg3 - Value to substitute to "{3}".
arg4 - Value to substitute to "{4}".
Returns:
The formatted string for the given key.
Throws:
java.util.MissingResourceException - If no object for the given key can be found.

toString

public java.lang.String toString()
Returns a string representation of this object. This method is for debugging purpose only.