1 29 30 package com.caucho.jstl.el; 31 32 import com.caucho.el.Expr; 33 import com.caucho.jsp.PageContextImpl; 34 import com.caucho.util.L10N; 35 36 import javax.el.ELContext; 37 import javax.el.ELException; 38 import javax.servlet.jsp.JspException ; 39 import javax.servlet.jsp.JspWriter ; 40 import javax.servlet.jsp.jstl.core.Config; 41 import javax.servlet.jsp.tagext.BodyTagSupport ; 42 import java.io.IOException ; 43 import java.text.DateFormat ; 44 import java.text.ParseException ; 45 import java.text.SimpleDateFormat ; 46 import java.util.Locale ; 47 import java.util.TimeZone ; 48 49 52 public class ParseDateTag extends BodyTagSupport { 53 private static L10N L = new L10N(ParseDateTag.class); 54 55 private Expr _valueExpr; 56 57 private Expr _typeExpr; 58 private Expr _dateStyleExpr; 59 private Expr _timeStyleExpr; 60 61 private Expr _parseLocaleExpr; 62 63 private Expr _patternExpr; 64 private Expr _timeZoneExpr; 65 66 private String _var; 67 private String _scope; 68 69 74 public void setValue(Expr value) 75 { 76 _valueExpr = value; 77 } 78 79 84 public void setType(Expr type) 85 { 86 _typeExpr = type; 87 } 88 89 94 public void setDateStyle(Expr style) 95 { 96 _dateStyleExpr = style; 97 } 98 99 104 public void setTimeStyle(Expr style) 105 { 106 _timeStyleExpr = style; 107 } 108 109 114 public void setPattern(Expr pattern) 115 { 116 _patternExpr = pattern; 117 } 118 119 124 public void setTimeZone(Expr zone) 125 { 126 _timeZoneExpr = zone; 127 } 128 129 134 public void setParseLocale(Expr locale) 135 { 136 _parseLocaleExpr = locale; 137 } 138 139 144 public void setVar(String var) 145 { 146 _var = var; 147 } 148 149 154 public void setScope(String scope) 155 { 156 _scope = scope; 157 } 158 159 162 public int doEndTag() 163 throws JspException 164 { 165 try { 166 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 167 168 JspWriter out = pageContext.getOut(); 169 170 String string; 171 172 if (_valueExpr != null) 173 string = _valueExpr.evalString(pageContext.getELContext()); 174 else 175 string = bodyContent.getString().trim(); 176 177 DateFormat format = getFormat(); 178 179 Object value = format.parse(string); 180 181 if (_var == null) 182 out.print(value); 183 else 184 CoreSetTag.setValue(pageContext, _var, _scope, value); 185 } catch (IOException e) { 186 } catch (ParseException e) { 187 throw new JspException (e); 188 } catch (ELException e) { 189 throw new JspException (e); 190 } 191 192 return EVAL_PAGE; 193 } 194 195 protected DateFormat getFormat() 196 throws JspException , ELException 197 { 198 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 199 ELContext env = pageContext.getELContext(); 200 201 DateFormat format = null; 202 Locale locale = null; 203 204 if (_parseLocaleExpr != null) { 205 Object localeObj = _parseLocaleExpr.evalObject(env); 206 207 if (localeObj instanceof Locale ) 208 locale = (Locale ) localeObj; 209 else if (localeObj instanceof String ) 210 locale = pageContext.getLocale((String ) localeObj, null); 211 } 212 213 if (locale == null) 214 locale = pageContext.getLocale(); 215 216 String type = null; 217 218 if (_typeExpr != null) 219 type = _typeExpr.evalString(env); 220 221 int dateStyle = DateFormat.DEFAULT; 222 if (_dateStyleExpr != null) 223 dateStyle = getDateStyle(_dateStyleExpr.evalString(env)); 224 225 int timeStyle = DateFormat.DEFAULT; 226 if (_timeStyleExpr != null) 227 timeStyle = getDateStyle(_timeStyleExpr.evalString(env)); 228 229 if (locale != null) { 230 if (type == null || type.equals("date")) 231 format = DateFormat.getDateInstance(dateStyle, locale); 232 else if (type.equals("both")) 233 format = DateFormat.getDateTimeInstance(dateStyle, 234 timeStyle, 235 locale); 236 else if (type.equals("time")) 237 format = DateFormat.getTimeInstance(timeStyle, locale); 238 else 239 throw new JspException (L.l("illegal type `{0}'", type)); 240 } 241 else { 242 if (type == null || type.equals("date")) 243 format = DateFormat.getDateInstance(dateStyle); 244 else if (type.equals("both")) 245 format = DateFormat.getDateTimeInstance(dateStyle, timeStyle); 246 else if (type.equals("time")) 247 format = DateFormat.getTimeInstance(timeStyle); 248 else 249 throw new JspException (L.l("illegal type `{0}'", type)); 250 } 251 252 if (format == null) 253 return null; 254 255 if (_patternExpr != null) { 256 String pattern = _patternExpr.evalString(env); 257 try { 258 ((SimpleDateFormat ) format).applyPattern(pattern); 259 } catch (ClassCastException e) { 260 format = new SimpleDateFormat (pattern, locale); 261 } 262 } 263 264 TimeZone timeZone = getTimeZone(); 265 if (timeZone != null) 266 format.setTimeZone(timeZone); 267 268 return format; 269 } 270 271 private TimeZone getTimeZone() 272 throws ELException 273 { 274 if (_timeZoneExpr != null) { 275 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 276 Object timeZoneObj = _timeZoneExpr.evalObject(pageContext.getELContext()); 277 278 TimeZone zone = getTimeZone(timeZoneObj); 279 if (zone != null) 280 return zone; 281 } 282 283 Object timeZoneObj = pageContext.getAttribute("com.caucho.time-zone"); 284 285 if (timeZoneObj != null) 286 return (TimeZone ) timeZoneObj; 287 288 timeZoneObj = Config.find(pageContext, Config.FMT_TIME_ZONE); 289 290 return getTimeZone(timeZoneObj); 291 } 292 293 private TimeZone getTimeZone(Object timeZoneObj) 294 { 295 if (timeZoneObj instanceof TimeZone ) 296 return (TimeZone ) timeZoneObj; 297 else if (timeZoneObj instanceof String ) { 298 String timeZoneString = (String ) timeZoneObj; 299 300 return TimeZone.getTimeZone(timeZoneString); 301 } 302 303 return null; 304 } 305 306 public static int getDateStyle(String style) 307 throws JspException 308 { 309 if (style == null || style.equals("default")) 310 return DateFormat.DEFAULT; 311 else if (style.equals("short")) 312 return DateFormat.SHORT; 313 else if (style.equals("medium")) 314 return DateFormat.MEDIUM; 315 else if (style.equals("long")) 316 return DateFormat.LONG; 317 else if (style.equals("full")) 318 return DateFormat.FULL; 319 else 320 throw new JspException (L.l("illegal date style `{0}'", style)); 321 } 322 } 323 | Popular Tags |