1 52 53 package freemarker.ext.beans; 54 55 import java.text.MessageFormat ; 56 import java.util.Hashtable ; 57 import java.util.Iterator ; 58 import java.util.Enumeration ; 59 import java.util.List ; 60 import java.util.Map ; 61 import java.util.Set ; 62 import java.util.MissingResourceException ; 63 import java.util.ResourceBundle ; 64 65 import freemarker.ext.util.ModelFactory; 66 import freemarker.template.ObjectWrapper; 67 import freemarker.template.TemplateMethodModelEx; 68 import freemarker.template.TemplateModel; 69 import freemarker.template.TemplateModelException; 70 71 88 public class ResourceBundleModel 89 extends 90 BeanModel 91 implements 92 TemplateMethodModelEx 93 { 94 static final ModelFactory FACTORY = 95 new ModelFactory() 96 { 97 public TemplateModel create(Object object, ObjectWrapper wrapper) 98 { 99 return new ResourceBundleModel((ResourceBundle )object, (BeansWrapper)wrapper); 100 } 101 }; 102 103 private Hashtable formats = null; 104 105 public ResourceBundleModel(ResourceBundle bundle, BeansWrapper wrapper) 106 { 107 super(bundle, wrapper); 108 } 109 110 113 protected TemplateModel invokeGenericGet(Map keyMap, Class clazz, String key) 114 throws 115 TemplateModelException 116 { 117 try 118 { 119 return wrap(((ResourceBundle )object).getObject(key)); 120 } 121 catch(MissingResourceException e) 122 { 123 throw new TemplateModelException("No such key: " + key); 124 } 125 } 126 127 130 public boolean isEmpty() 131 { 132 return !((ResourceBundle )object).getKeys().hasMoreElements() && 133 super.isEmpty(); 134 } 135 136 public int size() 137 { 138 return keySet().size(); 139 } 140 141 protected Set keySet() 142 { 143 Set set = super.keySet(); 144 Enumeration e = ((ResourceBundle )object).getKeys(); 145 while (e.hasMoreElements()) { 146 set.add(e.nextElement()); 147 } 148 return set; 149 } 150 151 156 public Object exec(List arguments) 157 throws 158 TemplateModelException 159 { 160 if(arguments.size() < 1) 162 throw new TemplateModelException("No message key was specified"); 163 Iterator it = arguments.iterator(); 165 String key = unwrap((TemplateModel)it.next()).toString(); 166 try 167 { 168 if(!it.hasNext()) 169 { 170 return wrap(((ResourceBundle )object).getObject(key)); 171 } 172 173 int args = arguments.size() - 1; 175 Object [] params = new Object [args]; 176 for(int i = 0; i < args; ++i) 177 params[i] = unwrap((TemplateModel)it.next()); 178 179 return new StringModel(format(key, params), wrapper); 181 } 182 catch(MissingResourceException e) 183 { 184 throw new TemplateModelException("No such key: " + key); 185 } 186 catch(Exception e) 187 { 188 throw new TemplateModelException(e.getMessage()); 189 } 190 } 191 192 195 public String format(String key, Object [] params) 196 throws 197 MissingResourceException 198 { 199 205 { 206 if(formats == null) 207 formats = new Hashtable (); 208 } 209 210 MessageFormat format = null; 211 217 { 218 format = (MessageFormat )formats.get(key); 219 if(format == null) 220 { 221 format = new MessageFormat (((ResourceBundle )object).getString(key)); 222 formats.put(key, format); 223 } 224 } 225 226 synchronized(format) 229 { 230 return format.format(params); 231 } 232 } 233 234 public ResourceBundle getBundle() 235 { 236 return (ResourceBundle )object; 237 } 238 } 239 | Popular Tags |