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.common.fmt;
040:
041: import java.io.IOException;
042: import java.text.DecimalFormat;
043: import java.text.DecimalFormatSymbols;
044: import java.text.NumberFormat;
045: import java.text.ParseException;
046: import java.util.Locale;
047:
048: import javax.servlet.jsp.JspException;
049: import javax.servlet.jsp.JspTagException;
050: import javax.servlet.jsp.PageContext;
051: import javax.servlet.jsp.tagext.BodyTagSupport;
052:
053: import org.apache.taglibs.standard.resources.Resources;
054: import org.apache.taglibs.standard.tag.common.core.Util;
055:
056: /**
057: * Support for tag handlers for <parseNumber>, the number parsing tag
058: * in JSTL 1.0.
059: *
060: * @author Jan Luehe
061: */
062:
063: public abstract class ParseNumberSupport extends BodyTagSupport {
064:
065: //*********************************************************************
066: // Private constants
067:
068: private static final String NUMBER = "number";
069: private static final String CURRENCY = "currency";
070: private static final String PERCENT = "percent";
071:
072: //*********************************************************************
073: // Protected state
074:
075: protected String value; // 'value' attribute
076: protected boolean valueSpecified; // status
077: protected String type; // 'type' attribute
078: protected String pattern; // 'pattern' attribute
079: protected Locale parseLocale; // 'parseLocale' attribute
080: protected boolean isIntegerOnly; // 'integerOnly' attribute
081: protected boolean integerOnlySpecified;
082:
083: //*********************************************************************
084: // Private state
085:
086: private String var; // 'var' attribute
087: private int scope; // 'scope' attribute
088:
089: //*********************************************************************
090: // Constructor and initialization
091:
092: public ParseNumberSupport() {
093: super ();
094: init();
095: }
096:
097: private void init() {
098: value = type = pattern = var = null;
099: valueSpecified = false;
100: parseLocale = null;
101: integerOnlySpecified = false;
102: scope = PageContext.PAGE_SCOPE;
103: }
104:
105: //*********************************************************************
106: // Tag attributes known at translation time
107:
108: public void setVar(String var) {
109: this .var = var;
110: }
111:
112: public void setScope(String scope) {
113: this .scope = Util.getScope(scope);
114: }
115:
116: //*********************************************************************
117: // Tag logic
118:
119: public int doEndTag() throws JspException {
120: String input = null;
121:
122: // determine the input by...
123: if (valueSpecified) {
124: // ... reading 'value' attribute
125: input = value;
126: } else {
127: // ... retrieving and trimming our body
128: if (bodyContent != null && bodyContent.getString() != null)
129: input = bodyContent.getString().trim();
130: }
131:
132: if ((input == null) || input.equals("")) {
133: if (var != null) {
134: pageContext.removeAttribute(var, scope);
135: }
136: return EVAL_PAGE;
137: }
138:
139: /*
140: * Set up parsing locale: Use locale specified via the 'parseLocale'
141: * attribute (if present), or else determine page's locale.
142: */
143: Locale loc = parseLocale;
144: if (loc == null)
145: loc = SetLocaleSupport.getFormattingLocale(pageContext,
146: this , false, false);
147: if (loc == null) {
148: throw new JspException(Resources
149: .getMessage("PARSE_NUMBER_NO_PARSE_LOCALE"));
150: }
151:
152: // Create parser
153: NumberFormat parser = null;
154: if ((pattern != null) && !pattern.equals("")) {
155: // if 'pattern' is specified, 'type' is ignored
156: DecimalFormatSymbols symbols = new DecimalFormatSymbols(loc);
157: parser = new DecimalFormat(pattern, symbols);
158: } else {
159: parser = createParser(loc);
160: }
161:
162: // Configure parser
163: if (integerOnlySpecified)
164: parser.setParseIntegerOnly(isIntegerOnly);
165:
166: // Parse number
167: Number parsed = null;
168: try {
169: parsed = parser.parse(input);
170: } catch (ParseException pe) {
171: throw new JspException(Resources.getMessage(
172: "PARSE_NUMBER_PARSE_ERROR", input), pe);
173: }
174:
175: if (var != null) {
176: pageContext.setAttribute(var, parsed, scope);
177: } else {
178: try {
179: pageContext.getOut().print(parsed);
180: } catch (IOException ioe) {
181: throw new JspTagException(ioe.toString(), ioe);
182: }
183: }
184:
185: return EVAL_PAGE;
186: }
187:
188: // Releases any resources we may have (or inherit)
189: public void release() {
190: init();
191: }
192:
193: //*********************************************************************
194: // Private utility methods
195:
196: private NumberFormat createParser(Locale loc) throws JspException {
197: NumberFormat parser = null;
198:
199: if ((type == null) || NUMBER.equalsIgnoreCase(type)) {
200: parser = NumberFormat.getNumberInstance(loc);
201: } else if (CURRENCY.equalsIgnoreCase(type)) {
202: parser = NumberFormat.getCurrencyInstance(loc);
203: } else if (PERCENT.equalsIgnoreCase(type)) {
204: parser = NumberFormat.getPercentInstance(loc);
205: } else {
206: throw new JspException(Resources.getMessage(
207: "PARSE_NUMBER_INVALID_TYPE", type));
208: }
209:
210: return parser;
211: }
212: }
|