1 28 29 package com.caucho.jstl.rt; 30 31 import com.caucho.jsp.BodyContentImpl; 32 import com.caucho.jsp.PageContextImpl; 33 import com.caucho.util.L10N; 34 35 import javax.servlet.jsp.JspException ; 36 import javax.servlet.jsp.JspWriter ; 37 import javax.servlet.jsp.tagext.BodyTagSupport ; 38 import java.io.IOException ; 39 import java.text.DecimalFormat ; 40 import java.text.DecimalFormatSymbols ; 41 import java.text.NumberFormat ; 42 import java.util.Locale ; 43 44 47 public class FormatNumberTag extends BodyTagSupport { 48 private static L10N L = new L10N(FormatNumberTag.class); 49 50 private double _value; 51 private boolean _hasValue; 52 53 private String _type; 54 55 private String _pattern; 56 57 private String _currencyCode; 58 private String _currencySymbol; 59 60 private boolean _groupingUsed = true; 61 62 private int _maxIntegerDigits = -1; 63 private int _minIntegerDigits = -1; 64 private int _maxFractionDigits = -1; 65 private int _minFractionDigits = -1; 66 67 private String _var; 68 private String _scope; 69 70 75 public void setValue(double value) 76 { 77 _value = value; 78 _hasValue = true; 79 } 80 81 86 public void setType(String type) 87 { 88 _type = type; 89 } 90 91 96 public void setPattern(String pattern) 97 { 98 _pattern = pattern; 99 } 100 101 106 public void setCurrencyCode(String currency) 107 { 108 _currencyCode = currency; 109 } 110 111 116 public void setCurrencySymbol(String currencySymbol) 117 { 118 _currencySymbol = currencySymbol; 119 } 120 121 126 public void setGroupingUsed(boolean groupingUsed) 127 { 128 _groupingUsed = groupingUsed; 129 } 130 131 136 public void setMinIntegerDigits(int minIntegerDigits) 137 { 138 _minIntegerDigits = minIntegerDigits; 139 } 140 141 146 public void setMaxIntegerDigits(int maxIntegerDigits) 147 { 148 _maxIntegerDigits = maxIntegerDigits; 149 } 150 151 156 public void setMinFractionDigits(int minFractionDigits) 157 { 158 _minFractionDigits = minFractionDigits; 159 } 160 161 166 public void setMaxFractionDigits(int maxFractionDigits) 167 { 168 _maxFractionDigits = maxFractionDigits; 169 } 170 171 176 public void setVar(String var) 177 { 178 _var = var; 179 } 180 181 186 public void setScope(String scope) 187 { 188 _scope = scope; 189 } 190 191 194 public int doEndTag() 195 throws JspException 196 { 197 try { 198 PageContextImpl pc = (PageContextImpl) pageContext; 199 200 JspWriter out = pc.getOut(); 201 202 NumberFormat format = getFormat(); 203 204 double rawValue; 205 BodyContentImpl body = (BodyContentImpl) getBodyContent(); 206 207 if (_hasValue) 208 rawValue = _value; 209 else if (body != null) { 210 String value = body.getTrimString(); 211 212 if (! value.equals("")) 213 rawValue = Double.parseDouble(value); 214 else 215 rawValue = 0; 216 } 217 else 218 rawValue = 0; 219 220 if (Double.isNaN(rawValue)) 221 rawValue = 0; 222 223 String value; 224 225 if (format != null) 226 value = format.format(rawValue); 227 else 228 value = String.valueOf(rawValue); 229 230 if (_var == null) 231 out.print(value); 232 else 233 CoreSetTag.setValue(pageContext, _var, _scope, value); 234 } catch (IOException e) { 235 } 236 237 return EVAL_PAGE; 238 } 239 240 protected NumberFormat getFormat() 241 throws JspException 242 { 243 PageContextImpl pc = (PageContextImpl) pageContext; 244 245 NumberFormat format = null; 246 247 Locale locale = pc.getLocale(); 248 249 String type = _type; 250 251 if (type == null || type.equals("") || type.equals("number")) { 252 if (locale != null) 253 format = NumberFormat.getInstance(locale); 254 else 255 format = NumberFormat.getInstance(); 256 257 DecimalFormat decimalFormat = (DecimalFormat ) format; 258 259 if (_pattern != null) 260 decimalFormat.applyPattern(_pattern); 261 } 262 else if (type.equals("percent")) { 263 if (locale != null) 264 format = NumberFormat.getPercentInstance(locale); 265 else 266 format = NumberFormat.getPercentInstance(); 267 } 268 else if (type.equals("currency")) { 269 if (locale != null) 270 format = NumberFormat.getCurrencyInstance(locale); 271 else 272 format = NumberFormat.getCurrencyInstance(); 273 274 if ((_currencyCode != null || _currencySymbol != null) && 275 format instanceof DecimalFormat ) { 276 DecimalFormat dFormat = (DecimalFormat ) format; 277 DecimalFormatSymbols dSymbols; 278 279 dSymbols = dFormat.getDecimalFormatSymbols(); 280 281 if (_currencyCode != null && dSymbols != null) 282 dSymbols.setInternationalCurrencySymbol(_currencyCode); 283 284 if (_currencySymbol != null && dSymbols != null) 285 dSymbols.setCurrencySymbol(_currencySymbol); 286 287 dFormat.setDecimalFormatSymbols(dSymbols); 288 } 289 } 290 else 291 throw new JspException (L.l("unknown formatNumber type `{0}'", 292 type)); 293 294 format.setGroupingUsed(_groupingUsed); 295 296 if (_minIntegerDigits > 0) 297 format.setMinimumIntegerDigits(_minIntegerDigits); 298 299 if (_maxIntegerDigits > 0) 300 format.setMaximumIntegerDigits(_maxIntegerDigits); 301 302 if (_minFractionDigits > 0) 303 format.setMinimumFractionDigits(_minFractionDigits); 304 305 if (_maxFractionDigits > 0) 306 format.setMaximumFractionDigits(_maxFractionDigits); 307 308 return format; 309 } 310 } 311 | Popular Tags |