KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > fmt > ParseNumberSupport


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
17 package org.apache.taglibs.standard.tag.common.fmt;
18
19 import java.io.IOException JavaDoc;
20 import java.text.DecimalFormat JavaDoc;
21 import java.text.DecimalFormatSymbols JavaDoc;
22 import java.text.NumberFormat JavaDoc;
23 import java.text.ParseException JavaDoc;
24 import java.util.Locale JavaDoc;
25
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.JspTagException JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
30
31 import org.apache.taglibs.standard.resources.Resources;
32 import org.apache.taglibs.standard.tag.common.core.Util;
33
34 /**
35  * Support for tag handlers for <parseNumber>, the number parsing tag
36  * in JSTL 1.0.
37  *
38  * @author Jan Luehe
39  */

40
41 public abstract class ParseNumberSupport extends BodyTagSupport JavaDoc {
42
43     //*********************************************************************
44
// Private constants
45

46     private static final String JavaDoc NUMBER = "number";
47     private static final String JavaDoc CURRENCY = "currency";
48     private static final String JavaDoc PERCENT = "percent";
49
50
51     //*********************************************************************
52
// Protected state
53

54     protected String JavaDoc value; // 'value' attribute
55
protected boolean valueSpecified; // status
56
protected String JavaDoc type; // 'type' attribute
57
protected String JavaDoc pattern; // 'pattern' attribute
58
protected Locale JavaDoc parseLocale; // 'parseLocale' attribute
59
protected boolean isIntegerOnly; // 'integerOnly' attribute
60
protected boolean integerOnlySpecified;
61
62
63     //*********************************************************************
64
// Private state
65

66     private String JavaDoc var; // 'var' attribute
67
private int scope; // 'scope' attribute
68

69
70     //*********************************************************************
71
// Constructor and initialization
72

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    //*********************************************************************
88
// Tag attributes known at translation time
89

90     public void setVar(String JavaDoc var) {
91         this.var = var;
92     }
93
94     public void setScope(String JavaDoc scope) {
95     this.scope = Util.getScope(scope);
96     }
97
98
99     //*********************************************************************
100
// Tag logic
101

102     public int doEndTag() throws JspException JavaDoc {
103     String JavaDoc input = null;
104
105         // determine the input by...
106
if (valueSpecified) {
107         // ... reading 'value' attribute
108
input = value;
109     } else {
110         // ... retrieving and trimming our body
111
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     /*
123      * Set up parsing locale: Use locale specified via the 'parseLocale'
124      * attribute (if present), or else determine page's locale.
125      */

126     Locale JavaDoc 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 JavaDoc(
135                     Resources.getMessage("PARSE_NUMBER_NO_PARSE_LOCALE"));
136     }
137
138     // Create parser
139
NumberFormat JavaDoc parser = null;
140     if ((pattern != null) && !pattern.equals("")) {
141         // if 'pattern' is specified, 'type' is ignored
142
DecimalFormatSymbols JavaDoc symbols = new DecimalFormatSymbols JavaDoc(loc);
143         parser = new DecimalFormat JavaDoc(pattern, symbols);
144     } else {
145         parser = createParser(loc);
146     }
147
148     // Configure parser
149
if (integerOnlySpecified)
150         parser.setParseIntegerOnly(isIntegerOnly);
151
152     // Parse number
153
Number JavaDoc parsed = null;
154     try {
155         parsed = parser.parse(input);
156     } catch (ParseException JavaDoc pe) {
157         throw new JspException JavaDoc(
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 JavaDoc ioe) {
168         throw new JspTagException JavaDoc(ioe.toString(), ioe);
169         }
170     }
171
172     return EVAL_PAGE;
173     }
174
175     // Releases any resources we may have (or inherit)
176
public void release() {
177     init();
178     }
179
180
181     //*********************************************************************
182
// Private utility methods
183

184     private NumberFormat JavaDoc createParser(Locale JavaDoc loc) throws JspException JavaDoc {
185     NumberFormat JavaDoc 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 JavaDoc(
195                     Resources.getMessage("PARSE_NUMBER_INVALID_TYPE",
196                      type));
197     }
198
199     return parser;
200     }
201 }
202
Popular Tags