1 16 package org.apache.taglibs.dbtags.resultset; 17 18 import java.io.IOException ; 19 import java.sql.ResultSet ; 20 import java.sql.ResultSetMetaData ; 21 import java.sql.SQLException ; 22 import java.sql.Types ; 23 import java.text.DecimalFormat ; 24 import java.text.DecimalFormatSymbols ; 25 import java.text.NumberFormat ; 26 import java.util.Locale ; 27 28 import javax.servlet.jsp.JspTagException ; 29 30 91 92 public class GetNumberTag extends BaseGetterTag { 93 94 protected String _format = null; 95 96 public void setFormat (String strFormat) { 97 _format = strFormat; 98 } 99 100 public int doStartTag() throws JspTagException { 101 try { 102 ResultSet rset = getResultSet(); 103 104 ResultSetMetaData meta = getMetaData(); 107 108 int position = getPosition(); 109 110 String string = null; 111 switch (meta.getColumnType(position)) { 112 case Types.REAL: 113 case Types.FLOAT: 114 case Types.DOUBLE: 115 case Types.DECIMAL: 116 case Types.NUMERIC: 117 { 118 double d = rset.getDouble (position); 119 120 if (rset.wasNull() == false) { 121 122 if (_format == null) { 123 string = Double.toString (d); 124 } else { 125 NumberFormat fmt = getNumberFormat (_format, getLocale (_locale)); 126 string = fmt.format (d); 127 } 128 } 129 break; 130 } 131 case Types.TINYINT: 132 case Types.SMALLINT: 133 case Types.INTEGER: 134 case Types.BIGINT: 135 { 136 long l = rset.getLong (position); 137 138 if (rset.wasNull() == false) { 139 if (_format == null) { 140 string = Long.toString (l); 141 } else { 142 NumberFormat fmt = getNumberFormat (_format, getLocale (_locale)); 143 string = fmt.format (l); 144 } 145 } 146 break; 147 } 148 default: 149 throw new JspTagException ("Column is not a recognized number type"); 150 } 151 152 if (string == null) { 154 return EVAL_BODY_INCLUDE; 155 } 156 157 if (_attributeName != null) { 158 setAttribute(_attributeName, string, _scope); 159 } else { 160 pageContext.getOut().write(string); 161 } 162 } catch (SQLException e) { 163 throw new JspTagException (e.toString()); 164 } catch (IOException e) { 165 throw new JspTagException (e.toString()); 166 } 167 168 return EVAL_BODY_INCLUDE; 169 } 170 171 private static NumberFormat getNumberFormat (String strFormat, Locale loc) 172 { 173 NumberFormat fmt; 174 175 if (strFormat.equals ("CURRENCY")) 176 fmt = NumberFormat.getCurrencyInstance (loc); 177 else if (strFormat.equals ("PERCENT")) 178 fmt = NumberFormat.getPercentInstance (loc); 179 else if (strFormat.equals ("NUMBER")) 180 fmt = NumberFormat.getNumberInstance (loc); 181 else 182 fmt = new DecimalFormat (strFormat, new DecimalFormatSymbols (loc)); 183 184 return fmt; 185 } 186 187 public void release() { 188 super.release(); 189 _format = null; 190 } 191 } 192 | Popular Tags |