001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.taglib.datetime;
019:
020: import java.text.DateFormatSymbols;
021: import java.util.Locale;
022:
023: import org.apache.cocoon.taglib.IterationTag;
024: import org.apache.cocoon.taglib.TagSupport;
025: import org.apache.cocoon.taglib.VarTagSupport;
026: import org.apache.cocoon.taglib.i18n.LocaleTag;
027: import org.xml.sax.Attributes;
028: import org.xml.sax.SAXException;
029:
030: /**
031: * Tag <b>months</b>, used to loop through all the months of the year.
032: * <p>
033: * The script variable of name <b>var</b> is availble only within the
034: * body of the <b>months</b> tag.
035: *
036: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
037: * @version CVS $Id: MonthsTag.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039: public class MonthsTag extends VarTagSupport implements IterationTag {
040: private String[] short_months = null;
041: private String[] long_months = null;
042: private int month;
043: private int month_num;
044:
045: /**
046: * Initializes tag so it can loop through the months of the year.
047: *
048: * @return EVAL_BODY
049: */
050: public final int doStartTag(String namespaceURI, String localName,
051: String qName, Attributes atts) throws SAXException {
052: // Initialize variables
053: month = 0;
054: month_num = 1;
055:
056: Locale locale = null;
057: LocaleTag localeTag = (LocaleTag) TagSupport
058: .findAncestorWithClass(this , LocaleTag.class);
059: if (localeTag != null) {
060: locale = localeTag.getLocale();
061: } else {
062: locale = Locale.getDefault();
063: }
064:
065: DateFormatSymbols dfs = new DateFormatSymbols(locale);
066: short_months = dfs.getShortMonths();
067: long_months = dfs.getMonths();
068:
069: // Make sure we skip any blank array elements
070: while (month < long_months.length
071: && (long_months[month] == null || long_months[month]
072: .length() == 0)) {
073: month++;
074: }
075:
076: if (month >= short_months.length)
077: return SKIP_BODY;
078:
079: setVariable(var, this );
080: return EVAL_BODY;
081: }
082:
083: /*
084: * @see Tag#doEndTag(String, String, String)
085: */
086: public int doEndTag(String namespaceURI, String localName,
087: String qName) throws SAXException {
088: removeVariable(var);
089: return EVAL_PAGE;
090: }
091:
092: /**
093: * Method called at end of each months tag.
094: *
095: * @return EVAL_BODY_TAG if there is another month, or SKIP_BODY if there are no more months
096: */
097: public final int doAfterBody() throws SAXException {
098: // See if we are done looping through months
099: month++;
100: month_num++;
101: if (month >= short_months.length)
102: return SKIP_BODY;
103:
104: // Make sure we skip any blank array elements
105: while (month < long_months.length
106: && (long_months[month] == null || long_months[month]
107: .length() == 0)) {
108: month++;
109: }
110:
111: if (month >= short_months.length)
112: return SKIP_BODY;
113:
114: // There is another month, so loop again
115: return EVAL_BODY_AGAIN;
116: }
117:
118: /**
119: * Returns the short name of the month.
120: *
121: * @return String - short name of the month
122: */
123: public final String getShortMonth() {
124: return short_months[month];
125: }
126:
127: /**
128: * Returns the long name of the month.
129: *
130: * @return String - long name of the month
131: */
132: public final String getMonth() {
133: return long_months[month];
134: }
135:
136: /**
137: * Returns the number of the month.
138: *
139: * @return String - number of the month
140: */
141: public final String getMonthOfYear() {
142: if (month_num < 10)
143: return "0" + month_num;
144: return "" + month_num;
145: }
146:
147: /*
148: * @see Recyclable#recycle()
149: */
150: public void recycle() {
151: this.short_months = null;
152: this.long_months = null;
153: super.recycle();
154: }
155:
156: }
|