1 28 29 package com.caucho.jstl.el; 30 31 import com.caucho.el.Expr; 32 import com.caucho.jsp.BodyContentImpl; 33 import com.caucho.jsp.PageContextImpl; 34 import com.caucho.util.L10N; 35 36 import javax.el.ELContext; 37 import javax.servlet.jsp.JspException ; 38 import javax.servlet.jsp.JspWriter ; 39 import javax.servlet.jsp.tagext.BodyTagSupport ; 40 import java.text.DecimalFormat ; 41 import java.text.DecimalFormatSymbols ; 42 import java.text.NumberFormat ; 43 import java.util.Locale ; 44 45 48 public class FormatNumberTag extends BodyTagSupport { 49 private static L10N L = new L10N(FormatNumberTag.class); 50 51 private Expr _valueExpr; 52 private Expr _typeExpr; 53 54 private Expr _patternExpr; 55 56 private Expr _currencyCodeExpr; 57 private Expr _currencySymbolExpr; 58 59 private Expr _groupingUsedExpr; 60 61 private Expr _maxIntegerDigitsExpr; 62 private Expr _minIntegerDigitsExpr; 63 private Expr _maxFractionDigitsExpr; 64 private Expr _minFractionDigitsExpr; 65 66 private String _var; 67 private String _scope; 68 69 74 public void setValue(Expr value) 75 { 76 _valueExpr = value; 77 } 78 79 84 public void setType(Expr type) 85 { 86 _typeExpr = type; 87 } 88 89 94 public void setPattern(Expr pattern) 95 { 96 _patternExpr = pattern; 97 } 98 99 104 public void setCurrencyCode(Expr currencyCode) 105 { 106 _currencyCodeExpr = currencyCode; 107 } 108 109 114 public void setCurrencySymbol(Expr currencySymbol) 115 { 116 _currencySymbolExpr = currencySymbol; 117 } 118 119 124 public void setGroupingUsed(Expr groupingUsed) 125 { 126 _groupingUsedExpr = groupingUsed; 127 } 128 129 134 public void setMinIntegerDigits(Expr minIntegerDigits) 135 { 136 _minIntegerDigitsExpr = minIntegerDigits; 137 } 138 139 144 public void setMaxIntegerDigits(Expr maxIntegerDigits) 145 { 146 _maxIntegerDigitsExpr = maxIntegerDigits; 147 } 148 149 154 public void setMinFractionDigits(Expr minFractionDigits) 155 { 156 _minFractionDigitsExpr = minFractionDigits; 157 } 158 159 164 public void setMaxFractionDigits(Expr maxFractionDigits) 165 { 166 _maxFractionDigitsExpr = maxFractionDigits; 167 } 168 169 174 public void setVar(String var) 175 { 176 _var = var; 177 } 178 179 184 public void setScope(String scope) 185 { 186 _scope = scope; 187 } 188 189 192 public int doEndTag() 193 throws JspException 194 { 195 try { 196 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 197 ELContext env = pageContext.getELContext(); 198 199 JspWriter out = pageContext.getOut(); 200 201 double number; 202 203 BodyContentImpl body = (BodyContentImpl) getBodyContent(); 204 205 if (_valueExpr != null) 206 number = _valueExpr.evalDouble(env); 207 else if (body != null) { 208 String value = body.getTrimString(); 209 210 if (! value.equals("")) 211 number = Double.parseDouble(value); 212 else 213 number = 0.0; 214 } 215 else 216 number = 0.0; 217 218 if (Double.isNaN(number)) 219 number = 0; 220 221 NumberFormat format = getFormat(); 222 223 String value; 224 if (format != null) 225 value = format.format(number); 226 else 227 value = String.valueOf(number); 228 229 if (_var == null) 230 out.print(value); 231 else 232 CoreSetTag.setValue(pageContext, _var, _scope, value); 233 } catch (Exception e) { 234 throw new JspException (e); 235 } 236 237 return EVAL_PAGE; 238 } 239 240 protected NumberFormat getFormat() 241 throws JspException 242 { 243 try { 244 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 245 ELContext env = pageContext.getELContext(); 246 247 NumberFormat format = null; 248 249 Locale locale = pageContext.getLocale(); 250 251 String type = null; 252 if (_typeExpr != null) 253 type = _typeExpr.evalString(env); 254 255 if (type == null || type.equals("") || type.equals("number")) { 256 if (locale != null) 257 format = NumberFormat.getInstance(locale); 258 else 259 format = NumberFormat.getInstance(); 260 261 DecimalFormat decimalFormat = (DecimalFormat ) format; 262 263 if (_patternExpr != null) 264 decimalFormat.applyPattern(_patternExpr.evalString(env)); 265 } 266 else if (type.equals("percent")) { 267 if (locale != null) 268 format = NumberFormat.getPercentInstance(locale); 269 else 270 format = NumberFormat.getPercentInstance(); 271 } 272 else if (type.equals("currency")) { 273 if (locale != null) 274 format = NumberFormat.getCurrencyInstance(locale); 275 else 276 format = NumberFormat.getCurrencyInstance(); 277 278 if ((_currencyCodeExpr != null || _currencySymbolExpr != null) && 279 format instanceof DecimalFormat ) { 280 DecimalFormat dFormat = (DecimalFormat ) format; 281 DecimalFormatSymbols dSymbols; 282 283 dSymbols = dFormat.getDecimalFormatSymbols(); 284 285 if (_currencyCodeExpr != null && dSymbols != null) 286 dSymbols.setInternationalCurrencySymbol(_currencyCodeExpr.evalString(env)); 287 288 if (_currencySymbolExpr != null && dSymbols != null) 289 dSymbols.setCurrencySymbol(_currencySymbolExpr.evalString(env)); 290 291 dFormat.setDecimalFormatSymbols(dSymbols); 292 } 293 } 294 else 295 throw new JspException (L.l("unknown formatNumber type `{0}'", 296 type)); 297 298 if (_groupingUsedExpr != null) 299 format.setGroupingUsed(_groupingUsedExpr.evalBoolean(env)); 300 301 if (_minIntegerDigitsExpr != null) 302 format.setMinimumIntegerDigits((int) _minIntegerDigitsExpr.evalLong(env)); 303 304 if (_maxIntegerDigitsExpr != null) 305 format.setMaximumIntegerDigits((int) _maxIntegerDigitsExpr.evalLong(env)); 306 307 if (_minFractionDigitsExpr != null) 308 format.setMinimumFractionDigits((int) _minFractionDigitsExpr.evalLong(env)); 309 310 if (_maxFractionDigitsExpr != null) 311 format.setMaximumFractionDigits((int) _maxFractionDigitsExpr.evalLong(env)); 312 313 return format; 314 } catch (Exception e) { 315 throw new JspException (e); 316 } 317 } 318 } 319 | Popular Tags |