1 28 29 package com.caucho.jstl.rt; 30 31 import com.caucho.jsp.PageContextImpl; 32 import com.caucho.util.L10N; 33 34 import javax.servlet.jsp.JspException ; 35 import javax.servlet.jsp.JspWriter ; 36 import javax.servlet.jsp.tagext.BodyTagSupport ; 37 import java.io.IOException ; 38 import java.text.DecimalFormat ; 39 import java.text.NumberFormat ; 40 import java.text.ParseException ; 41 import java.util.Locale ; 42 43 46 public class FmtParseNumberTag extends BodyTagSupport { 47 private static L10N L = new L10N(FmtParseNumberTag.class); 48 49 private String _value; 50 private String _type; 51 52 private String _pattern; 53 54 private Object _parseLocale; 55 private boolean _integerOnly = false; 56 57 private String _var; 58 private String _scope; 59 60 65 public void setValue(String value) 66 { 67 _value = value; 68 } 69 70 75 public void setType(String type) 76 { 77 _type = type; 78 } 79 80 85 public void setPattern(String pattern) 86 { 87 _pattern = pattern; 88 } 89 90 95 public void setParseLocale(Object locale) 96 { 97 _parseLocale = locale; 98 } 99 100 105 public void setIntegerOnly(boolean integerOnly) 106 { 107 _integerOnly = integerOnly; 108 } 109 110 115 public void setVar(String var) 116 { 117 _var = var; 118 } 119 120 125 public void setScope(String scope) 126 { 127 _scope = scope; 128 } 129 130 133 public int doEndTag() 134 throws JspException 135 { 136 try { 137 PageContextImpl pc = (PageContextImpl) pageContext; 138 139 JspWriter out = pc.getOut(); 140 141 NumberFormat format = getFormat(); 142 143 String string; 144 145 if (_value != null) 146 string = _value; 147 else 148 string = bodyContent.getString().trim(); 149 150 Number value = format.parse(string); 151 152 if (_var == null) 153 out.print(value); 154 else 155 CoreSetTag.setValue(pageContext, _var, _scope, value); 156 } catch (IOException e) { 157 } catch (ParseException e) { 158 throw new JspException (e); 159 } 160 161 return EVAL_PAGE; 162 } 163 164 protected NumberFormat getFormat() 165 throws JspException 166 { 167 PageContextImpl pc = (PageContextImpl) pageContext; 168 169 NumberFormat format = null; 170 171 Locale locale = null; 172 173 if (_parseLocale != null) { 174 Object localeObj = _parseLocale; 175 176 if (localeObj instanceof Locale ) 177 locale = (Locale ) localeObj; 178 else if (localeObj instanceof String ) 179 locale = pc.getLocale((String ) localeObj, null); 180 } 181 182 if (locale == null) 183 locale = pc.getLocale(); 184 185 if (_type == null || _type.equals("") || _type.equals("number")) { 186 if (locale != null) 187 format = NumberFormat.getInstance(locale); 188 else 189 format = NumberFormat.getInstance(); 190 191 DecimalFormat decimalFormat = (DecimalFormat ) format; 192 193 if (_pattern != null) 194 decimalFormat.applyPattern(_pattern); 195 } 196 else if (_type.equals("percent")) { 197 if (locale != null) 198 format = NumberFormat.getPercentInstance(locale); 199 else 200 format = NumberFormat.getPercentInstance(); 201 } 202 else if (_type.equals("currency")) { 203 if (locale != null) 204 format = NumberFormat.getCurrencyInstance(locale); 205 else 206 format = NumberFormat.getCurrencyInstance(locale); 207 } 208 else 209 throw new JspException (L.l("unknown formatNumber type `{0}'", _type)); 210 211 format.setParseIntegerOnly(_integerOnly); 212 213 return format; 214 } 215 } 216 | Popular Tags |