Method from freemarker.ext.beans.ResourceBundleModel Detail: |
public Object exec(List arguments) throws TemplateModelException {
// Must have at least one argument - the key
if(arguments.size() < 1)
throw new TemplateModelException("No message key was specified");
// Read it
Iterator it = arguments.iterator();
String key = unwrap((TemplateModel)it.next()).toString();
try
{
if(!it.hasNext())
{
return wrap(((ResourceBundle)object).getObject(key));
}
// Copy remaining arguments into an Object[]
int args = arguments.size() - 1;
Object[] params = new Object[args];
for(int i = 0; i < args; ++i)
params[i] = unwrap((TemplateModel)it.next());
// Invoke format
return new StringModel(format(key, params), wrapper);
}
catch(MissingResourceException e)
{
throw new TemplateModelException("No such key: " + key);
}
catch(Exception e)
{
throw new TemplateModelException(e.getMessage());
}
}
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. |
public String format(String key,
Object[] params) throws MissingResourceException {
// Check to see if we already have a cache for message formats
// and construct it if we don't
// NOTE: this block statement should be synchronized. However
// concurrent creation of two caches will have no harmful
// consequences, and we avoid a performance hit.
/* synchronized(this) */
{
if(formats == null)
formats = new Hashtable();
}
MessageFormat format = null;
// Check to see if we already have a requested MessageFormat cached
// and construct it if we don't
// NOTE: this block statement should be synchronized. However
// concurrent creation of two formats will have no harmful
// consequences, and we avoid a performance hit.
/* synchronized(formats) */
{
format = (MessageFormat)formats.get(key);
if(format == null)
{
format = new MessageFormat(((ResourceBundle)object).getString(key));
format.setLocale(getBundle().getLocale());
formats.put(key, format);
}
}
// Perform the formatting. We synchronize on it in case it
// contains date formatting, which is not thread-safe.
synchronized(format)
{
return format.format(params);
}
}
Provides direct access to caching format engine from code (instead of from script). |
public ResourceBundle getBundle() {
return (ResourceBundle)object;
}
|
protected TemplateModel invokeGenericGet(Map keyMap,
Class clazz,
String key) throws TemplateModelException {
try
{
return wrap(((ResourceBundle)object).getObject(key));
}
catch(MissingResourceException e)
{
throw new TemplateModelException("No such key: " + key);
}
}
Overridden to invoke the getObject method of the resource bundle. |
public boolean isEmpty() {
return !((ResourceBundle)object).getKeys().hasMoreElements() &&
super.isEmpty();
}
Returns true if this bundle contains no objects. |
protected Set keySet() {
Set set = super.keySet();
Enumeration e = ((ResourceBundle)object).getKeys();
while (e.hasMoreElements()) {
set.add(e.nextElement());
}
return set;
}
|
public int size() {
return keySet().size();
}
|