freemarker.ext.beans
Class ResourceBundleModel

java.lang.Object
  |
  +--freemarker.ext.beans.BeanModelBase
        |
        +--freemarker.ext.beans.BeanModel
              |
              +--freemarker.ext.beans.ResourceBundleModel
All Implemented Interfaces:
TemplateHashModel, TemplateMethodModel, TemplateMethodModelEx, TemplateModel, TemplateScalarModel

public final class ResourceBundleModel
extends BeanModel
implements TemplateMethodModelEx

A hash model that wraps a resource bundle. Makes it convenient to store localized content in the data model. It also acts as a method model that will take a resource key and arbitrary number of arguments and will apply MessageFormat with arguments on the string represented by the key.

Typical usages:

Version:
1.0
Author:
Attila Szegedi, szegedia@freemail.hu

Fields inherited from class freemarker.ext.beans.BeanModel
TYPE_ARRAY, TYPE_COLLECTION, TYPE_ENUMERATION, TYPE_ITERATOR, TYPE_MAP, TYPE_OBJECT, TYPE_OBJECT_BASE, TYPE_RESOURCE_BUNDLE
 
Constructor Summary
ResourceBundleModel(java.util.ResourceBundle bundle)
           
 
Method Summary
 TemplateModel exec(java.util.List arguments)
          Takes first argument as a resource key, looks up a string in resource bundle with this key, then applies a MessageFormat.format on the string with the rest of the arguments.
 java.lang.String format(java.lang.String key, java.lang.Object[] params)
          Provides direct access to caching format engine from code (instead of from script).
 java.util.ResourceBundle getBundle()
           
static ResourceBundleModel getInstance(java.util.ResourceBundle object)
          Returns a model wrapping the specified resource bundle object.
 int getType()
          Returns the type of this object (which is TYPE_RESOURCE_BUNDLE)
protected  TemplateModel invokeGenericGet(java.util.Map keyMap, java.lang.Class clazz, java.lang.String key)
          Overridden to invoke the getObject method of the resource bundle.
 boolean isEmpty()
          Returns true if this bundle contains no objects.
 
Methods inherited from class freemarker.ext.beans.BeanModel
get, getAsString, getInstance, getInstance, getObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceBundleModel

public ResourceBundleModel(java.util.ResourceBundle bundle)
Method Detail

getInstance

public static final ResourceBundleModel getInstance(java.util.ResourceBundle object)
Returns a model wrapping the specified resource bundle object. If there is already a cached model instance for this resource bundle, returns the cached model instance. Models are cached using WeakReference objects. The caching can be turned off by setting the freemarker.beans.nocache system property to true. In this case calling this method is equivalent to constructing a new model.
Parameters:
object - the resource bundle to wrap into a model.
Returns:
the model for the resource bundle

getType

public int getType()
Returns the type of this object (which is TYPE_RESOURCE_BUNDLE)
Overrides:
getType in class BeanModel

invokeGenericGet

protected TemplateModel invokeGenericGet(java.util.Map keyMap,
                                         java.lang.Class clazz,
                                         java.lang.String key)
                                  throws TemplateModelException
Overridden to invoke the getObject method of the resource bundle.
Overrides:
invokeGenericGet in class BeanModel

isEmpty

public boolean isEmpty()
Returns true if this bundle contains no objects.
Specified by:
isEmpty in interface TemplateModel
Overrides:
isEmpty in class freemarker.ext.beans.BeanModelBase
Following copied from interface: freemarker.template.TemplateModel
Returns:
true if this object is empty.

exec

public TemplateModel exec(java.util.List arguments)
                   throws TemplateModelException
Takes first argument as a resource key, looks up a string in resource bundle with this key, then applies a MessageFormat.format on the string with the rest of the arguments. The created MessageFormats are cached for later reuse.
Specified by:
exec in interface TemplateMethodModel
Following copied from interface: freemarker.template.TemplateMethodModel
Parameters:
arguments - a List of String objects containing the values of the arguments passed to the method.
Returns:
the TemplateModel produced by the method, or null.

format

public java.lang.String format(java.lang.String key,
                               java.lang.Object[] params)
                        throws java.util.MissingResourceException
Provides direct access to caching format engine from code (instead of from script).

getBundle

public java.util.ResourceBundle getBundle()