1 52 53 package freemarker.ext.beans; 54 55 import java.util.List ; 56 import java.util.Map ; 57 58 import freemarker.ext.util.ModelFactory; 59 import freemarker.ext.util.WrapperTemplateModel; 60 import freemarker.core.CollectionAndSequence; 61 import freemarker.template.ObjectWrapper; 62 import freemarker.template.SimpleSequence; 63 import freemarker.template.TemplateCollectionModel; 64 import freemarker.template.TemplateHashModelEx; 65 import freemarker.template.TemplateMethodModelEx; 66 import freemarker.template.TemplateModel; 67 import freemarker.template.TemplateModelException; 68 import freemarker.template.WrappingTemplateModel; 69 70 78 public class SimpleMapModel extends WrappingTemplateModel 79 implements TemplateHashModelEx, TemplateMethodModelEx, WrapperTemplateModel { 80 static final ModelFactory FACTORY = 81 new ModelFactory() 82 { 83 public TemplateModel create(Object object, ObjectWrapper wrapper) 84 { 85 return new SimpleMapModel((Map )object, (BeansWrapper)wrapper); 86 } 87 }; 88 89 private final Map map; 90 91 public SimpleMapModel(Map map, BeansWrapper wrapper) 92 { 93 super(wrapper); 94 this.map = map; 95 } 96 97 public TemplateModel get(String key) throws TemplateModelException { 98 return wrap(map.get(key)); 99 } 100 101 public Object exec(List args) throws TemplateModelException { 102 Object key = ((BeansWrapper)getObjectWrapper()).unwrap((TemplateModel)args.get(0)); 103 return wrap(map.get(key)); 104 } 105 106 public boolean isEmpty() { 107 return map.isEmpty(); 108 } 109 110 public int size() { 111 return map.size(); 112 } 113 114 public TemplateCollectionModel keys() { 115 return new CollectionAndSequence(new SimpleSequence(map.keySet(), getObjectWrapper())); 116 } 117 118 public TemplateCollectionModel values() { 119 return new CollectionAndSequence(new SimpleSequence(map.values(), getObjectWrapper())); 120 } 121 122 public Object getWrappedObject() { 123 return map; 124 } 125 } 126 | Popular Tags |