001: package com.technoetic.xplanner.tags;
002:
003: import org.apache.struts.util.RequestUtils;
004: import org.apache.struts.util.ResponseUtils;
005:
006: import javax.servlet.jsp.JspException;
007: import javax.servlet.jsp.tagext.TagSupport;
008: import java.text.DateFormat;
009: import java.text.SimpleDateFormat;
010: import java.util.Date;
011: import java.util.HashMap;
012:
013: public class FormatDateTag extends TagSupport {
014: private static HashMap dateFormatters = new HashMap();
015: private final static String DEFAULT_DATE_FORMAT = "EEE MMM dd k:mm:ss z";
016: private String property;
017: private String scope;
018: private String name;
019: private String format;
020: private String formatKey;
021: private String locale;
022: private Date value;
023: private String bundle;
024:
025: public int doStartTag() throws JspException {
026: Object object = null;
027: if (value == null) {
028: object = RequestUtils.lookup(pageContext, name, property,
029: scope);
030: } else {
031: object = value;
032: }
033:
034: if (object == null) {
035: return (SKIP_BODY); // Nothing to output
036: }
037:
038: Date dateValue = null;
039: if (object instanceof Date) {
040: dateValue = (Date) object;
041: } else {
042: throw new JspException(
043: "date value must be a java.util.Date");
044: }
045:
046: String dateFormat = null;
047: if (format != null) {
048: dateFormat = format;
049: } else if (formatKey != null) {
050: dateFormat = RequestUtils.message(pageContext, bundle,
051: locale, formatKey);
052: } else {
053: dateFormat = DEFAULT_DATE_FORMAT;
054: }
055:
056: DateFormat dateFormatter = (DateFormat) dateFormatters
057: .get(dateFormat);
058: if (dateFormatter == null) {
059: dateFormatter = new SimpleDateFormat(dateFormat,
060: pageContext.getRequest().getLocale());
061: // This should really be keyed off of both format and locale
062: dateFormatters.put(dateFormat, dateFormatter);
063: }
064:
065: ResponseUtils.write(pageContext, dateFormatter
066: .format(dateValue));
067:
068: return (SKIP_BODY);
069: }
070:
071: public void setProperty(String property) {
072: this .property = property;
073: }
074:
075: public String getProperty() {
076: return property;
077: }
078:
079: public void setScope(String scope) {
080: this .scope = scope;
081: }
082:
083: public String getScope() {
084: return scope;
085: }
086:
087: public void setName(String name) {
088: this .name = name;
089: }
090:
091: public String getName() {
092: return name;
093: }
094:
095: public void setFormat(String format) {
096: this .format = format;
097: }
098:
099: public String getFormat() {
100: return format;
101: }
102:
103: public void setFormatKey(String formatKey) {
104: this .formatKey = formatKey;
105: }
106:
107: public String getFormatKey() {
108: return formatKey;
109: }
110:
111: public void setValue(java.util.Date value) {
112: this .value = value;
113: }
114:
115: public java.util.Date getValue() {
116: return value;
117: }
118:
119: public void setLocale(String locale) {
120: this .locale = locale;
121: }
122:
123: public void setBundle(String bundle) {
124: this .bundle = bundle;
125: }
126:
127: public String getBundle() {
128: return bundle;
129: }
130: }
|