1 25 package org.ofbiz.webapp.taglib; 26 27 import java.text.DateFormat ; 28 import java.text.NumberFormat ; 29 import javax.servlet.jsp.JspException ; 30 import javax.servlet.jsp.tagext.BodyContent ; 31 import javax.servlet.jsp.tagext.BodyTagSupport ; 32 33 import org.ofbiz.base.util.Debug; 34 import org.ofbiz.base.util.UtilJ2eeCompat; 35 36 43 public class FormatTag extends BodyTagSupport { 44 45 public static final String module = FormatTag.class.getName(); 46 47 private String type = "N"; 48 private String defaultStr = ""; 49 50 public void setType(String type) { 51 this.type = type; 52 } 53 54 public String getType() { 55 return type; 56 } 57 58 public String getDefault() { 59 return defaultStr; 60 } 61 62 public void setDefault(String defaultStr) { 63 this.defaultStr = defaultStr; 64 } 65 66 public int doAfterBody() throws JspException { 67 NumberFormat nf = null; 68 DateFormat df = null; 69 BodyContent body = getBodyContent(); 70 String value = body.getString(); 71 body.clearBody(); 72 73 if (value == null || value.length() == 0) 74 return SKIP_BODY; 75 76 if (type.charAt(0) == 'C' || type.charAt(0) == 'c') 77 nf = NumberFormat.getCurrencyInstance(); 78 if (type.charAt(0) == 'N' || type.charAt(0) == 'n') 79 nf = NumberFormat.getNumberInstance(); 80 if (type.charAt(0) == 'D' || type.charAt(0) == 'd') 81 df = DateFormat.getDateInstance(); 82 83 try { 84 if (nf != null) { 85 NumberFormat strFormat = NumberFormat.getInstance(); 87 88 getPreviousOut().print(nf.format(strFormat.parse(value.trim()))); 89 } else if (df != null) { 90 getPreviousOut().print(df.format(df.parse(value.trim()))); 92 } else { 93 getPreviousOut().print(value); 95 } 96 } catch (Exception e) { 97 if (UtilJ2eeCompat.useNestedJspException(pageContext.getServletContext())) { 98 throw new JspException (e.getMessage(), e); 99 } else { 100 Debug.logError(e, "Server does not support nested exceptions, here is the exception", module); 101 throw new JspException (e.toString()); 102 } 103 } 104 105 return SKIP_BODY; 106 } 107 108 } 109 110 | Popular Tags |