1 16 17 package org.apache.taglibs.standard.tag.common.fmt; 18 19 import java.io.IOException ; 20 import java.text.DecimalFormat ; 21 import java.text.DecimalFormatSymbols ; 22 import java.text.NumberFormat ; 23 import java.text.ParseException ; 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 ParseNumberSupport extends BodyTagSupport { 42 43 46 private static final String NUMBER = "number"; 47 private static final String CURRENCY = "currency"; 48 private static final String PERCENT = "percent"; 49 50 51 54 protected String value; protected boolean valueSpecified; protected String type; protected String pattern; protected Locale parseLocale; protected boolean isIntegerOnly; protected boolean integerOnlySpecified; 61 62 63 66 private String var; private int scope; 69 70 73 public ParseNumberSupport() { 74 super(); 75 init(); 76 } 77 78 private void init() { 79 value = type = pattern = var = null; 80 valueSpecified = false; 81 parseLocale = null; 82 integerOnlySpecified = false; 83 scope = PageContext.PAGE_SCOPE; 84 } 85 86 87 90 public void setVar(String var) { 91 this.var = var; 92 } 93 94 public void setScope(String scope) { 95 this.scope = Util.getScope(scope); 96 } 97 98 99 102 public int doEndTag() throws JspException { 103 String input = null; 104 105 if (valueSpecified) { 107 input = value; 109 } else { 110 if (bodyContent != null && bodyContent.getString() != null) 112 input = bodyContent.getString().trim(); 113 } 114 115 if ((input == null) || input.equals("")) { 116 if (var != null) { 117 pageContext.removeAttribute(var, scope); 118 } 119 return EVAL_PAGE; 120 } 121 122 126 Locale loc = parseLocale; 127 if (loc == null) 128 loc = SetLocaleSupport.getFormattingLocale( 129 pageContext, 130 this, 131 false, 132 NumberFormat.getAvailableLocales()); 133 if (loc == null) { 134 throw new JspException ( 135 Resources.getMessage("PARSE_NUMBER_NO_PARSE_LOCALE")); 136 } 137 138 NumberFormat parser = null; 140 if ((pattern != null) && !pattern.equals("")) { 141 DecimalFormatSymbols symbols = new DecimalFormatSymbols (loc); 143 parser = new DecimalFormat (pattern, symbols); 144 } else { 145 parser = createParser(loc); 146 } 147 148 if (integerOnlySpecified) 150 parser.setParseIntegerOnly(isIntegerOnly); 151 152 Number parsed = null; 154 try { 155 parsed = parser.parse(input); 156 } catch (ParseException pe) { 157 throw new JspException ( 158 Resources.getMessage("PARSE_NUMBER_PARSE_ERROR", input), 159 pe); 160 } 161 162 if (var != null) { 163 pageContext.setAttribute(var, parsed, scope); 164 } else { 165 try { 166 pageContext.getOut().print(parsed); 167 } catch (IOException ioe) { 168 throw new JspTagException (ioe.toString(), ioe); 169 } 170 } 171 172 return EVAL_PAGE; 173 } 174 175 public void release() { 177 init(); 178 } 179 180 181 184 private NumberFormat createParser(Locale loc) throws JspException { 185 NumberFormat parser = null; 186 187 if ((type == null) || NUMBER.equalsIgnoreCase(type)) { 188 parser = NumberFormat.getNumberInstance(loc); 189 } else if (CURRENCY.equalsIgnoreCase(type)) { 190 parser = NumberFormat.getCurrencyInstance(loc); 191 } else if (PERCENT.equalsIgnoreCase(type)) { 192 parser = NumberFormat.getPercentInstance(loc); 193 } else { 194 throw new JspException ( 195 Resources.getMessage("PARSE_NUMBER_INVALID_TYPE", 196 type)); 197 } 198 199 return parser; 200 } 201 } 202 | Popular Tags |