1 29 30 package com.caucho.jstl.el; 31 32 import com.caucho.el.Expr; 33 import com.caucho.jsp.PageContextImpl; 34 import com.caucho.util.L10N; 35 36 import javax.el.ELContext; 37 import javax.el.ELException; 38 import javax.servlet.jsp.JspException ; 39 import javax.servlet.jsp.JspWriter ; 40 import javax.servlet.jsp.tagext.BodyTagSupport ; 41 import java.io.IOException ; 42 import java.text.DecimalFormat ; 43 import java.text.NumberFormat ; 44 import java.text.ParseException ; 45 import java.util.Locale ; 46 47 50 public class ParseNumberTag extends BodyTagSupport { 51 private static L10N L = new L10N(ParseNumberTag.class); 52 53 private Expr _valueExpr; 54 private Expr _typeExpr; 55 56 private Expr _patternExpr; 57 58 private Expr _parseLocaleExpr; 59 private Expr _integerOnlyExpr; 60 61 private String _var; 62 private String _scope; 63 64 69 public void setValue(Expr value) 70 { 71 _valueExpr = value; 72 } 73 74 79 public void setType(Expr type) 80 { 81 _typeExpr = type; 82 } 83 84 89 public void setPattern(Expr pattern) 90 { 91 _patternExpr = pattern; 92 } 93 94 99 public void setParseLocale(Expr locale) 100 { 101 _parseLocaleExpr = locale; 102 } 103 104 109 public void setIntegerOnly(Expr integerOnly) 110 { 111 _integerOnlyExpr = integerOnly; 112 } 113 114 119 public void setVar(String var) 120 { 121 _var = var; 122 } 123 124 129 public void setScope(String scope) 130 { 131 _scope = scope; 132 } 133 134 137 public int doEndTag() 138 throws JspException 139 { 140 try { 141 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 142 143 JspWriter out = pageContext.getOut(); 144 145 NumberFormat format = getFormat(); 146 147 String string; 148 149 if (_valueExpr != null) 150 string = _valueExpr.evalString(pageContext.getELContext()); 151 else 152 string = bodyContent.getString().trim(); 153 154 Number value = format.parse(string); 155 156 if (_var == null) 157 out.print(value); 158 else 159 CoreSetTag.setValue(pageContext, _var, _scope, value); 160 } catch (IOException e) { 161 } catch (ParseException e) { 162 throw new JspException (e); 163 } catch (ELException e) { 164 throw new JspException (e); 165 } 166 167 return EVAL_PAGE; 168 } 169 170 protected NumberFormat getFormat() 171 throws JspException , ELException 172 { 173 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 174 ELContext env = pageContext.getELContext(); 175 176 NumberFormat format = null; 177 178 Locale locale = null; 179 180 if (_parseLocaleExpr != null) { 181 Object localeObj = _parseLocaleExpr.evalObject(env); 182 183 if (localeObj instanceof Locale ) 184 locale = (Locale ) localeObj; 185 else if (localeObj instanceof String ) 186 locale = pageContext.getLocale((String ) localeObj, null); 187 } 188 189 if (locale == null) 190 locale = pageContext.getLocale(); 191 192 String type = null; 193 if (_typeExpr != null) 194 type = _typeExpr.evalString(env); 195 196 if (type == null || type.equals("") || type.equals("number")) { 197 if (locale != null) 198 format = NumberFormat.getInstance(locale); 199 else 200 format = NumberFormat.getInstance(); 201 202 DecimalFormat decimalFormat = (DecimalFormat ) format; 203 204 if (_patternExpr != null) 205 decimalFormat.applyPattern(_patternExpr.evalString(env)); 206 } 207 else if (type.equals("percent")) { 208 if (locale != null) 209 format = NumberFormat.getPercentInstance(locale); 210 else 211 format = NumberFormat.getPercentInstance(); 212 } 213 else if (type.equals("currency")) { 214 if (locale != null) 215 format = NumberFormat.getCurrencyInstance(locale); 216 else 217 format = NumberFormat.getCurrencyInstance(locale); 218 } 219 else 220 throw new JspException (L.l("unknown formatNumber type `{0}'", 221 type)); 222 223 if (_integerOnlyExpr != null) 224 format.setParseIntegerOnly(_integerOnlyExpr.evalBoolean(env)); 225 226 return format; 227 } 228 } 229 | Popular Tags |