1 28 29 package com.caucho.jstl.rt; 30 31 import com.caucho.jsp.PageContextImpl; 32 import com.caucho.util.L10N; 33 34 import javax.servlet.jsp.JspException ; 35 import javax.servlet.jsp.JspWriter ; 36 import javax.servlet.jsp.jstl.core.Config; 37 import javax.servlet.jsp.tagext.BodyTagSupport ; 38 import java.io.IOException ; 39 import java.text.DateFormat ; 40 import java.text.SimpleDateFormat ; 41 import java.util.Date ; 42 import java.util.Locale ; 43 import java.util.TimeZone ; 44 45 48 public class FormatDateTag extends BodyTagSupport { 49 private static L10N L = new L10N(FormatDateTag.class); 50 51 private Date _value; 52 53 private String _type; 54 private String _dateStyle; 55 private String _timeStyle; 56 57 private String _pattern; 58 private Object _timeZone; 59 60 private String _var; 61 private String _scope; 62 63 68 public void setValue(Object value) 69 { 70 if (value instanceof Number ) 71 _value = new Date (((Number ) value).longValue()); 72 else 73 _value = (Date ) value; 74 } 75 76 81 public void setType(String type) 82 { 83 _type = type; 84 } 85 86 91 public void setDateStyle(String style) 92 { 93 _dateStyle = style; 94 } 95 96 101 public void setTimeStyle(String style) 102 { 103 _timeStyle = style; 104 } 105 106 111 public void setPattern(String pattern) 112 { 113 _pattern = pattern; 114 } 115 116 121 public void setTimeZone(Object zone) 122 { 123 _timeZone = zone; 124 } 125 126 131 public void setVar(String var) 132 { 133 _var = var; 134 } 135 136 141 public void setScope(String scope) 142 { 143 _scope = scope; 144 } 145 146 149 public int doEndTag() 150 throws JspException 151 { 152 try { 153 PageContextImpl pc = (PageContextImpl) pageContext; 154 155 JspWriter out = pc.getOut(); 156 157 if (_value == null) { 158 if (_var != null) 159 CoreSetTag.setValue(pageContext, _var, _scope, null); 160 161 return EVAL_PAGE; 162 } 163 164 long time = _value.getTime(); 165 166 DateFormat format = null; 167 168 Locale locale = pc.getLocale(); 169 170 int dateStyle = DateFormat.DEFAULT; 171 if (_dateStyle != null) 172 dateStyle = getDateStyle(_dateStyle); 173 174 int timeStyle = DateFormat.DEFAULT; 175 if (_timeStyle != null) 176 timeStyle = getDateStyle(_timeStyle); 177 178 if (locale != null) { 179 if (_type == null || _type.equals("date")) 180 format = DateFormat.getDateInstance(dateStyle, locale); 181 else if (_type.equals("both")) 182 format = DateFormat.getDateTimeInstance(dateStyle, 183 timeStyle, 184 locale); 185 else if (_type.equals("time")) 186 format = DateFormat.getTimeInstance(timeStyle, locale); 187 else 188 throw new JspException (L.l("illegal type `{0}'", _type)); 189 } 190 else { 191 if (_type == null || _type.equals("date")) 192 format = DateFormat.getDateInstance(dateStyle); 193 else if (_type.equals("both")) 194 format = DateFormat.getDateTimeInstance(dateStyle, timeStyle); 195 else if (_type.equals("time")) 196 format = DateFormat.getTimeInstance(timeStyle); 197 else 198 throw new JspException (L.l("illegal type `{0}'", _type)); 199 } 200 201 if (format != null && _pattern != null) { 202 try { 203 ((SimpleDateFormat ) format).applyPattern(_pattern); 204 } catch (ClassCastException e) { 205 format = new SimpleDateFormat (_pattern, locale); 206 } 207 } 208 209 if (format != null) { 210 TimeZone timeZone = getTimeZone(_timeZone); 211 212 if (timeZone == null) 213 timeZone = (TimeZone ) pageContext.getAttribute("com.caucho.time-zone"); 214 215 if (timeZone == null) 216 timeZone = getTimeZone(Config.find(pageContext, Config.FMT_TIME_ZONE)); 217 218 if (timeZone != null) 219 format.setTimeZone(timeZone); 220 } 221 222 Object value = _value; 223 if (format != null) 224 value = format.format(new Date (time)); 225 226 if (_var == null) 227 out.print(value); 228 else 229 CoreSetTag.setValue(pageContext, _var, _scope, value); 230 } catch (IOException e) { 231 } 232 233 return EVAL_PAGE; 234 } 235 236 private TimeZone getTimeZone(Object timeZoneObj) 237 { 238 if (timeZoneObj instanceof TimeZone ) 239 return (TimeZone ) timeZoneObj; 240 else if (timeZoneObj instanceof String ) { 241 String timeZoneString = (String ) timeZoneObj; 242 243 return TimeZone.getTimeZone(timeZoneString); 244 } 245 246 return null; 247 } 248 249 public static int getDateStyle(String style) 250 throws JspException 251 { 252 if (style == null || style.equals("default")) 253 return DateFormat.DEFAULT; 254 else if (style.equals("short")) 255 return DateFormat.SHORT; 256 else if (style.equals("medium")) 257 return DateFormat.MEDIUM; 258 else if (style.equals("long")) 259 return DateFormat.LONG; 260 else if (style.equals("full")) 261 return DateFormat.FULL; 262 else 263 throw new JspException (L.l("illegal date style `{0}'", style)); 264 } 265 } 266 | Popular Tags |