001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package org.apache.taglibs.standard.tag.el.fmt;
040:
041: import java.util.Locale;
042:
043: import javax.servlet.jsp.JspException;
044:
045: import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
046: import org.apache.taglibs.standard.tag.common.fmt.ParseNumberSupport;
047: import org.apache.taglibs.standard.tag.common.fmt.SetLocaleSupport;
048:
049: /**
050: * <p>A handler for <parseNumber> that accepts attributes as Strings
051: * and evaluates them as expressions at runtime.</p>
052: *
053: * @author Jan Luehe
054: */
055:
056: public class ParseNumberTag extends ParseNumberSupport {
057:
058: //*********************************************************************
059: // 'Private' state (implementation details)
060:
061: private String value_; // stores EL-based property
062: private String type_; // stores EL-based property
063: private String pattern_; // stores EL-based property
064: private String parseLocale_; // stores EL-based property
065: private String integerOnly_; // stores EL-based property
066:
067: //*********************************************************************
068: // Constructor
069:
070: /**
071: * Constructs a new ParseNumberTag. As with TagSupport, subclasses
072: * should not provide other constructors and are expected to call
073: * the superclass constructor
074: */
075: public ParseNumberTag() {
076: super ();
077: init();
078: }
079:
080: //*********************************************************************
081: // Tag logic
082:
083: // evaluates expression and chains to parent
084: public int doStartTag() throws JspException {
085:
086: // evaluate any expressions we were passed, once per invocation
087: evaluateExpressions();
088:
089: // chain to the parent implementation
090: return super .doStartTag();
091: }
092:
093: // Releases any resources we may have (or inherit)
094: public void release() {
095: super .release();
096: init();
097: }
098:
099: //*********************************************************************
100: // Accessor methods
101:
102: // for EL-based attribute
103: public void setValue(String value_) {
104: this .value_ = value_;
105: this .valueSpecified = true;
106: }
107:
108: // for EL-based attribute
109: public void setType(String type_) {
110: this .type_ = type_;
111: }
112:
113: // for EL-based attribute
114: public void setPattern(String pattern_) {
115: this .pattern_ = pattern_;
116: }
117:
118: // for EL-based attribute
119: public void setParseLocale(String parseLocale_) {
120: this .parseLocale_ = parseLocale_;
121: }
122:
123: // for EL-based attribute
124: public void setIntegerOnly(String integerOnly_) {
125: this .integerOnly_ = integerOnly_;
126: this .integerOnlySpecified = true;
127: }
128:
129: //*********************************************************************
130: // Private (utility) methods
131:
132: // (re)initializes state (during release() or construction)
133: private void init() {
134: // null implies "no expression"
135: value_ = type_ = pattern_ = parseLocale_ = integerOnly_ = null;
136: }
137:
138: // Evaluates expressions as necessary
139: private void evaluateExpressions() throws JspException {
140: Object obj = null;
141:
142: /*
143: * Note: we don't check for type mismatches here; we assume
144: * the expression evaluator will return the expected type
145: * (by virtue of knowledge we give it about what that type is).
146: * A ClassCastException here is truly unexpected, so we let it
147: * propagate up.
148: */
149:
150: // 'value' attribute
151: if (value_ != null) {
152: value = (String) ExpressionEvaluatorManager.evaluate(
153: "value", value_, String.class, this , pageContext);
154: }
155:
156: // 'type' attribute
157: if (type_ != null) {
158: type = (String) ExpressionEvaluatorManager.evaluate("type",
159: type_, String.class, this , pageContext);
160: }
161:
162: // 'pattern' attribute
163: if (pattern_ != null) {
164: pattern = (String) ExpressionEvaluatorManager.evaluate(
165: "pattern", pattern_, String.class, this ,
166: pageContext);
167: }
168:
169: // 'parseLocale' attribute
170: if (parseLocale_ != null) {
171: obj = ExpressionEvaluatorManager.evaluate("parseLocale",
172: parseLocale_, Object.class, this , pageContext);
173: if (obj != null) {
174: if (obj instanceof Locale) {
175: parseLocale = (Locale) obj;
176: } else {
177: String localeStr = (String) obj;
178: if (!"".equals(localeStr)) {
179: parseLocale = SetLocaleSupport
180: .parseLocale(localeStr);
181: }
182: }
183: }
184: }
185:
186: // 'integerOnly' attribute
187: if (integerOnly_ != null) {
188: obj = ExpressionEvaluatorManager.evaluate("integerOnly",
189: integerOnly_, Boolean.class, this , pageContext);
190: if (obj != null) {
191: isIntegerOnly = ((Boolean) obj).booleanValue();
192: }
193: }
194: }
195: }
|