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 105 106 public class FormatTag extends BodyTagSupport 107 { 108 109 111 private boolean locale_flag = false; 113 private String pattern = null; 115 private String patternid = null; 117 private String timeZone_string; 119 private Date date = null; 121 private String default_text = "Invalid Date"; 123 private String localeRef = null; 125 private String symbolsRef = null; 127 128 130 private DateFormatSymbols symbols = null; 132 private Date output_date = null; 134 135 140 public final int doStartTag() throws JspException 141 { 142 output_date = date; 143 return EVAL_BODY_TAG; 144 } 145 146 151 public final int doAfterBody() throws JspException 152 { 153 BodyContent body = getBodyContent(); 155 String s = body.getString().trim(); 156 body.clearBody(); 158 if( output_date == null ) { 159 long time; 160 try { 161 time = Long.valueOf(s).longValue(); 162 output_date = new Date(time); 163 } catch(NumberFormatException nfe) { 164 } 165 } 166 167 return SKIP_BODY; 168 } 169 170 175 public final int doEndTag() throws JspException 176 { 177 String date_formatted = default_text; 178 179 if (output_date != null) { 180 SimpleDateFormat sdf; 182 String pat = pattern; 183 184 if (pat == null && patternid != null) { 185 Object attr = pageContext.findAttribute(patternid); 186 if (attr != null) 187 pat = attr.toString(); 188 } 189 190 if (pat == null) { 191 sdf = new SimpleDateFormat(); 192 pat = sdf.toPattern(); 193 } 194 195 if (symbolsRef != null) { 197 symbols = (DateFormatSymbols) pageContext.findAttribute(symbolsRef); 198 if (symbols == null) { 199 throw new JspException( 200 "datetime format tag could not find dateFormatSymbols for symbolsRef \"" + 201 symbolsRef + "\"."); 202 } 203 } 204 205 if (localeRef != null) { 207 Locale locale = (Locale) pageContext.findAttribute(localeRef); 208 if (locale == null) { 209 throw new JspException( 210 "datetime format tag could not find locale for localeRef \"" + 211 localeRef + "\"."); 212 } 213 214 sdf = new SimpleDateFormat(pat, locale); 215 } else if (locale_flag) { 216 sdf = new SimpleDateFormat(pat, 217 (Locale) pageContext.getRequest().getLocale()); 218 } else if (symbols != null) { 219 sdf = new SimpleDateFormat(pat, 220 symbols); 221 } else { 222 sdf = new SimpleDateFormat(pat); 223 } 224 225 if (timeZone_string != null) { 227 TimeZone timeZone = 228 (TimeZone) pageContext.getAttribute(timeZone_string, 229 PageContext.SESSION_SCOPE); 230 if (timeZone == null) { 231 throw new JspTagException("Datetime format tag timeZone " + 232 "script variable \"" + timeZone_string + 233 " \" does not exist"); 234 } 235 sdf.setTimeZone(timeZone); 236 } 237 238 date_formatted = sdf.format(output_date); 240 } 241 242 try { 243 pageContext.getOut().write(date_formatted); 244 } catch (Exception e) { 245 throw new JspException("IO Error: " + e.getMessage()); 246 } 247 248 return EVAL_PAGE; 249 } 250 251 public void release() 252 { 253 super.release(); 254 locale_flag = false; 255 pattern = null; 256 patternid = null; 257 date = null; 258 localeRef = null; 259 symbolsRef = null; 260 symbols = null; 261 } 262 263 269 public final void setLocale(boolean flag) 270 { 271 locale_flag = flag; 272 } 273 274 282 public final void setTimeZone(String tz) 283 { 284 timeZone_string = tz; 285 } 286 287 292 public final void setPattern(String str) 293 { 294 pattern = str; 295 } 296 297 303 public final void setPatternId(String str) 304 { 305 patternid = str; 306 } 307 308 313 public final void setDate(Date date) 314 { 315 this.date = date; 316 } 317 318 323 public final void setDefault(String default_text) 324 { 325 this.default_text = default_text; 326 } 327 328 334 public void setLocaleRef(String value) 335 { 336 localeRef = value; 337 } 338 339 345 public void setSymbolsRef(String symbolsRef) throws JspException 346 { 347 this.symbolsRef = symbolsRef; 348 } 349 } 350 | Popular Tags |