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.DateFormat;
043: import java.text.ParseException;
044: import java.text.SimpleDateFormat;
045: import java.util.Date;
046: import java.util.Locale;
047: import java.util.TimeZone;
048:
049: import javax.servlet.jsp.JspException;
050: import javax.servlet.jsp.JspTagException;
051: import javax.servlet.jsp.PageContext;
052: import javax.servlet.jsp.tagext.BodyTagSupport;
053:
054: import org.apache.taglibs.standard.resources.Resources;
055: import org.apache.taglibs.standard.tag.common.core.Util;
056:
057: /**
058: * Support for tag handlers for <parseDate>, the date and time
059: * parsing tag in JSTL 1.0.
060: *
061: * @author Jan Luehe
062: */
063:
064: public abstract class ParseDateSupport extends BodyTagSupport {
065:
066: //*********************************************************************
067: // Private constants
068:
069: private static final String DATE = "date";
070: private static final String TIME = "time";
071: private static final String DATETIME = "both";
072:
073: //*********************************************************************
074: // Protected state
075:
076: protected String value; // 'value' attribute
077: protected boolean valueSpecified; // status
078: protected String type; // 'type' attribute
079: protected String pattern; // 'pattern' attribute
080: protected Object timeZone; // 'timeZone' attribute
081: protected Locale parseLocale; // 'parseLocale' attribute
082: protected String dateStyle; // 'dateStyle' attribute
083: protected String timeStyle; // 'timeStyle' attribute
084:
085: //*********************************************************************
086: // Private state
087:
088: private String var; // 'var' attribute
089: private int scope; // 'scope' attribute
090:
091: //*********************************************************************
092: // Constructor and initialization
093:
094: public ParseDateSupport() {
095: super ();
096: init();
097: }
098:
099: private void init() {
100: type = dateStyle = timeStyle = null;
101: value = pattern = var = null;
102: valueSpecified = false;
103: timeZone = null;
104: scope = PageContext.PAGE_SCOPE;
105: parseLocale = null;
106: }
107:
108: //*********************************************************************
109: // Tag attributes known at translation time
110:
111: public void setVar(String var) {
112: this .var = var;
113: }
114:
115: public void setScope(String scope) {
116: this .scope = Util.getScope(scope);
117: }
118:
119: //*********************************************************************
120: // Tag logic
121:
122: public int doEndTag() throws JspException {
123:
124: String input = null;
125:
126: // determine the input by...
127: if (valueSpecified) {
128: // ... reading 'value' attribute
129: input = value;
130: } else {
131: // ... retrieving and trimming our body
132: if (bodyContent != null && bodyContent.getString() != null)
133: input = bodyContent.getString().trim();
134: }
135:
136: if ((input == null) || input.equals("")) {
137: if (var != null) {
138: pageContext.removeAttribute(var, scope);
139: }
140: return EVAL_PAGE;
141: }
142:
143: /*
144: * Set up parsing locale: Use locale specified via the 'parseLocale'
145: * attribute (if present), or else determine page's locale.
146: */
147: Locale locale = parseLocale;
148: if (locale == null)
149: locale = SetLocaleSupport.getFormattingLocale(pageContext,
150: this , true, false);
151: if (locale == null) {
152: throw new JspException(Resources
153: .getMessage("PARSE_DATE_NO_PARSE_LOCALE"));
154: }
155:
156: // Create parser
157: DateFormat parser = createParser(locale);
158:
159: // Apply pattern, if present
160: if (pattern != null) {
161: try {
162: ((SimpleDateFormat) parser).applyPattern(pattern);
163: } catch (ClassCastException cce) {
164: parser = new SimpleDateFormat(pattern, locale);
165: }
166: }
167:
168: // Set time zone
169: TimeZone tz = null;
170: if ((timeZone instanceof String)
171: && ((String) timeZone).equals("")) {
172: timeZone = null;
173: }
174: if (timeZone != null) {
175: if (timeZone instanceof String) {
176: tz = TimeZone.getTimeZone((String) timeZone);
177: } else if (timeZone instanceof TimeZone) {
178: tz = (TimeZone) timeZone;
179: } else {
180: throw new JspException(Resources
181: .getMessage("PARSE_DATE_BAD_TIMEZONE"));
182: }
183: } else {
184: tz = TimeZoneSupport.getTimeZone(pageContext, this );
185: }
186: if (tz != null) {
187: parser.setTimeZone(tz);
188: }
189:
190: // Parse date
191: Date parsed = null;
192: try {
193: parsed = parser.parse(input);
194: } catch (ParseException pe) {
195: throw new JspException(Resources.getMessage(
196: "PARSE_DATE_PARSE_ERROR", input), pe);
197: }
198:
199: if (var != null) {
200: pageContext.setAttribute(var, parsed, scope);
201: } else {
202: try {
203: pageContext.getOut().print(parsed);
204: } catch (IOException ioe) {
205: throw new JspTagException(ioe.toString(), ioe);
206: }
207: }
208:
209: return EVAL_PAGE;
210: }
211:
212: // Releases any resources we may have (or inherit)
213: public void release() {
214: init();
215: }
216:
217: //*********************************************************************
218: // Private utility methods
219:
220: private DateFormat createParser(Locale loc) throws JspException {
221: DateFormat parser = null;
222:
223: if ((type == null) || DATE.equalsIgnoreCase(type)) {
224: parser = DateFormat.getDateInstance(Util.getStyle(
225: dateStyle, "PARSE_DATE_INVALID_DATE_STYLE"), loc);
226: } else if (TIME.equalsIgnoreCase(type)) {
227: parser = DateFormat.getTimeInstance(Util.getStyle(
228: timeStyle, "PARSE_DATE_INVALID_TIME_STYLE"), loc);
229: } else if (DATETIME.equalsIgnoreCase(type)) {
230: parser = DateFormat.getDateTimeInstance(Util.getStyle(
231: dateStyle, "PARSE_DATE_INVALID_DATE_STYLE"), Util
232: .getStyle(timeStyle,
233: "PARSE_DATE_INVALID_TIME_STYLE"), loc);
234: } else {
235: throw new JspException(Resources.getMessage(
236: "PARSE_DATE_INVALID_TYPE", type));
237: }
238:
239: parser.setLenient(false);
240:
241: return parser;
242: }
243: }
|