1 52 53 package freemarker.ext.beans; 54 55 import java.util.List ; 56 import java.util.Map ; 57 import java.util.Set ; 58 59 import freemarker.ext.util.ModelFactory; 60 import freemarker.template.ObjectWrapper; 61 import freemarker.template.TemplateMethodModelEx; 62 import freemarker.template.TemplateModel; 63 import freemarker.template.TemplateModelException; 64 65 80 public class MapModel 81 extends 82 StringModel 83 implements 84 TemplateMethodModelEx 85 { 86 static final ModelFactory FACTORY = 87 new ModelFactory() 88 { 89 public TemplateModel create(Object object, ObjectWrapper wrapper) 90 { 91 return new MapModel((Map )object, (BeansWrapper)wrapper); 92 } 93 }; 94 95 103 public MapModel(Map map, BeansWrapper wrapper) 104 { 105 super(map, wrapper); 106 } 107 108 111 public Object exec(List arguments) 112 throws 113 TemplateModelException 114 { 115 Object key = unwrap((TemplateModel)arguments.get(0)); 116 return wrap(((Map )object).get(key)); 117 } 118 119 123 protected TemplateModel invokeGenericGet(Map keyMap, Class clazz, String key) 124 throws TemplateModelException 125 { 126 return wrap(((Map )object).get(key)); 127 } 128 129 public boolean isEmpty() 130 { 131 return ((Map )object).isEmpty() && super.isEmpty(); 132 } 133 134 public int size() 135 { 136 return keySet().size(); 137 } 138 139 protected Set keySet() 140 { 141 Set set = super.keySet(); 142 set.addAll(((Map )object).keySet()); 143 return set; 144 } 145 } 146 | Popular Tags |