KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > el > fmt > ParseNumberTag


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.el.fmt;
18
19 import java.util.Locale JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22
23 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
24 import org.apache.taglibs.standard.tag.common.fmt.ParseNumberSupport;
25 import org.apache.taglibs.standard.tag.common.fmt.SetLocaleSupport;
26
27 /**
28  * <p>A handler for &lt;parseNumber&gt; that accepts attributes as Strings
29  * and evaluates them as expressions at runtime.</p>
30  *
31  * @author Jan Luehe
32  */

33
34 public class ParseNumberTag extends ParseNumberSupport {
35
36     //*********************************************************************
37
// 'Private' state (implementation details)
38

39     private String JavaDoc value_; // stores EL-based property
40
private String JavaDoc type_; // stores EL-based property
41
private String JavaDoc pattern_; // stores EL-based property
42
private String JavaDoc parseLocale_; // stores EL-based property
43
private String JavaDoc integerOnly_; // stores EL-based property
44

45
46     //*********************************************************************
47
// Constructor
48

49     /**
50      * Constructs a new ParseNumberTag. As with TagSupport, subclasses
51      * should not provide other constructors and are expected to call
52      * the superclass constructor
53      */

54     public ParseNumberTag() {
55         super();
56         init();
57     }
58
59
60     //*********************************************************************
61
// Tag logic
62

63     // evaluates expression and chains to parent
64
public int doStartTag() throws JspException JavaDoc {
65
66         // evaluate any expressions we were passed, once per invocation
67
evaluateExpressions();
68
69     // chain to the parent implementation
70
return super.doStartTag();
71     }
72
73     // Releases any resources we may have (or inherit)
74
public void release() {
75         super.release();
76         init();
77     }
78
79
80     //*********************************************************************
81
// Accessor methods
82

83     // for EL-based attribute
84
public void setValue(String JavaDoc value_) {
85         this.value_ = value_;
86     this.valueSpecified = true;
87     }
88
89     // for EL-based attribute
90
public void setType(String JavaDoc type_) {
91         this.type_ = type_;
92     }
93
94     // for EL-based attribute
95
public void setPattern(String JavaDoc pattern_) {
96         this.pattern_ = pattern_;
97     }
98
99     // for EL-based attribute
100
public void setParseLocale(String JavaDoc parseLocale_) {
101         this.parseLocale_ = parseLocale_;
102     }
103
104     // for EL-based attribute
105
public void setIntegerOnly(String JavaDoc integerOnly_) {
106         this.integerOnly_ = integerOnly_;
107     this.integerOnlySpecified = true;
108     }
109
110
111     //*********************************************************************
112
// Private (utility) methods
113

114     // (re)initializes state (during release() or construction)
115
private void init() {
116         // null implies "no expression"
117
value_ = type_ = pattern_ = parseLocale_ = integerOnly_ = null;
118     }
119
120     // Evaluates expressions as necessary
121
private void evaluateExpressions() throws JspException JavaDoc {
122     Object JavaDoc obj = null;
123
124         /*
125          * Note: we don't check for type mismatches here; we assume
126          * the expression evaluator will return the expected type
127          * (by virtue of knowledge we give it about what that type is).
128          * A ClassCastException here is truly unexpected, so we let it
129          * propagate up.
130          */

131
132     // 'value' attribute
133
if (value_ != null) {
134         value = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
135             "value", value_, String JavaDoc.class, this, pageContext);
136     }
137
138     // 'type' attribute
139
if (type_ != null) {
140         type = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
141             "type", type_, String JavaDoc.class, this, pageContext);
142     }
143
144     // 'pattern' attribute
145
if (pattern_ != null) {
146         pattern = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
147             "pattern", pattern_, String JavaDoc.class, this, pageContext);
148     }
149
150     // 'parseLocale' attribute
151
if (parseLocale_ != null) {
152         obj = ExpressionEvaluatorManager.evaluate(
153             "parseLocale", parseLocale_, Object JavaDoc.class, this, pageContext);
154         if (obj != null) {
155         if (obj instanceof Locale JavaDoc) {
156             parseLocale = (Locale JavaDoc) obj;
157         } else {
158             String JavaDoc localeStr = (String JavaDoc) obj;
159             if (!"".equals(localeStr)) {
160             parseLocale = SetLocaleSupport.parseLocale(localeStr);
161             }
162         }
163         }
164     }
165
166     // 'integerOnly' attribute
167
if (integerOnly_ != null) {
168         obj = ExpressionEvaluatorManager.evaluate(
169             "integerOnly", integerOnly_, Boolean JavaDoc.class, this, pageContext);
170         if (obj != null) {
171         isIntegerOnly = ((Boolean JavaDoc) obj).booleanValue();
172         }
173     }
174     }
175 }
176
177
Popular Tags