1 16 package com.blandware.atleap.webapp.taglib.core; 17 18 import com.blandware.atleap.common.util.DateUtil; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.struts.Globals; 22 import org.apache.struts.taglib.TagUtils; 23 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.JspTagException ; 26 import javax.servlet.jsp.PageContext ; 27 import javax.servlet.jsp.tagext.SimpleTagSupport ; 28 import java.text.DateFormat ; 29 import java.util.Calendar ; 30 import java.util.Date ; 31 import java.util.Locale ; 32 33 98 public class FormatDateTag extends SimpleTagSupport { 99 100 protected transient final Log log = LogFactory.getLog(FormatDateTag.class); 101 102 107 protected Object value; 108 109 112 protected Object locale; 113 114 117 protected String type = "date"; 118 119 125 protected String dateStyle = "medium"; 126 127 133 protected String timeStyle = "medium"; 134 135 138 protected String var; 139 140 143 protected String scope; 144 145 155 public Object getValue() { 156 return value; 157 } 158 159 165 public void setValue(Object value) { 166 this.value = value; 167 } 168 169 179 public Object getLocale() { 180 return locale; 181 } 182 183 189 public void setLocale(Object locale) { 190 this.locale = locale; 191 } 192 193 203 public String getType() { 204 return type; 205 } 206 207 213 public void setType(String type) { 214 this.type = type; 215 } 216 217 227 public String getDateStyle() { 228 return dateStyle; 229 } 230 231 237 public void setDateStyle(String dateStyle) { 238 this.dateStyle = dateStyle; 239 } 240 241 251 public String getTimeStyle() { 252 return timeStyle; 253 } 254 255 261 public void setTimeStyle(String timeStyle) { 262 this.timeStyle = timeStyle; 263 } 264 265 275 public String getVar() { 276 return var; 277 } 278 279 285 public void setVar(String var) { 286 this.var = var; 287 } 288 289 299 public String getScope() { 300 return scope; 301 } 302 303 309 public void setScope(String scope) { 310 this.scope = scope; 311 } 312 313 319 public void doTag() throws JspException { 320 321 PageContext pageContext = (PageContext ) getJspContext(); 322 323 if ( value == null ) { 324 value = new Date (); 325 } 326 327 int dateStyle = DateFormat.MEDIUM; 328 329 if ( "short".equalsIgnoreCase(this.dateStyle) ) { 330 dateStyle = DateFormat.SHORT; 331 } else if ( "long".equalsIgnoreCase(this.dateStyle) ) { 332 dateStyle = DateFormat.LONG; 333 } else if ( "full".equalsIgnoreCase(this.dateStyle) ) { 334 dateStyle = DateFormat.FULL; 335 } 336 337 int timeStyle = DateFormat.MEDIUM; 338 339 if ( "short".equalsIgnoreCase(this.timeStyle) ) { 340 timeStyle = DateFormat.SHORT; 341 } else if ( "long".equalsIgnoreCase(this.timeStyle) ) { 342 timeStyle = DateFormat.LONG; 343 } else if ( "full".equalsIgnoreCase(this.timeStyle) ) { 344 timeStyle = DateFormat.FULL; 345 } 346 347 348 if ( !"date".equalsIgnoreCase(type) && !"time".equalsIgnoreCase(type) && !"datetime".equalsIgnoreCase(type) ) { 349 type = "date"; 350 } 351 352 Locale locale = null; 353 TagUtils tagUtils = TagUtils.getInstance(); 354 if ( this.locale == null ) { 355 locale = (Locale ) pageContext.getAttribute(Globals.LOCALE_KEY, PageContext.SESSION_SCOPE); 356 } else if ( this.locale instanceof Locale ) { 357 locale = (Locale ) this.locale; 358 } else if ( this.locale instanceof String ) { 359 String localeIdentifier = (String ) this.locale; 360 String [] localeElements = localeIdentifier.split("_"); 361 if ( localeElements.length >= 1 ) { 362 String language = localeElements[0]; 363 if ( localeElements.length >= 2 ) { 364 String country = localeElements[1]; 365 if ( localeElements.length >= 3 ) { 366 String variant = localeElements[2]; 367 locale = new Locale (language, country, variant); 368 } else { 369 locale = new Locale (language, country); 370 } 371 } else { 372 locale = new Locale (language); 373 } 374 } 375 } else { 376 JspTagException e = new JspTagException ("Only instances of java.lang.String and java.util.Locale are supported in 'locale' attribute"); 377 tagUtils.saveException(pageContext, e); 378 throw e; 379 } 380 381 String result = new String (); 382 if ( value != null ) { 383 Date date = null; 384 if ( value instanceof String ) { 385 String s = (String ) value; 386 if ( s.equalsIgnoreCase("now") ) { 387 date = new Date (); 388 } else if ( s.equalsIgnoreCase("epoch") ) { 389 date = new Date (0); 390 } else { 391 JspTagException e = new JspTagException ("Can't produce java.util.Date object for string \"" + s + "\""); 392 tagUtils.saveException(pageContext, e); 393 throw e; 394 } 395 } else if ( value instanceof Date ) { 396 date = (Date ) value; 397 } else if ( value instanceof Calendar ) { 398 date = ((Calendar ) value).getTime(); 399 } else { 400 JspTagException e = new JspTagException ("Instances of class " + value.getClass().getName() + " are not supported by this tag"); 401 tagUtils.saveException(pageContext, e); 402 throw e; 403 } 404 if ( "date".equalsIgnoreCase(type) ) { 405 result = DateUtil.formatDate(date, locale, dateStyle); 406 } else if ( "time".equalsIgnoreCase(type) ) { 407 result = DateUtil.formatTime(date, locale, timeStyle); 408 } else { 409 result = DateUtil.formatDateTime(date, locale, dateStyle, timeStyle); 410 } 411 } 412 413 if ( var != null ) { 414 int varScope = PageContext.PAGE_SCOPE; 415 if ( scope != null ) { 416 varScope = tagUtils.getScope(scope); 417 } 418 pageContext.setAttribute(var, result, varScope); 419 } else { 420 tagUtils.write(pageContext, result); 421 } 422 } 423 424 } 425 | Popular Tags |