KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > datetime > ParseTag


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.datetime;
18
19 import java.util.*;
20 import java.text.*;
21 import javax.servlet.*;
22 import javax.servlet.http.*;
23 import javax.servlet.jsp.*;
24 import javax.servlet.jsp.tagext.*;
25
26 /**
27  * JSP Tag <b>parse</b>, used to parse a Date string and output
28  * the time in ms.
29  * <p>
30  * The Date as a string is obtained from the body of the tag.
31  * <p>
32  * Uses the optional attribute <b>pattern</b> as the pattern
33  * to use when parsing the Date string.
34  * <p>
35  * The optional attribute <b>timeZone</b> can be set to the id of
36  * a timeZone script varaible so that the Date if adjusted for
37  * that timeZone.
38  * <p>
39  * If the optional attribute <b>locale</b> is true, the Date
40  * is parsed for the clients locale if known.
41  * <p>
42  * The optional attribute <b>localeRef</b> can be used to specify
43  * the name of a page, session, application, or request scope attribute
44  * of type java.util.Locale to use.
45  * <p>
46  * If the date string can not be parsed, 0 is returned.
47  * <p>
48  * JSP Tag Lib Descriptor
49  * <p><pre>
50  * &lt;name&gt;parse&lt;/name&gt;
51  * &lt;tagclass&gt;org.apache.taglibs.datetime.ParseTag&lt;/tagclass&gt;
52  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
53  * &lt;info&gt;Parses a date string and outputs the time in milliseconds since Jan 1, 1970 GMT.&lt;/info&gt;
54  * &lt;attribute&gt;
55  * &lt;name&gt;pattern&lt;/name&gt;
56  * &lt;required&gt;false&lt;/required&gt;
57  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
58  * &lt;/attribute&gt;
59  * &lt;attribute&gt;
60  * &lt;name&gt;patternId&lt;/name&gt;
61  * &lt;required&gt;false&lt;/required&gt;
62  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
63  * &lt;/attribute&gt;
64  * &lt;attribute&gt;
65  * &lt;name&gt;timeZone&lt;/name&gt;
66  * &lt;required&gt;false&lt;/required&gt;
67  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
68  * &lt;/attribute&gt;
69  * &lt;attribute&gt;
70  * &lt;name&gt;locale&lt;/name&gt;
71  * &lt;required&gt;false&lt;/required&gt;
72  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
73  * &lt;/attribute&gt;
74  * &lt;attribute&gt;
75  * &lt;name&gt;localeRef&lt;/name&gt;
76  * &lt;required&gt;false&lt;/required&gt;
77  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
78  * &lt;/attribute&gt;
79  * </pre>
80  *
81  * @author Glenn Nielsen
82  */

83
84 public class ParseTag extends BodyTagSupport
85 {
86     // Optional attribute, use users locale if known when formatting date
87
private boolean locale_flag = false;
88     // Optional attribute, time pattern string to use when formatting date
89
private String JavaDoc pattern = null;
90     // Optional attribute, name of script variable to use as pattern
91
private String JavaDoc patternid = null;
92     // Optional attribute, timeZone script variable id to use when
93
// formatting date
94
private String JavaDoc timeZone_string;
95     // Optional attribute, the name of an attribute which contains the Locale
96
private String JavaDoc localeRef = null;
97                                                                              
98     // format tag invocation variables
99
private TimeZone timeZone = null;
100     // Date after parsing tag body
101
private Date date = null;
102
103     /**
104      * Method called at start of tag, always returns EVAL_BODY_TAG
105      *
106      * @return EVAL_BODY_TAG
107      */

108     public final int doStartTag() throws JspException
109     {
110     return EVAL_BODY_TAG;
111     }
112
113     /**
114      * Method called at end of parse tag body.
115      *
116      * @return SKIP_BODY
117      */

118     public final int doAfterBody() throws JspException
119     {
120     // Use the body of the tag as input for the date
121
BodyContent body = getBodyContent();
122     String JavaDoc s = body.getString().trim();
123     // Clear the body since we will output only the parseted date
124
body.clearBody();
125
126         // Get the pattern to use
127
SimpleDateFormat sdf;
128         String JavaDoc pat = pattern;
129  
130         if( pat == null && patternid != null ) {
131           Object JavaDoc attr = pageContext.findAttribute(patternid);
132           if( attr != null )
133             pat = attr.toString();
134         }
135         
136         if( pat == null ) {
137             sdf = new SimpleDateFormat();
138             pat = sdf.toPattern();
139         }
140
141     // Get a SimpleDateFormat using locale if necessary
142
if( localeRef != null ) {
143             Locale locale = (Locale)pageContext.findAttribute(localeRef);
144             if( locale == null ) {
145                 throw new JspException(
146                     "datetime parse tag could not find locale for localeRef \"" +
147                     localeRef + "\".");
148             }
149                                        
150             sdf = new SimpleDateFormat(pat,locale);
151         } else if( locale_flag ) {
152             sdf = new SimpleDateFormat(pat,
153                       (Locale)pageContext.getRequest().getLocale());
154         } else {
155             sdf = new SimpleDateFormat(pat);
156         }
157
158     // See if there is a timeZone
159
if( timeZone_string != null ) {
160             timeZone =
161                 (TimeZone)pageContext.getAttribute(timeZone_string,
162                                                    PageContext.SESSION_SCOPE);
163         if( timeZone == null )
164                 throw new JspTagException("Datetime parse tag timeZone " +
165                     "script variable \"" + timeZone_string +
166                     " \" does not exist");
167             sdf.setTimeZone(timeZone);
168         }
169
170     // Parse the string and create the date.
171
try {
172             date = null;
173         date = sdf.parse(s);
174     } catch(ParseException e) {
175     }
176     return SKIP_BODY;
177     }
178
179     /**
180      * Method called at end of Tag
181      *
182      * @return EVAL_PAGE
183      */

184     public final int doEndTag() throws JspException
185     {
186     long time = 0;
187     if( date != null )
188         time = date.getTime();
189     try {
190         pageContext.getOut().write("" + time);
191     } catch(Exception JavaDoc e) {
192         throw new JspException("IO Error: " + e.getMessage());
193     }
194
195     return EVAL_PAGE;
196     }
197
198     /**
199      * Locale flag, if set to true, date format is for
200      * client's preferred locale if known.
201      *
202      * @param boolean use users locale, true or false
203      */

204     public final void setLocale(boolean flag)
205     {
206         locale_flag = flag;
207     }
208  
209     /**
210      * Set the time zone to use when parsing date.
211      *
212      * Value must be the name of a <b>timeZone</b> tag script
213      * variable ID.
214      *
215      * @param String name of timeZone to use
216      */

217     public final void setTimeZone(String JavaDoc tz)
218     {
219         timeZone_string = tz;
220     }
221  
222     /**
223      * Set the pattern to use when parsing Date.
224      *
225      * @param String SimpleDateFormat style time pattern format string
226      */

227     public final void setPattern(String JavaDoc str)
228     {
229         pattern = str;
230     }
231
232     /**
233      * Set the pattern to use when parsing Date using a script variable
234      * attribute.
235      *
236      * @param String name of script variable attribute id
237      */

238     public final void setPatternId(String JavaDoc str)
239     {
240         patternid = str;
241     }
242
243     /**
244      * Provides a key to search the page context for in order to get the
245      * java.util.Locale to use.
246      *
247      * @param String name of locale attribute to use
248      */

249     public void setLocaleRef(String JavaDoc value)
250     {
251         localeRef = value;
252     }
253
254 }
255
Popular Tags