org.szegedi.expose.model
Class ResourceBundleHashModel
java.lang.Object
|
+--org.szegedi.expose.model.ReflectionObjectModelBase
|
+--org.szegedi.expose.model.ResourceBundleHashModel
- All Implemented Interfaces:
- freemarker.template.TemplateHashModel, freemarker.template.TemplateMethodModel, freemarker.template.TemplateModel, freemarker.template.TemplateScalarModel
- public final class ResourceBundleHashModel
- extends ReflectionObjectModelBase
- implements freemarker.template.TemplateHashModel, freemarker.template.TemplateMethodModel
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:
- bundle.resourceKey will retrieve the object from resource bundle
with key resourceKey
- bundle("patternKey", arg1, arg2, arg3) will retrieve the string
from resource bundle with key patternKey, and will use it as a pattern
for MessageFormat with arguments arg1, arg2 and arg3
- Version:
- 1.0
- Author:
- Attila Szegedi, attila@szegedi.org
|
Method Summary |
freemarker.template.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). |
freemarker.template.TemplateModel |
get(java.lang.String key)
Retrieves the object with specified key from resource bundle, and returns it
wrapped in an appropriate TemplateModel. |
java.util.ResourceBundle |
getBundle()
|
static ResourceBundleHashModel |
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) |
boolean |
isEmpty()
Returns true if this bundle contains no objects. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ResourceBundleHashModel
public ResourceBundleHashModel(java.util.ResourceBundle bundle)
getInstance
public static final ResourceBundleHashModel 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 expose.reflection.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 ReflectionObjectModelBase
get
public freemarker.template.TemplateModel get(java.lang.String key)
throws freemarker.template.TemplateModelException
- Retrieves the object with specified key from resource bundle, and returns it
wrapped in an appropriate TemplateModel.
- Specified by:
get in interface freemarker.template.TemplateHashModel
isEmpty
public boolean isEmpty()
- Returns true if this bundle contains no objects.
- Specified by:
isEmpty in interface freemarker.template.TemplateModel- Overrides:
isEmpty in class ReflectionObjectModelBase
exec
public freemarker.template.TemplateModel exec(java.util.List arguments)
throws freemarker.template.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 freemarker.template.TemplateMethodModel
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()