1 18 package org.apache.beehive.netui.tags.html; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import org.apache.beehive.netui.util.Bundle; 23 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.tagext.JspTag ; 26 import javax.servlet.jsp.tagext.SimpleTagSupport ; 27 import java.text.DecimalFormat ; 28 import java.util.Locale ; 29 30 99 public class FormatNumber extends FormatTag 100 { 101 104 protected String _type; 105 106 109 public String getTagName() 110 { 111 return "FormatNumber"; 112 } 113 114 123 public void setType(String type) 124 throws JspException 125 { 126 _type = setRequiredValueAttribute(type, "type"); 127 if (_type != null) { 128 if (!type.equals("number") && !type.equals("currency") && !type.equals("percent")) { 129 String s = Bundle.getString("Tags_NumberFormatWrongType"); 130 registerTagError(s, null); 131 } 132 } 133 } 134 135 139 public void doTag() 140 throws JspException 141 { 142 JspTag parentTag = SimpleTagSupport.findAncestorWithClass(this, IFormattable.class); 143 144 if (hasErrors()) { 146 if (parentTag instanceof IFormattable) { 147 IFormattable parent = (IFormattable) parentTag; 148 parent.formatterHasError(); 149 } 150 reportErrors(); 151 return; 152 } 153 154 if (parentTag instanceof IFormattable) { 156 NumberFormatter formatter = new NumberFormatter(); 157 formatter.setPattern(_pattern); 158 formatter.setType(_type); 159 formatter.setLocale(getLocale()); 160 IFormattable parent = (IFormattable) parentTag; 161 parent.addFormatter(formatter); 162 } 163 else { 164 String s = Bundle.getString("Tags_FormattableParentRequired"); 165 registerTagError(s, null); 166 reportErrors(); 167 } 168 } 169 170 173 public static class NumberFormatter extends FormatTag.Formatter 174 { 175 private String type; 176 private Locale locale; 177 178 public void setType(String type) 179 { 180 this.type = type; 181 } 182 183 public void setLocale(Locale locale) 184 { 185 this.locale = locale; 186 } 187 188 public String format(Object dataToFormat) throws JspException 189 { 190 if (dataToFormat == null) { 191 return null; 192 } 193 InternalStringBuilder formattedString = new InternalStringBuilder(32); 194 DecimalFormat numberFormat = null; 195 196 if (locale == null) { 198 if ((type == null) || (type.equals("number"))) { 199 numberFormat = (DecimalFormat ) java.text.NumberFormat.getNumberInstance(); 200 } 201 else if (type.equals("currency")) { 202 numberFormat = (DecimalFormat ) java.text.NumberFormat.getCurrencyInstance(); 203 } 204 else if (type.equals("percent")) { 205 numberFormat = (DecimalFormat ) java.text.NumberFormat.getPercentInstance(); 206 } 207 else { 208 assert(false) : "Invalid type was found:" + type; 209 } 210 } 211 else { 212 if ((type == null) || (type.equals("number"))) { 213 numberFormat = (DecimalFormat ) java.text.NumberFormat.getNumberInstance(locale); 214 } 215 else if (type.equals("currency")) { 216 numberFormat = (DecimalFormat ) java.text.NumberFormat.getCurrencyInstance(locale); 217 } 218 else if (type.equals("percent")) { 219 numberFormat = (DecimalFormat ) java.text.NumberFormat.getPercentInstance(locale); 220 } 221 else { 222 assert(false) : "Invalid type was found:" + type; 223 } 224 } 225 226 try { 228 if (getPattern() != null) 229 numberFormat.applyPattern(getPattern()); 230 } 231 catch (Exception e) { 232 throw new JspException (Bundle.getString("Tags_NumberFormatPatternException", e.getMessage()), e); 233 } 234 235 if (dataToFormat.toString().length() == 0) { 237 return ""; 238 } 239 try { 240 double number = Double.parseDouble(dataToFormat.toString()); 241 formattedString.append(numberFormat.format(number)); 242 } 243 catch (Exception e) { 244 throw new JspException (Bundle.getString("Tags_NumberFormatParseException", e.getMessage()), e); 245 } 246 247 return formattedString.toString(); 248 249 } 250 } 251 } 252 | Popular Tags |