freemarker.ext.beans
public class: MapModel [javadoc |
source]
java.lang.Object
freemarker.ext.beans.BeanModel
freemarker.ext.beans.StringModel
freemarker.ext.beans.MapModel
All Implemented Interfaces:
TemplateMethodModelEx, TemplateScalarModel, AdapterTemplateModel, WrapperTemplateModel, TemplateHashModelEx
A special case of BeanModel that adds implementation
for TemplateMethodModelEx on map objects that is a shortcut for the
Map.get() method. Note that if the passed argument itself is a
reflection-wrapper model, then the map lookup will be performed using the
wrapped object as the key. Note that you can call get() using the
map.key syntax inherited from BeanModel as well,
however in that case the key is always a string.
The class itself does not implement the freemarker.template.TemplateCollectionModel .
You can, however use map.entrySet(), map.keySet(), or
map.values() to obtain freemarker.template.TemplateCollectionModel instances for
various aspects of the map.
- author:
Attila - Szegedi
- version:
$ - Id: MapModel.java,v 1.26.2.3 2006/02/26 18:26:37 revusky Exp $
| Field Summary |
|---|
| static final ModelFactory | FACTORY | |
| Constructor: |
public MapModel(Map map,
BeansWrapper wrapper) {
super(map, wrapper);
}
Creates a new model that wraps the specified map object. Parameters:
map - the map object to wrap into a model.
wrapper - the BeansWrapper associated with this model.
Every model has to have an associated BeansWrapper instance. The
model gains many attributes from its wrapper, including the caching
behavior, method exposure level, method-over-item shadowing policy etc.
|
| Methods from freemarker.ext.beans.StringModel: |
|---|
|
getAsString |
| Methods from freemarker.ext.beans.BeanModel: |
|---|
|
get, getAdaptedObject, getWrappedObject, hasPlainGetMethod, invokeGenericGet, isEmpty, keySet, keys, size, toString, unwrap, values, wrap |
| Method from freemarker.ext.beans.MapModel Detail: |
public Object exec(List arguments) throws TemplateModelException {
Object key = unwrap((TemplateModel)arguments.get(0));
return wrap(((Map)object).get(key));
}
The first argument is used as a key to call the map's get method. |
protected TemplateModel invokeGenericGet(Map keyMap,
Class clazz,
String key) throws TemplateModelException {
Map map = (Map) object;
Object val = map.get(key);
if (val == null && key.length()==1) {
// just check for Character key if this is a single-character string
try{
val = map.get(new Character(key.charAt(0)));
} catch (Exception e) {}
}
return wrap(val);
}
Overridden to invoke the generic get method by casting to Map instead of
through reflection - should yield better performance. |
public boolean isEmpty() {
return ((Map)object).isEmpty() && super.isEmpty();
}
|
protected Set keySet() {
Set set = super.keySet();
set.addAll(((Map)object).keySet());
return set;
}
|
public int size() {
return keySet().size();
}
|