KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.*;
21 import javax.servlet.http.*;
22 import javax.servlet.jsp.*;
23 import javax.servlet.jsp.tagext.*;
24
25 /**
26  * JSP Tag <b>timeZones</b>, used to loop through all the TimeZone's
27  * so that ID's and Display Names can be accessed by using the standard
28  * JSP &lt;jsp:getProperty&gt; tag.
29  * <p>
30  * The script variable of name <b>id</b> is availble only within the
31  * body of the <b>timeZones</b> tag.
32  * <p>
33  * Loops through all the timeZones.
34  * <p>
35  * If the optional attribute <b>locale</b> is <b>true</b>, the Displaynames
36  * are formatted for the clients locale if known.
37  * <p>
38  * The optional attribute <b>localeRef</b> can be used to specify
39  * the name of a page, session, application, or request scope attribute
40  * of type java.util.Locale to use.
41  * <p>
42  * The optional attribute <b>style</b> can be set to <i>SHORT</i> or
43  * <i>LONG</i>.
44  * <p>
45  * JSP Tag Lib Descriptor
46  * <p><pre>
47  * &lt;name&gt;timeZones&lt;/name&gt;
48  * &lt;tagclass&gt;org.apache.taglibs.datetime.TimeZonesTag&lt;/tagclass&gt;
49  * &lt;teiclass&gt;org.apache.taglibs.datetime.TimeZonesTEI&lt;/teiclass&gt;
50  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
51  * &lt;info&gt;Loop through all the timeZone display names.&lt;/info&gt;
52  * &lt;attribute&gt;
53  * &lt;name&gt;id&lt;/name&gt;
54  * &lt;required&gt;true&lt;/required&gt;
55  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
56  * &lt;/attribute&gt;
57  * &lt;attribute&gt;
58  * &lt;name&gt;locale&lt;/name&gt;
59  * &lt;required&gt;false&lt;/required&gt;
60  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
61  * &lt;/attribute&gt;
62  * &lt;attribute&gt;
63  * &lt;name&gt;localeRef&lt;/name&gt;
64  * &lt;required&gt;false&lt;/required&gt;
65  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
66  * &lt;/attribute&gt;
67  * &lt;attribute&gt;
68  * &lt;name&gt;style&lt;/name&gt;
69  * &lt;required&gt;false&lt;/required&gt;
70  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
71  * &lt;/attribute&gt;
72  * </pre>
73  *
74  * @author Glenn Nielsen
75  */

76
77 public class TimeZonesTag extends BodyTagSupport
78 {
79     // timeZones tag attributes
80
private boolean locale_flag = false;
81     private String JavaDoc localeRef = null;
82     private String JavaDoc style_string = "SHORT";
83
84     // timeZones tag invocation variables
85
private int style = TimeZone.SHORT;
86     private String JavaDoc [] timeZones = null;
87     private TimeZone timeZone = null;
88     private int zone_num = 0;
89
90     /**
91      * Initializes tag so it can loop through the time zones.
92      *
93      * @return EVAL_BODY_TAG, or SKIP_BODY if no time zones are found
94      */

95     public final int doStartTag() throws JspException
96     {
97         // Initialize variables
98
zone_num = 0;
99
100         if( style_string == null || style_string.equals("SHORT") )
101             style = TimeZone.SHORT;
102         else if( style_string.equals("LONG") )
103             style = TimeZone.LONG;
104         else
105            throw new JspTagException(
106                "Datetime tag timeZones style attribute must be set to" +
107                " either SHORT or LONG");
108     timeZones = TimeZone.getAvailableIDs();
109
110     timeZone = TimeZone.getTimeZone(timeZones[zone_num]);
111     if( timeZone == null )
112         return SKIP_BODY;
113
114     pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
115     return EVAL_BODY_TAG;
116     }
117
118     /**
119      * Method called at end of each timeZones tag.
120      *
121      * @return EVAL_BODY_TAG if there is another timeZone, or SKIP_BODY if there are no more timeZones
122      */

123     public final int doAfterBody() throws JspException
124     {
125     // See if we are done looping through timeZones
126
zone_num++;
127     if( zone_num >= timeZones.length )
128         return SKIP_BODY;
129
130     // There is another timeZone, so loop again
131
timeZone = TimeZone.getTimeZone(timeZones[zone_num]);
132     if( timeZone == null )
133         return SKIP_BODY;
134
135     return EVAL_BODY_TAG;
136     }
137
138     /**
139      * Method called at end of Tag
140      *
141      * @return EVAL_PAGE
142      */

143     public final int doEndTag() throws JspException
144     {
145         pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
146     try
147     {
148         if(bodyContent != null)
149         bodyContent.writeOut(bodyContent.getEnclosingWriter());
150     } catch(java.io.IOException JavaDoc e)
151     {
152         throw new JspException("IO Error: " + e.getMessage());
153     }
154     return EVAL_PAGE;
155     }
156
157     /**
158      * Locale flag, if set to true, format timeZone Displayname
159      * for client's preferred locale if known.
160      *
161      * @param boolean either <b>true</b> or <b>false</b>
162      */

163     public final void setLocale(boolean flag)
164     {
165         locale_flag = flag;
166     }
167
168     /**
169      * Provides a key to search the page context for in order to get the
170      * java.util.Locale to use.
171      *
172      * @param String name of locale attribute to use
173      */

174     public void setLocaleRef(String JavaDoc value)
175     {
176         localeRef = value;
177     }
178
179     /**
180      * Set they style of Displaynames to either <b>SHORT</b> or <b>LONG</b>.
181      *
182      * @param String style, either <b>SHORT</b> or <b>LONG</b>
183      */

184     public final void setStyle(String JavaDoc str)
185     {
186         style_string = str;
187     }
188
189     /**
190      * Returns the display name of the timeZone.
191      * <p>
192      * &lt;jsp:getProperty name=<i>"id"</i> property="displayName"/&gt;
193      *
194      * @return String - display name
195      */

196     public final String JavaDoc getDisplayName() throws JspException
197     {
198     String JavaDoc dn = null;
199     Date now = new Date();
200     boolean daylight = false;
201
202     if( timeZone.useDaylightTime() )
203         daylight = timeZone.inDaylightTime(now);
204
205         if( localeRef != null ) {
206             Locale locale = (Locale)pageContext.findAttribute(localeRef);
207             if( locale == null ) {
208                 throw new JspException(
209                     "datetime amPms tag could not find locale for localeRef \"" +
210                     localeRef + "\".");
211             }
212             dn = timeZone.getDisplayName(daylight,style,locale);
213     } else if( locale_flag) {
214         dn = timeZone.getDisplayName(daylight,style,
215                      (Locale)pageContext.getRequest().getLocale());
216     } else {
217         dn = timeZone.getDisplayName(daylight, style);
218     }
219
220     if( dn == null )
221         dn = "";
222
223     return dn;
224     }
225
226     /**
227      * Returns the value of the time zone ID.
228      * <p>
229      * &lt;jsp:getProperty name=<i>"id"</i> property="zoneId"/&gt;
230      *
231      * @return String - time zone ID
232      */

233     public final String JavaDoc getZoneId()
234     {
235     return timeZone.getID();
236     }
237
238 }
239
Popular Tags