1 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 69 70 public class MonthsTag extends BodyTagSupport 71 { 72 private static String PATTERN = "yyyy"; 74 75 private boolean locale_flag = false; 77 private String localeRef = null; 78 79 private String [] short_months = null; 81 private String [] long_months = null; 82 private int month = 0; 83 private int month_num = 1; 84 85 90 public final int doStartTag() throws JspException 91 { 92 month = 0; 94 month_num = 1; 95 96 SimpleDateFormat sdf; 97 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 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 133 public final int doAfterBody() throws JspException 134 { 135 month++; 137 month_num++; 138 if( month >= short_months.length ) 139 return SKIP_BODY; 140 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 return EVAL_BODY_TAG; 150 } 151 152 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 e) 164 { 165 throw new JspException("IO Error: " + e.getMessage()); 166 } 167 return EVAL_PAGE; 168 } 169 170 176 public final void setLocale(boolean flag) 177 { 178 locale_flag = flag; 179 } 180 181 188 public final String getShortMonth() 189 { 190 return short_months[month]; 191 } 192 193 200 public final String getMonth() 201 { 202 return long_months[month]; 203 } 204 205 211 public void setLocaleRef(String value) 212 { 213 localeRef = value; 214 } 215 216 223 public final String getMonthOfYear() 224 { 225 if( month_num < 10 ) 226 return "0" + month_num; 227 return "" + month_num; 228 } 229 230 } 231 | Popular Tags |