KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

182     public void setLocaleRef(String JavaDoc value)
183     {
184         localeRef = value;
185     }
186
187     /**
188      * Returns the am/pm name.
189      * <p>
190      * &lt;jsp:getProperty name=<i>"id"</i> property="name"/&gt;
191      *
192      * @return String - am/pm name
193      */

194     public final String JavaDoc getName()
195     {
196         return amPms[count];
197     }
198
199 }
200
Popular Tags