KickJava   Java API By Example, From Geeks To Geeks.

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


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

41
42 public abstract class ParseDateSupport extends BodyTagSupport JavaDoc {
43
44     //*********************************************************************
45
// Private constants
46

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

55     protected String JavaDoc value; // 'value' attribute
56
protected boolean valueSpecified; // status
57
protected String JavaDoc type; // 'type' attribute
58
protected String JavaDoc pattern; // 'pattern' attribute
59
protected Object JavaDoc timeZone; // 'timeZone' attribute
60
protected Locale JavaDoc parseLocale; // 'parseLocale' attribute
61
protected String JavaDoc dateStyle; // 'dateStyle' attribute
62
protected String JavaDoc timeStyle; // 'timeStyle' attribute
63

64
65     //*********************************************************************
66
// Private state
67

68     private String JavaDoc var; // 'var' attribute
69
private int scope; // 'scope' attribute
70

71
72     //*********************************************************************
73
// Constructor and initialization
74

75     public ParseDateSupport() {
76     super();
77     init();
78     }
79
80     private void init() {
81     type = dateStyle = timeStyle = null;
82     value = pattern = var = null;
83     valueSpecified = false;
84     timeZone = null;
85     scope = PageContext.PAGE_SCOPE;
86     parseLocale = null;
87     }
88
89
90    //*********************************************************************
91
// Tag attributes known at translation time
92

93     public void setVar(String JavaDoc var) {
94         this.var = var;
95     }
96
97     public void setScope(String JavaDoc scope) {
98     this.scope = Util.getScope(scope);
99     }
100
101
102     //*********************************************************************
103
// Tag logic
104

105     public int doEndTag() throws JspException JavaDoc {
106
107         String JavaDoc input = null;
108
109         // determine the input by...
110
if (valueSpecified) {
111         // ... reading 'value' attribute
112
input = value;
113     } else {
114         // ... retrieving and trimming our body
115
if (bodyContent != null && bodyContent.getString() != null)
116             input = bodyContent.getString().trim();
117     }
118
119     if ((input == null) || input.equals("")) {
120         if (var != null) {
121         pageContext.removeAttribute(var, scope);
122         }
123         return EVAL_PAGE;
124     }
125
126     /*
127      * Set up parsing locale: Use locale specified via the 'parseLocale'
128      * attribute (if present), or else determine page's locale.
129      */

130     Locale JavaDoc locale = parseLocale;
131     if (locale == null)
132         locale = SetLocaleSupport.getFormattingLocale(
133                 pageContext,
134             this,
135         false,
136             DateFormat.getAvailableLocales());
137     if (locale == null) {
138         throw new JspException JavaDoc(
139                     Resources.getMessage("PARSE_DATE_NO_PARSE_LOCALE"));
140     }
141
142     // Create parser
143
DateFormat JavaDoc parser = createParser(locale);
144
145     // Apply pattern, if present
146
if (pattern != null) {
147         try {
148         ((SimpleDateFormat JavaDoc) parser).applyPattern(pattern);
149         } catch (ClassCastException JavaDoc cce) {
150         parser = new SimpleDateFormat JavaDoc(pattern, locale);
151         }
152     }
153
154     // Set time zone
155
TimeZone JavaDoc tz = null;
156     if ((timeZone instanceof String JavaDoc) && ((String JavaDoc) timeZone).equals("")) {
157         timeZone = null;
158     }
159     if (timeZone != null) {
160         if (timeZone instanceof String JavaDoc) {
161         tz = TimeZone.getTimeZone((String JavaDoc) timeZone);
162         } else if (timeZone instanceof TimeZone JavaDoc) {
163         tz = (TimeZone JavaDoc) timeZone;
164         } else {
165         throw new JspException JavaDoc(
166                     Resources.getMessage("PARSE_DATE_BAD_TIMEZONE"));
167         }
168     } else {
169         tz = TimeZoneSupport.getTimeZone(pageContext, this);
170     }
171     if (tz != null) {
172         parser.setTimeZone(tz);
173     }
174
175     // Parse date
176
Date JavaDoc parsed = null;
177     try {
178         parsed = parser.parse(input);
179     } catch (ParseException JavaDoc pe) {
180         throw new JspException JavaDoc(
181                 Resources.getMessage("PARSE_DATE_PARSE_ERROR", input),
182             pe);
183     }
184
185     if (var != null) {
186         pageContext.setAttribute(var, parsed, scope);
187     } else {
188         try {
189         pageContext.getOut().print(parsed);
190         } catch (IOException JavaDoc ioe) {
191         throw new JspTagException JavaDoc(ioe.toString(), ioe);
192         }
193     }
194
195     return EVAL_PAGE;
196     }
197
198     // Releases any resources we may have (or inherit)
199
public void release() {
200     init();
201     }
202
203
204     //*********************************************************************
205
// Private utility methods
206

207     private DateFormat JavaDoc createParser(Locale JavaDoc loc) throws JspException JavaDoc {
208     DateFormat JavaDoc parser = null;
209
210     if ((type == null) || DATE.equalsIgnoreCase(type)) {
211         parser = DateFormat.getDateInstance(
212             Util.getStyle(dateStyle, "PARSE_DATE_INVALID_DATE_STYLE"),
213         loc);
214     } else if (TIME.equalsIgnoreCase(type)) {
215         parser = DateFormat.getTimeInstance(
216             Util.getStyle(timeStyle, "PARSE_DATE_INVALID_TIME_STYLE"),
217         loc);
218     } else if (DATETIME.equalsIgnoreCase(type)) {
219         parser = DateFormat.getDateTimeInstance(
220             Util.getStyle(dateStyle, "PARSE_DATE_INVALID_DATE_STYLE"),
221         Util.getStyle(timeStyle, "PARSE_DATE_INVALID_TIME_STYLE"),
222         loc);
223     } else {
224         throw new JspException JavaDoc(
225                     Resources.getMessage("PARSE_DATE_INVALID_TYPE", type));
226     }
227
228     parser.setLenient(false);
229
230     return parser;
231     }
232 }
233
Popular Tags