1 28 29 package com.caucho.jstl.el; 30 31 import com.caucho.el.Expr; 32 import com.caucho.jsp.PageContextImpl; 33 import com.caucho.util.L10N; 34 35 import javax.el.ELContext; 36 import javax.el.ELException; 37 import javax.servlet.jsp.JspException ; 38 import javax.servlet.jsp.JspWriter ; 39 import javax.servlet.jsp.jstl.core.Config; 40 import javax.servlet.jsp.tagext.BodyTagSupport ; 41 import java.text.DateFormat ; 42 import java.text.SimpleDateFormat ; 43 import java.util.Date ; 44 import java.util.Locale ; 45 import java.util.TimeZone ; 46 47 50 public class FormatDateTag extends BodyTagSupport { 51 private static L10N L = new L10N(FormatDateTag.class); 52 53 private Expr _valueExpr; 54 55 private Expr _typeExpr; 56 private Expr _dateStyleExpr; 57 private Expr _timeStyleExpr; 58 59 private Expr _patternExpr; 60 private Expr _timeZoneExpr; 61 62 private String _var; 63 private String _scope; 64 65 70 public void setValue(Expr value) 71 { 72 _valueExpr = value; 73 } 74 75 80 public void setType(Expr type) 81 { 82 _typeExpr = type; 83 } 84 85 90 public void setDateStyle(Expr style) 91 { 92 _dateStyleExpr = style; 93 } 94 95 100 public void setTimeStyle(Expr style) 101 { 102 _timeStyleExpr = style; 103 } 104 105 110 public void setPattern(Expr pattern) 111 { 112 _patternExpr = pattern; 113 } 114 115 120 public void setTimeZone(Expr zone) 121 { 122 _timeZoneExpr = zone; 123 } 124 125 130 public void setVar(String var) 131 { 132 _var = var; 133 } 134 135 140 public void setScope(String scope) 141 { 142 _scope = scope; 143 } 144 145 148 public int doEndTag() 149 throws JspException 150 { 151 try { 152 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 153 154 JspWriter out = pageContext.getOut(); 155 156 Object value = _valueExpr.evalObject(pageContext.getELContext()); 157 158 if (value == null) { 159 if (_var != null) 160 CoreSetTag.setValue(pageContext, _var, _scope, null); 161 162 return EVAL_PAGE; 163 } 164 165 long time = 0; 166 167 if (value instanceof Number ) 168 time = ((Number ) value).longValue(); 169 else if (value instanceof Date ) 170 time = ((Date ) value).getTime(); 171 172 DateFormat format = null; 173 174 Locale locale = pageContext.getLocale(); 175 176 String type = null; 177 178 ELContext env = pageContext.getELContext(); 179 180 if (_typeExpr != null) 181 type = _typeExpr.evalString(env); 182 183 int dateStyle = DateFormat.DEFAULT; 184 if (_dateStyleExpr != null) 185 dateStyle = getDateStyle(_dateStyleExpr.evalString(env)); 186 187 int timeStyle = DateFormat.DEFAULT; 188 if (_timeStyleExpr != null) 189 timeStyle = getDateStyle(_timeStyleExpr.evalString(env)); 190 191 if (locale != null) { 192 if (type == null || type.equals("date")) 193 format = DateFormat.getDateInstance(dateStyle, locale); 194 else if (type.equals("both")) 195 format = DateFormat.getDateTimeInstance(dateStyle, 196 timeStyle, 197 locale); 198 else if (type.equals("time")) 199 format = DateFormat.getTimeInstance(timeStyle, locale); 200 else 201 throw new JspException (L.l("illegal type `{0}'", type)); 202 } 203 else { 204 if (type == null || type.equals("date")) 205 format = DateFormat.getDateInstance(dateStyle); 206 else if (type.equals("both")) 207 format = DateFormat.getDateTimeInstance(dateStyle, timeStyle); 208 else if (type.equals("time")) 209 format = DateFormat.getTimeInstance(timeStyle); 210 else 211 throw new JspException (L.l("illegal type `{0}'", type)); 212 } 213 214 if (format != null && _patternExpr != null) { 215 String pattern = _patternExpr.evalString(env); 216 try { 217 ((SimpleDateFormat ) format).applyPattern(pattern); 218 } catch (ClassCastException e) { 219 format = new SimpleDateFormat (pattern, locale); 220 } 221 } 222 223 if (format != null) { 224 TimeZone timeZone = getTimeZone(); 225 if (timeZone != null) 226 format.setTimeZone(timeZone); 227 } 228 229 if (format != null) 230 value = format.format(new Date (time)); 231 232 if (_var == null) 233 out.print(value); 234 else 235 CoreSetTag.setValue(pageContext, _var, _scope, value); 236 } catch (Exception e) { 237 throw new JspException (e); 238 } 239 240 return EVAL_PAGE; 241 } 242 243 private TimeZone getTimeZone() 244 throws ELException 245 { 246 if (_timeZoneExpr != null) { 247 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 248 Object timeZoneObj = _timeZoneExpr.evalObject(pageContext.getELContext()); 249 250 TimeZone zone = getTimeZone(timeZoneObj); 251 if (zone != null) 252 return zone; 253 } 254 255 Object timeZoneObj = pageContext.getAttribute("com.caucho.time-zone"); 256 257 if (timeZoneObj != null) 258 return (TimeZone ) timeZoneObj; 259 260 timeZoneObj = Config.find(pageContext, Config.FMT_TIME_ZONE); 261 262 return getTimeZone(timeZoneObj); 263 } 264 265 private TimeZone getTimeZone(Object timeZoneObj) 266 { 267 if (timeZoneObj instanceof TimeZone ) 268 return (TimeZone ) timeZoneObj; 269 else if (timeZoneObj instanceof String ) { 270 String timeZoneString = (String ) timeZoneObj; 271 272 return TimeZone.getTimeZone(timeZoneString); 273 } 274 275 return null; 276 } 277 278 public static int getDateStyle(String style) 279 throws JspException 280 { 281 if (style == null || style.equals("default")) 282 return DateFormat.DEFAULT; 283 else if (style.equals("short")) 284 return DateFormat.SHORT; 285 else if (style.equals("medium")) 286 return DateFormat.MEDIUM; 287 else if (style.equals("long")) 288 return DateFormat.LONG; 289 else if (style.equals("full")) 290 return DateFormat.FULL; 291 else 292 throw new JspException (L.l("illegal date style `{0}'", style)); 293 } 294 } 295 296 | Popular Tags |