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.SimpleDateFormat;
044: import java.util.Date;
045: import java.util.Locale;
046: import java.util.TimeZone;
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.TagSupport;
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 <formatDate>, the date and time
058: * formatting tag in JSTL 1.0.
059: *
060: * @author Jan Luehe
061: */
062:
063: public abstract class FormatDateSupport extends TagSupport {
064:
065: //*********************************************************************
066: // Private constants
067:
068: private static final String DATE = "date";
069: private static final String TIME = "time";
070: private static final String DATETIME = "both";
071:
072: //*********************************************************************
073: // Protected state
074:
075: protected Date value; // 'value' attribute
076: protected String type; // 'type' attribute
077: protected String pattern; // 'pattern' attribute
078: protected Object timeZone; // 'timeZone' attribute
079: protected String dateStyle; // 'dateStyle' attribute
080: protected String timeStyle; // 'timeStyle' attribute
081:
082: //*********************************************************************
083: // Private state
084:
085: private String var; // 'var' attribute
086: private int scope; // 'scope' attribute
087:
088: //*********************************************************************
089: // Constructor and initialization
090:
091: public FormatDateSupport() {
092: super ();
093: init();
094: }
095:
096: private void init() {
097: type = dateStyle = timeStyle = null;
098: pattern = var = null;
099: value = null;
100: timeZone = null;
101: scope = PageContext.PAGE_SCOPE;
102: }
103:
104: //*********************************************************************
105: // Tag attributes known at translation time
106:
107: public void setVar(String var) {
108: this .var = var;
109: }
110:
111: public void setScope(String scope) {
112: this .scope = Util.getScope(scope);
113: }
114:
115: //*********************************************************************
116: // Tag logic
117:
118: /*
119: * Formats the given date and time.
120: */
121: public int doEndTag() throws JspException {
122:
123: String formatted = null;
124:
125: if (value == null) {
126: if (var != null) {
127: pageContext.removeAttribute(var, scope);
128: }
129: return EVAL_PAGE;
130: }
131:
132: // Create formatter
133: Locale locale = SetLocaleSupport.getFormattingLocale(
134: pageContext, this , true, true);
135: if (locale != null) {
136: DateFormat formatter = createFormatter(locale);
137:
138: // Apply pattern, if present
139: if (pattern != null) {
140: try {
141: ((SimpleDateFormat) formatter)
142: .applyPattern(pattern);
143: } catch (ClassCastException cce) {
144: formatter = new SimpleDateFormat(pattern, locale);
145: }
146: }
147:
148: // Set time zone
149: TimeZone tz = null;
150: if ((timeZone instanceof String)
151: && ((String) timeZone).equals("")) {
152: timeZone = null;
153: }
154: if (timeZone != null) {
155: if (timeZone instanceof String) {
156: tz = TimeZone.getTimeZone((String) timeZone);
157: } else if (timeZone instanceof TimeZone) {
158: tz = (TimeZone) timeZone;
159: } else {
160: throw new JspTagException(Resources
161: .getMessage("FORMAT_DATE_BAD_TIMEZONE"));
162: }
163: } else {
164: tz = TimeZoneSupport.getTimeZone(pageContext, this );
165: }
166: if (tz != null) {
167: formatter.setTimeZone(tz);
168: }
169: formatted = formatter.format(value);
170: } else {
171: // no formatting locale available, use Date.toString()
172: formatted = value.toString();
173: }
174:
175: if (var != null) {
176: pageContext.setAttribute(var, formatted, scope);
177: } else {
178: try {
179: pageContext.getOut().print(formatted);
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 DateFormat createFormatter(Locale loc) throws JspException {
197: DateFormat formatter = null;
198:
199: if ((type == null) || DATE.equalsIgnoreCase(type)) {
200: formatter = DateFormat.getDateInstance(Util.getStyle(
201: dateStyle, "FORMAT_DATE_INVALID_DATE_STYLE"), loc);
202: } else if (TIME.equalsIgnoreCase(type)) {
203: formatter = DateFormat.getTimeInstance(Util.getStyle(
204: timeStyle, "FORMAT_DATE_INVALID_TIME_STYLE"), loc);
205: } else if (DATETIME.equalsIgnoreCase(type)) {
206: formatter = DateFormat.getDateTimeInstance(Util.getStyle(
207: dateStyle, "FORMAT_DATE_INVALID_DATE_STYLE"), Util
208: .getStyle(timeStyle,
209: "FORMAT_DATE_INVALID_TIME_STYLE"), loc);
210: } else {
211: throw new JspException(Resources.getMessage(
212: "FORMAT_DATE_INVALID_TYPE", type));
213: }
214:
215: return formatter;
216: }
217: }
|