KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > taglib > 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.cocoon.taglib.datetime;
18
19 import java.text.DateFormatSymbols JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import org.apache.cocoon.taglib.IterationTag;
23 import org.apache.cocoon.taglib.TagSupport;
24 import org.apache.cocoon.taglib.VarTagSupport;
25 import org.apache.cocoon.taglib.i18n.LocaleTag;
26 import org.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29 /**
30  * Tag <b>months</b>, used to loop through all the months of the year.
31  * <p>
32  * The script variable of name <b>var</b> is availble only within the
33  * body of the <b>months</b> tag.
34  *
35  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
36  * @version CVS $Id: MonthsTag.java 30932 2004-07-29 17:35:38Z vgritsenko $
37  */

38 public class MonthsTag extends VarTagSupport implements IterationTag {
39     private String JavaDoc[] short_months = null;
40     private String JavaDoc[] long_months = null;
41     private int month;
42     private int month_num;
43
44     /**
45      * Initializes tag so it can loop through the months of the year.
46      *
47      * @return EVAL_BODY
48      */

49     public final int doStartTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
50         // Initialize variables
51
month = 0;
52         month_num = 1;
53
54         Locale JavaDoc locale = null;
55         LocaleTag localeTag = (LocaleTag) TagSupport.findAncestorWithClass(this, LocaleTag.class);
56         if (localeTag != null) {
57             locale = localeTag.getLocale();
58         } else {
59             locale = Locale.getDefault();
60         }
61
62         DateFormatSymbols JavaDoc dfs = new DateFormatSymbols JavaDoc(locale);
63         short_months = dfs.getShortMonths();
64         long_months = dfs.getMonths();
65
66         // Make sure we skip any blank array elements
67
while (month < long_months.length && (long_months[month] == null || long_months[month].length() == 0)) {
68             month++;
69         }
70
71         if (month >= short_months.length)
72             return SKIP_BODY;
73
74         setVariable(var, this);
75         return EVAL_BODY;
76     }
77
78     /*
79      * @see Tag#doEndTag(String, String, String)
80      */

81     public int doEndTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
82         removeVariable(var);
83         return EVAL_PAGE;
84     }
85
86     /**
87      * Method called at end of each months tag.
88      *
89      * @return EVAL_BODY_TAG if there is another month, or SKIP_BODY if there are no more months
90      */

91     public final int doAfterBody() throws SAXException JavaDoc {
92         // See if we are done looping through months
93
month++;
94         month_num++;
95         if (month >= short_months.length)
96             return SKIP_BODY;
97
98         // Make sure we skip any blank array elements
99
while (month < long_months.length && (long_months[month] == null || long_months[month].length() == 0)) {
100             month++;
101         }
102
103         if (month >= short_months.length)
104             return SKIP_BODY;
105
106         // There is another month, so loop again
107
return EVAL_BODY_AGAIN;
108     }
109
110     /**
111      * Returns the short name of the month.
112      *
113      * @return String - short name of the month
114      */

115     public final String JavaDoc getShortMonth() {
116         return short_months[month];
117     }
118
119     /**
120      * Returns the long name of the month.
121      *
122      * @return String - long name of the month
123      */

124     public final String JavaDoc getMonth() {
125         return long_months[month];
126     }
127
128     /**
129      * Returns the number of the month.
130      *
131      * @return String - number of the month
132      */

133     public final String JavaDoc getMonthOfYear() {
134         if (month_num < 10)
135             return "0" + month_num;
136         return "" + month_num;
137     }
138
139     /*
140      * @see Recyclable#recycle()
141      */

142     public void recycle() {
143         this.short_months = null;
144         this.long_months = null;
145         super.recycle();
146     }
147
148 }
149
Popular Tags