KickJava   Java API By Example, From Geeks To Geeks.

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


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>months</b>, used to loop through all the months of the year
28  * so that month 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>months</b> tag.
33  * <p>
34  * Loops through all the months.
35  * <p>
36  * If the optional attribute <b>locale</b> is true, the month 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;months&lt;/name&gt;
46  * &lt;tagclass&gt;org.apache.taglibs.datetime.MonthsTag&lt;/tagclass&gt;
47  * &lt;teiclass&gt;org.apache.taglibs.datetime.MonthsTEI&lt;/teiclass&gt;
48  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
49  * &lt;info&gt;Loop through all the months of the year.&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 MonthsTag extends BodyTagSupport
71 {
72     // Static constants
73
private static String JavaDoc PATTERN = "yyyy";
74
75     // months tag attributes
76
private boolean locale_flag = false;
77     private String JavaDoc localeRef = null;
78
79     // months tag invocation variables
80
private String JavaDoc [] short_months = null;
81     private String JavaDoc [] long_months = null;
82     private int month = 0;
83     private int month_num = 1;
84
85     /**
86      * Initializes tag so it can loop through the months of the year.
87      *
88      * @return EVAL_BODY_TAG
89      */

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

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

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

176     public final void setLocale(boolean flag)
177     {
178         locale_flag = flag;
179     }
180
181     /**
182      * Returns the short name of the month.
183      * <p>
184      * &lt;jsp:getProperty name=<i>"id"</i> property="shortMonth"/&gt;
185      *
186      * @return String - short name of the month
187      */

188     public final String JavaDoc getShortMonth()
189     {
190     return short_months[month];
191     }
192
193     /**
194      * Returns the long name of the month.
195      * <p>
196      * &lt;jsp:getProperty name=<i>"id"</i> property="month"/&gt;
197      *
198      * @return String - long name of the month
199      */

200     public final String JavaDoc getMonth()
201     {
202         return long_months[month];
203     }
204
205     /**
206      * Provides a key to search the page context for in order to get the
207      * java.util.Locale to use.
208      *
209      * @param String name of locale attribute to use
210      */

211     public void setLocaleRef(String JavaDoc value)
212     {
213         localeRef = value;
214     }
215
216     /**
217      * Returns the number of the month.
218      * <p>
219      * &lt;jsp:getProperty name=<i>"id"</i> property="monthOfYear"/&gt;
220      *
221      * @return String - number of the month
222      */

223     public final String JavaDoc getMonthOfYear()
224     {
225     if( month_num < 10 )
226         return "0" + month_num;
227         return "" + month_num;
228     }
229
230 }
231
Popular Tags