1 16 17 package org.apache.taglibs.standard.tag.common.fmt; 18 19 import java.io.IOException ; 20 import java.lang.reflect.Method ; 21 import java.text.DecimalFormat ; 22 import java.text.DecimalFormatSymbols ; 23 import java.text.NumberFormat ; 24 import java.util.Locale ; 25 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspTagException ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.tagext.BodyTagSupport ; 30 31 import org.apache.taglibs.standard.resources.Resources; 32 import org.apache.taglibs.standard.tag.common.core.Util; 33 34 40 41 public abstract class FormatNumberSupport extends BodyTagSupport { 42 43 46 private static final Class [] GET_INSTANCE_PARAM_TYPES = 47 new Class [] { String .class }; 48 private static final String NUMBER = "number"; 49 private static final String CURRENCY = "currency"; 50 private static final String PERCENT = "percent"; 51 52 53 56 protected Object value; protected boolean valueSpecified; protected String type; protected String pattern; protected String currencyCode; protected String currencySymbol; protected boolean isGroupingUsed; protected boolean groupingUsedSpecified; 64 protected int maxIntegerDigits; protected boolean maxIntegerDigitsSpecified; 66 protected int minIntegerDigits; protected boolean minIntegerDigitsSpecified; 68 protected int maxFractionDigits; protected boolean maxFractionDigitsSpecified; 70 protected int minFractionDigits; protected boolean minFractionDigitsSpecified; 72 73 74 77 private String var; private int scope; private static Class currencyClass; 80 81 82 85 static { 86 try { 87 currencyClass = Class.forName("java.util.Currency"); 88 } catch (Exception cnfe) { 90 } 91 } 92 93 public FormatNumberSupport() { 94 super(); 95 init(); 96 } 97 98 private void init() { 99 value = type = null; 100 valueSpecified = false; 101 pattern = var = currencyCode = currencySymbol = null; 102 groupingUsedSpecified = false; 103 maxIntegerDigitsSpecified = minIntegerDigitsSpecified = false; 104 maxFractionDigitsSpecified = minFractionDigitsSpecified = false; 105 scope = PageContext.PAGE_SCOPE; 106 } 107 108 109 112 public void setVar(String var) { 113 this.var = var; 114 } 115 116 public void setScope(String scope) { 117 this.scope = Util.getScope(scope); 118 } 119 120 121 124 public int doEndTag() throws JspException { 125 String formatted = null; 126 Object input = null; 127 128 if (valueSpecified) { 130 input = value; 132 } else { 133 if (bodyContent != null && bodyContent.getString() != null) 135 input = bodyContent.getString().trim(); 136 } 137 138 if ((input == null) || input.equals("")) { 139 if (var != null) { 143 pageContext.removeAttribute(var, scope); 144 } 145 return EVAL_PAGE; 146 } 147 148 152 if (input instanceof String ) { 153 try { 154 if (((String ) input).indexOf('.') != -1) { 155 input = Double.valueOf((String ) input); 156 } else { 157 input = Long.valueOf((String ) input); 158 } 159 } catch (NumberFormatException nfe) { 160 throw new JspException ( 161 Resources.getMessage("FORMAT_NUMBER_PARSE_ERROR", input), 162 nfe); 163 } 164 } 165 166 Locale loc = SetLocaleSupport.getFormattingLocale( 168 pageContext, 169 this, 170 true, 171 NumberFormat.getAvailableLocales()); 172 173 if (loc != null) { 174 NumberFormat formatter = null; 176 if ((pattern != null) && !pattern.equals("")) { 177 DecimalFormatSymbols symbols = new DecimalFormatSymbols (loc); 179 formatter = new DecimalFormat (pattern, symbols); 180 } else { 181 formatter = createFormatter(loc); 182 } 183 if (((pattern != null) && !pattern.equals("")) 184 || CURRENCY.equalsIgnoreCase(type)) { 185 try { 186 setCurrency(formatter); 187 } catch (Exception e) { 188 throw new JspException ( 189 Resources.getMessage("FORMAT_NUMBER_CURRENCY_ERROR"), 190 e); 191 } 192 } 193 configureFormatter(formatter); 194 formatted = formatter.format(input); 195 } else { 196 formatted = input.toString(); 198 } 199 200 if (var != null) { 201 pageContext.setAttribute(var, formatted, scope); 202 } else { 203 try { 204 pageContext.getOut().print(formatted); 205 } catch (IOException ioe) { 206 throw new JspTagException (ioe.toString(), ioe); 207 } 208 } 209 210 return EVAL_PAGE; 211 } 212 213 public void release() { 215 init(); 216 } 217 218 219 222 private NumberFormat createFormatter(Locale loc) throws JspException { 223 NumberFormat formatter = null; 224 225 if ((type == null) || NUMBER.equalsIgnoreCase(type)) { 226 formatter = NumberFormat.getNumberInstance(loc); 227 } else if (CURRENCY.equalsIgnoreCase(type)) { 228 formatter = NumberFormat.getCurrencyInstance(loc); 229 } else if (PERCENT.equalsIgnoreCase(type)) { 230 formatter = NumberFormat.getPercentInstance(loc); 231 } else { 232 throw new JspException ( 233 Resources.getMessage("FORMAT_NUMBER_INVALID_TYPE", type)); 234 } 235 236 return formatter; 237 } 238 239 244 private void configureFormatter(NumberFormat formatter) { 245 if (groupingUsedSpecified) 246 formatter.setGroupingUsed(isGroupingUsed); 247 if (maxIntegerDigitsSpecified) 248 formatter.setMaximumIntegerDigits(maxIntegerDigits); 249 if (minIntegerDigitsSpecified) 250 formatter.setMinimumIntegerDigits(minIntegerDigits); 251 if (maxFractionDigitsSpecified) 252 formatter.setMaximumFractionDigits(maxFractionDigits); 253 if (minFractionDigitsSpecified) 254 formatter.setMinimumFractionDigits(minFractionDigits); 255 } 256 257 285 private void setCurrency(NumberFormat formatter) throws Exception { 286 String code = null; 287 String symbol = null; 288 289 if ((currencyCode == null) && (currencySymbol == null)) { 290 return; 291 } 292 293 if ((currencyCode != null) && (currencySymbol != null)) { 294 if (currencyClass != null) 295 code = currencyCode; 296 else 297 symbol = currencySymbol; 298 } else if (currencyCode == null) { 299 symbol = currencySymbol; 300 } else { 301 if (currencyClass != null) 302 code = currencyCode; 303 else 304 symbol = currencyCode; 305 } 306 307 if (code != null) { 308 Object [] methodArgs = new Object [1]; 309 310 313 Method m = currencyClass.getMethod("getInstance", 314 GET_INSTANCE_PARAM_TYPES); 315 methodArgs[0] = code; 316 Object currency = m.invoke(null, methodArgs); 317 318 321 Class [] paramTypes = new Class [1]; 322 paramTypes[0] = currencyClass; 323 Class numberFormatClass = Class.forName("java.text.NumberFormat"); 324 m = numberFormatClass.getMethod("setCurrency", paramTypes); 325 methodArgs[0] = currency; 326 m.invoke(formatter, methodArgs); 327 } else { 328 332 DecimalFormat df = (DecimalFormat ) formatter; 333 DecimalFormatSymbols dfs = df.getDecimalFormatSymbols(); 334 dfs.setCurrencySymbol(symbol); 335 df.setDecimalFormatSymbols(dfs); 336 } 337 } 338 } 339 | Popular Tags |