1 28 package net.sf.jasperreports.engine.fill; 29 30 import java.text.MessageFormat ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 import java.util.MissingResourceException ; 34 import java.util.ResourceBundle ; 35 36 import net.sf.jasperreports.engine.JRException; 37 import net.sf.jasperreports.engine.JRExpression; 38 import net.sf.jasperreports.engine.JRParameter; 39 import net.sf.jasperreports.engine.JRReport; 40 import net.sf.jasperreports.engine.JRRuntimeException; 41 42 48 public abstract class JREvaluator 49 { 50 53 private JRFillParameter resourceBundle = null; 54 55 58 private byte whenResourceMissingType; 59 60 63 private JRFillParameter locale; 64 65 68 protected JREvaluator() 69 { 70 } 71 72 73 82 public void init(Map parametersMap, Map fieldsMap, Map variablesMap, byte resourceMissingType) throws JRException 83 { 84 this.whenResourceMissingType = resourceMissingType; 85 this.resourceBundle = (JRFillParameter)parametersMap.get(JRParameter.REPORT_RESOURCE_BUNDLE); 86 this.locale = (JRFillParameter)parametersMap.get(JRParameter.REPORT_LOCALE); 87 customizedInit(parametersMap, fieldsMap, variablesMap); 88 } 89 90 91 99 public String msg(String pattern, Object arg0) 100 { 101 return getMessageFormat(pattern).format(new Object [] { arg0 }, new StringBuffer (), null).toString(); 102 } 103 104 113 public String msg(String pattern, Object arg0, Object arg1) 114 { 115 return getMessageFormat(pattern).format(new Object [] { arg0, arg1 }, new StringBuffer (), null).toString(); 116 } 117 118 119 129 public String msg(String pattern, Object arg0, Object arg1, Object arg2) 130 { 131 return getMessageFormat(pattern).format(new Object [] { arg0, arg1, arg2 }, new StringBuffer (), null).toString(); 132 } 133 134 142 public String msg(String pattern, Object [] args) 143 { 144 return getMessageFormat(pattern).format(args, new StringBuffer (), null).toString(); 145 } 146 147 154 public String str(String key) 155 { 156 String str = null; 157 158 try 159 { 160 str = ((ResourceBundle ) resourceBundle.getValue()).getString(key); 161 } 162 catch (NullPointerException e) 163 { 164 str = handleMissingResource(key, e); 165 } 166 catch (MissingResourceException e) 167 { 168 str = handleMissingResource(key, e); 169 } 170 171 return str; 172 } 173 174 175 178 public Object evaluate(JRExpression expression) throws JRExpressionEvalException 179 { 180 Object value = null; 181 182 if (expression != null) 183 { 184 try 185 { 186 value = evaluate(expression.getId()); 187 } 188 catch (NullPointerException e) 189 { 190 } 191 catch (OutOfMemoryError e) 192 { 193 throw e; 194 } 195 catch (Throwable e) 196 { 197 throw new JRExpressionEvalException(expression, e); 198 } 199 } 200 201 return value; 202 } 203 204 205 208 public Object evaluateOld(JRExpression expression) throws JRExpressionEvalException 209 { 210 Object value = null; 211 212 if (expression != null) 213 { 214 try 215 { 216 value = evaluateOld(expression.getId()); 217 } 218 catch (NullPointerException e) 219 { 220 } 221 catch (OutOfMemoryError e) 222 { 223 throw e; 224 } 225 catch (Throwable e) 226 { 227 throw new JRExpressionEvalException(expression, e); 228 } 229 } 230 231 return value; 232 } 233 234 235 238 public Object evaluateEstimated(JRExpression expression) throws JRExpressionEvalException 239 { 240 Object value = null; 241 242 if (expression != null) 243 { 244 try 245 { 246 value = evaluateEstimated(expression.getId()); 247 } 248 catch (NullPointerException e) 249 { 250 } 251 catch (OutOfMemoryError e) 252 { 253 throw e; 254 } 255 catch (Throwable e) 256 { 257 throw new JRExpressionEvalException(expression, e); 258 } 259 } 260 261 return value; 262 } 263 264 265 276 protected String handleMissingResource(String key, Exception e) throws JRRuntimeException 277 { 278 String str; 279 switch (whenResourceMissingType) 280 { 281 case JRReport.WHEN_RESOURCE_MISSING_TYPE_EMPTY: 282 { 283 str = ""; 284 break; 285 } 286 case JRReport.WHEN_RESOURCE_MISSING_TYPE_KEY: 287 { 288 str = key; 289 break; 290 } 291 case JRReport.WHEN_RESOURCE_MISSING_TYPE_ERROR: 292 { 293 throw new JRRuntimeException("Resource nout found for key \"" + key + "\".", e); 294 } 295 case JRReport.WHEN_RESOURCE_MISSING_TYPE_NULL: 296 default: 297 { 298 str = null; 299 break; 300 } 301 } 302 303 return str; 304 } 305 306 307 315 protected abstract void customizedInit(Map parametersMap, Map fieldsMap, Map variablesMap) throws JRException; 316 317 318 328 protected abstract Object evaluate(int id) throws Throwable ; 329 330 331 341 protected abstract Object evaluateOld(int id) throws Throwable ; 342 343 344 353 protected abstract Object evaluateEstimated(int id) throws Throwable ; 354 355 356 359 private MessageFormat getMessageFormat(String pattern) 360 { 361 MessageFormat messageFormat = new MessageFormat (""); 362 messageFormat.setLocale((Locale )locale.getValue()); 363 messageFormat.applyPattern(pattern); 364 return messageFormat; 365 } 366 367 } 368 | Popular Tags |