Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/taglibs/datetime/ErasTag.java


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>eras</b>, used to loop through all the era 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>eras</b> tag.
33   * <p>
34   * Loops through all the era 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;eras&lt;/name&gt;
46   * &lt;tagclass&gt;org.apache.taglibs.datetime.ErasTag&lt;/tagclass&gt;
47   * &lt;teiclass&gt;org.apache.taglibs.datetime.ErasTEI&lt;/teiclass&gt;
48   * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
49   * &lt;info&gt;Loop through all the era 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 ErasTag extends BodyTagSupport
71  {
72      // Static constants                     
73      private static String PATTERN = "yyyy";
74  
75      // eras tag attributes
76      private boolean locale_flag = false;
77      private String localeRef = null;
78  
79      // eras tag invocation variables
80      private String [] eras = null;
81      private int count = 0;
82  
83      /**
84       * Initializes tag so it can loop through the eras 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 eras 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   eras = dfs.getEras();
113   // Make sure we skip any blank array elements
114         while( count < eras.length && 
115             (eras[count] == null || eras[count].length() == 0) )
116                 count++;
117         if( count >= eras.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 eras tag.
126      *
127      * @return EVAL_BODY_TAG if there is another era, or SKIP_BODY if there are no more eras
128      */
129     public final int doAfterBody() throws JspException
130     {
131   // See if we are done looping through eras
132   count++;
133         if( count >= eras.length )
134             return SKIP_BODY;
135   // Make sure we skip any blank array elements
136         while( count < eras.length && 
137       (eras[count] == null || eras[count].length() == 0) )
138                 count++;
139 
140   if( count >= eras.length )
141       return SKIP_BODY;
142 
143   // There is another era, 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 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 era names
167      * for client's preferred locale if known.
168      *
169      * @param boolean either <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 value)
183     {
184         localeRef = value;
185     }
186 
187     /**
188      * Returns the era name.
189      * <p>
190      * &lt;jsp:getProperty name=<i>"id"</i> property="name"/&gt;
191      *
192      * @return String - era name
193      */
194     public final String getName()
195     {  
196         return eras[count];
197     }
198 
199 }