KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > fmt > FormatDateSupport


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.common.fmt;
18
19 import java.io.IOException JavaDoc;
20 import java.text.DateFormat JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.TimeZone JavaDoc;
25
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.JspTagException JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
30
31 import org.apache.taglibs.standard.resources.Resources;
32 import org.apache.taglibs.standard.tag.common.core.Util;
33
34 /**
35  * Support for tag handlers for <formatDate>, the date and time
36  * formatting tag in JSTL 1.0.
37  *
38  * @author Jan Luehe
39  */

40
41 public abstract class FormatDateSupport extends TagSupport JavaDoc {
42
43     //*********************************************************************
44
// Private constants
45

46     private static final String JavaDoc DATE = "date";
47     private static final String JavaDoc TIME = "time";
48     private static final String JavaDoc DATETIME = "both";
49
50
51     //*********************************************************************
52
// Protected state
53

54     protected Date JavaDoc value; // 'value' attribute
55
protected String JavaDoc type; // 'type' attribute
56
protected String JavaDoc pattern; // 'pattern' attribute
57
protected Object JavaDoc timeZone; // 'timeZone' attribute
58
protected String JavaDoc dateStyle; // 'dateStyle' attribute
59
protected String JavaDoc timeStyle; // 'timeStyle' attribute
60

61
62     //*********************************************************************
63
// Private state
64

65     private String JavaDoc var; // 'var' attribute
66
private int scope; // 'scope' attribute
67

68
69     //*********************************************************************
70
// Constructor and initialization
71

72     public FormatDateSupport() {
73     super();
74     init();
75     }
76
77     private void init() {
78     type = dateStyle = timeStyle = null;
79     pattern = var = null;
80     value = null;
81     timeZone = null;
82     scope = PageContext.PAGE_SCOPE;
83     }
84
85
86    //*********************************************************************
87
// Tag attributes known at translation time
88

89     public void setVar(String JavaDoc var) {
90         this.var = var;
91     }
92
93     public void setScope(String JavaDoc scope) {
94     this.scope = Util.getScope(scope);
95     }
96
97
98     //*********************************************************************
99
// Tag logic
100

101     /*
102      * Formats the given date and time.
103      */

104     public int doEndTag() throws JspException JavaDoc {
105
106     String JavaDoc formatted = null;
107
108     if (value == null) {
109         if (var != null) {
110         pageContext.removeAttribute(var, scope);
111         }
112         return EVAL_PAGE;
113     }
114
115     // Create formatter
116
Locale JavaDoc locale = SetLocaleSupport.getFormattingLocale(
117             pageContext,
118         this,
119         true,
120         DateFormat.getAvailableLocales());
121
122     if (locale != null) {
123         DateFormat JavaDoc formatter = createFormatter(locale);
124
125         // Apply pattern, if present
126
if (pattern != null) {
127         try {
128             ((SimpleDateFormat JavaDoc) formatter).applyPattern(pattern);
129         } catch (ClassCastException JavaDoc cce) {
130             formatter = new SimpleDateFormat JavaDoc(pattern, locale);
131         }
132         }
133
134         // Set time zone
135
TimeZone JavaDoc tz = null;
136         if ((timeZone instanceof String JavaDoc)
137         && ((String JavaDoc) timeZone).equals("")) {
138         timeZone = null;
139         }
140         if (timeZone != null) {
141         if (timeZone instanceof String JavaDoc) {
142             tz = TimeZone.getTimeZone((String JavaDoc) timeZone);
143         } else if (timeZone instanceof TimeZone JavaDoc) {
144             tz = (TimeZone JavaDoc) timeZone;
145         } else {
146             throw new JspTagException JavaDoc(
147                             Resources.getMessage("FORMAT_DATE_BAD_TIMEZONE"));
148         }
149         } else {
150         tz = TimeZoneSupport.getTimeZone(pageContext, this);
151         }
152         if (tz != null) {
153         formatter.setTimeZone(tz);
154         }
155         formatted = formatter.format(value);
156     } else {
157         // no formatting locale available, use Date.toString()
158
formatted = value.toString();
159     }
160
161     if (var != null) {
162         pageContext.setAttribute(var, formatted, scope);
163     } else {
164         try {
165         pageContext.getOut().print(formatted);
166         } catch (IOException JavaDoc ioe) {
167         throw new JspTagException JavaDoc(ioe.toString(), ioe);
168         }
169     }
170
171     return EVAL_PAGE;
172     }
173
174     // Releases any resources we may have (or inherit)
175
public void release() {
176     init();
177     }
178
179
180     //*********************************************************************
181
// Private utility methods
182

183     private DateFormat JavaDoc createFormatter(Locale JavaDoc loc) throws JspException JavaDoc {
184     DateFormat JavaDoc formatter = null;
185
186     if ((type == null) || DATE.equalsIgnoreCase(type)) {
187         formatter = DateFormat.getDateInstance(
188             Util.getStyle(dateStyle, "FORMAT_DATE_INVALID_DATE_STYLE"),
189         loc);
190     } else if (TIME.equalsIgnoreCase(type)) {
191         formatter = DateFormat.getTimeInstance(
192             Util.getStyle(timeStyle, "FORMAT_DATE_INVALID_TIME_STYLE"),
193         loc);
194     } else if (DATETIME.equalsIgnoreCase(type)) {
195         formatter = DateFormat.getDateTimeInstance(
196             Util.getStyle(dateStyle, "FORMAT_DATE_INVALID_DATE_STYLE"),
197         Util.getStyle(timeStyle, "FORMAT_DATE_INVALID_TIME_STYLE"),
198         loc);
199     } else {
200         throw new JspException JavaDoc(
201                     Resources.getMessage("FORMAT_DATE_INVALID_TYPE",
202                      type));
203     }
204
205     return formatter;
206     }
207 }
208
Popular Tags