KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > dbtags > resultset > GetNumberTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.taglibs.dbtags.resultset;
17
18 import java.io.IOException JavaDoc;
19 import java.sql.ResultSet JavaDoc;
20 import java.sql.ResultSetMetaData JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Types JavaDoc;
23 import java.text.DecimalFormat JavaDoc;
24 import java.text.DecimalFormatSymbols JavaDoc;
25 import java.text.NumberFormat JavaDoc;
26 import java.util.Locale JavaDoc;
27
28 import javax.servlet.jsp.JspTagException JavaDoc;
29
30 /**
31  * Translates any SQL number to a readable String.
32  *
33  * <p>JSP Tag Lib Descriptor
34  * <pre>
35  * &LT;tag>
36  * &LT;name>getNumber&LT;/name>
37  * &LT;tagclass>org.apache.taglibs.dbtags.resultset.GetNumberTag&LT;/tagclass>
38  * &LT;teiclass>org.apache.taglibs.dbtags.resultset.BaseGetterTEI&LT;/teiclass>
39  * &LT;bodycontent>empty&LT;/bodycontent>
40  * &LT;info>
41  * Similar to getColumn, but provides more precise control over
42  * number formatting.
43  *
44  * The "format" attribute can be either a pattern as
45  * accepted by the DecimalFormat constructor or a style: "CURRENCY",
46  * "PERCENT" or "NUMBER".
47  *
48  * The "locale" attribute can have one to three
49  * components as accepted by the Locale constructor: language,
50  * country and variant. They are separated by "_".
51  *
52  * If neither the format nor locale attribute is set, output should be
53  * identical to getColumn.
54  * &LT;/info>
55  * &LT;attribute>
56  * &LT;name>position&LT;/name>
57  * &LT;required>false&LT;/required>
58  * &LT;rtexprvalue>false&LT;/rtexprvalue>
59  * &LT;/attribute>
60  * &LT;attribute>
61  * &LT;name>colName&LT;/name>
62  * &LT;required>false&LT;/required>
63  * &LT;rtexprvalue>false&LT;/rtexprvalue>
64  * &LT;/attribute>
65  * &LT;attribute>
66  * &LT;name>to&LT;/name>
67  * &LT;required>false&LT;/required>
68  * &LT;rtexprvalue>false&LT;/rtexprvalue>
69  * &LT;/attribute>
70  * &LT;attribute>
71  * &LT;name>scope&LT;/name>
72  * &LT;required>false&LT;/required>
73  * &LT;rtexprvalue>false&LT;/rtexprvalue>
74  * &LT;/attribute>
75  * &LT;attribute>
76  * &LT;name>locale&LT;/name>
77  * &LT;required>false&LT;/required>
78  * &LT;rtexprvalue>true&LT;/rtexprvalue>
79  * &LT;/attribute>
80  * &LT;attribute>
81  * &LT;name>format&LT;/name>
82  * &LT;required>false&LT;/required>
83  * &LT;rtexprvalue>true&LT;/rtexprvalue>
84  * &LT;/attribute>
85  * &LT;/tag>
86  * </pre>
87  *
88  * @author Morgan Delagrange
89  * @author Marius Scurtescu
90  */

91
92 public class GetNumberTag extends BaseGetterTag {
93
94   protected String JavaDoc _format = null;
95
96   public void setFormat (String JavaDoc strFormat) {
97     _format = strFormat;
98   }
99
100   public int doStartTag() throws JspTagException JavaDoc {
101     try {
102       ResultSet JavaDoc rset = getResultSet();
103
104       // some complex datatypes, such as clobs,
105
// require special handling
106
ResultSetMetaData JavaDoc meta = getMetaData();
107
108       int position = getPosition();
109
110       String JavaDoc 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 JavaDoc 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 JavaDoc fmt = getNumberFormat (_format, getLocale (_locale));
143               string = fmt.format (l);
144             }
145           }
146           break;
147         }
148       default:
149         throw new JspTagException JavaDoc("Column is not a recognized number type");
150       }
151
152       // null results are often OK, in outer joins for example
153
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 JavaDoc e) {
163       throw new JspTagException JavaDoc(e.toString());
164     } catch (IOException JavaDoc e) {
165       throw new JspTagException JavaDoc(e.toString());
166     }
167
168     return EVAL_BODY_INCLUDE;
169   }
170
171   private static NumberFormat JavaDoc getNumberFormat (String JavaDoc strFormat, Locale JavaDoc loc)
172   {
173     NumberFormat JavaDoc 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 JavaDoc (strFormat, new DecimalFormatSymbols JavaDoc(loc));
183
184     return fmt;
185   }
186
187   public void release() {
188     super.release();
189     _format = null;
190   }
191 }
192
Popular Tags