KickJava   Java API By Example, From Geeks To Geeks.

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


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>weekdays</b>, used to loop through the days of the week
28  * so that weekday names can be accessed by using the standard
29  * JSP &lt;jsp:getProperty&gt; tag.
30  * <p>
31  * The script variable of name <b>id</b> is availble only within the
32  * body of the <b>weekdays</b> tag.
33  * <p>
34  * Loops through all the weekdays.
35  * <p>
36  * If the optional attribute <b>locale</b> is true, the weekday names
37  * are formatted for the clients locale if known.
38  * <p>
39  * The optional attribute <b>localeRef</b> can be used to specify
40  * the name of a page, session, application, or request scope attribute
41  * of type java.util.Locale to use.
42  * <p>
43  * JSP Tag Lib Descriptor
44  * <p><pre>
45  * &lt;name&gt;weekdays&lt;/name&gt;
46  * &lt;tagclass&gt;org.apache.taglibs.datetime.WeekdaysTag&lt;/tagclass&gt;
47  * &lt;teiclass&gt;org.apache.taglibs.datetime.WeekdaysTEI&lt;/teiclass&gt;
48  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
49  * &lt;info&gt;Loop through all the days of the week.&lt;/info&gt;
50  * &lt;attribute&gt;
51  * &lt;name&gt;id&lt;/name&gt;
52  * &lt;required&gt;true&lt;/required&gt;
53  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
54  * &lt;/attribute&gt;
55  * &lt;attribute&gt;
56  * &lt;name&gt;locale&lt;/name&gt;
57  * &lt;required&gt;false&lt;/required&gt;
58  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
59  * &lt;/attribute&gt;
60  * &lt;attribute&gt;
61  * &lt;name&gt;localeRef&lt;/name&gt;
62  * &lt;required&gt;false&lt;/required&gt;
63  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
64  * &lt;/attribute&gt;
65  * </pre>
66  *
67  * @author Glenn Nielsen
68  */

69
70 public class WeekdaysTag extends BodyTagSupport
71 {
72     // Static constants
73
private static String JavaDoc PATTERN = "yyyy";
74                                            
75     // weekdays tag attributes
76
private boolean locale_flag = false;
77     private String JavaDoc localeRef = null;
78
79     private String JavaDoc [] short_weekdays = null;
80     private String JavaDoc [] long_weekdays = null;
81     private int day = 0;
82     private int day_num = 1;
83
84     /**
85      * Initializes tag so it can loop through the weekdays of the year.
86      *
87      * @return EVAL_BODY_TAG
88      */

89     public final int doStartTag() throws JspException
90     {
91         // Initialize variables
92
day = 0;
93         day_num = 1;
94
95         SimpleDateFormat sdf;
96         if( localeRef != null ) {
97             Locale locale = (Locale)pageContext.findAttribute(localeRef);
98             if( locale == null ) {
99                 throw new JspException(
100                     "datetime amPms tag could not find locale for localeRef \"" +
101                     localeRef + "\".");
102             }
103             
104             sdf = new SimpleDateFormat(PATTERN,locale);
105         } else if( locale_flag ) {
106             sdf = new SimpleDateFormat(PATTERN,
107                       (Locale)pageContext.getRequest().getLocale());
108         } else {
109             sdf = new SimpleDateFormat(PATTERN);
110         }
111
112     DateFormatSymbols dfs = sdf.getDateFormatSymbols();
113     short_weekdays = dfs.getShortWeekdays();
114     long_weekdays = dfs.getWeekdays();
115     // Make sure we skip any blank weekday array elements
116
while( day < long_weekdays.length &&
117         (long_weekdays[day] == null || long_weekdays[day].length() == 0) )
118         day++;
119         if( day >= short_weekdays.length )
120             return SKIP_BODY;
121
122     pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
123     return EVAL_BODY_TAG;
124     }
125
126     /**
127      * Method called at end of each weekdays tag.
128      *
129      * @return EVAL_BODY_TAG if there is another weekday, or SKIP_BODY if there are no more weekdays
130      */

131     public final int doAfterBody() throws JspException
132     {
133     // See if we are done looping through weekdays
134
day++;
135     day_num++;
136         if( day >= short_weekdays.length )
137             return SKIP_BODY;
138     // Make sure we skip any blank weekday array elements
139
while( day < long_weekdays.length &&
140             (long_weekdays[day] == null || long_weekdays[day].length() == 0) )
141                 day++;
142
143     if( day >= short_weekdays.length )
144         return SKIP_BODY;
145
146     // There is another weekday, so loop again
147
return EVAL_BODY_TAG;
148     }
149
150     /**
151      * Method called at end of Tag
152      * @return EVAL_PAGE
153      */

154     public final int doEndTag() throws JspException
155     {
156         pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
157     try
158     {
159         if(bodyContent != null)
160         bodyContent.writeOut(bodyContent.getEnclosingWriter());
161     } catch(java.io.IOException JavaDoc e)
162     {
163         throw new JspException("IO Error: " + e.getMessage());
164     }
165     return EVAL_PAGE;
166     }
167
168     /**
169      * Locale flag, if set to true, use weekday names
170      * for client's preferred locale if known.
171      *
172      * @param boolean either <b>true</b> or <b>false</b>
173      */

174     public final void setLocale(boolean flag)
175     {
176         locale_flag = flag;
177     }
178
179     /**
180      * Provides a key to search the page context for in order to get the
181      * java.util.Locale to use.
182      *
183      * @param String name of locale attribute to use
184      */

185     public void setLocaleRef(String JavaDoc value)
186     {
187         localeRef = value;
188     }
189
190     /**
191      * Returns the short name of the weekday.
192      * <p>
193      * &lt;jsp:getProperty name=<i>"id"</i> property="shortWeekday"/&gt;
194      *
195      * @return String - short name of the weekday
196      */

197     public final String JavaDoc getShortWeekday()
198     {
199     return short_weekdays[day];
200     }
201
202     /**
203      * Returns the long name of the weekday.
204      * <p>
205      * &lt;jsp:getProperty name=<i>"id"</i> property="weekday"/&gt;
206      *
207      * @return String - long name of the weekday
208      */

209     public final String JavaDoc getWeekday()
210     {
211         return long_weekdays[day];
212     }
213
214     /**
215      * Returns the number of the day of the week.
216      * <p>
217      * &lt;jsp:getProperty name=<i>"id"</i> property="dayOfWeek"/&gt;
218      *
219      * @return String - number of the day of the week
220      */

221     public final String JavaDoc getDayOfWeek()
222     {
223         return "" + day_num;
224     }
225
226 }
227
Popular Tags