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.ParseException ; 41 import java.text.SimpleDateFormat ; 42 import java.util.Locale ; 43 import java.util.TimeZone ; 44 45 48 public class FmtParseDateTag extends BodyTagSupport { 49 private static L10N L = new L10N(FmtParseDateTag.class); 50 51 private String _value; 52 53 private String _type; 54 private String _dateStyle; 55 private String _timeStyle; 56 57 private Object _parseLocale; 58 59 private String _pattern; 60 private Object _timeZone; 61 62 private String _var; 63 private String _scope; 64 65 70 public void setValue(String value) 71 { 72 _value = value; 73 } 74 75 80 public void setType(String type) 81 { 82 _type = type; 83 } 84 85 90 public void setDateStyle(String style) 91 { 92 _dateStyle = style; 93 } 94 95 100 public void setTimeStyle(String style) 101 { 102 _timeStyle = style; 103 } 104 105 110 public void setPattern(String pattern) 111 { 112 _pattern = pattern; 113 } 114 115 120 public void setTimeZone(Object zone) 121 { 122 _timeZone = zone; 123 } 124 125 130 public void setParseLocale(Object locale) 131 { 132 _parseLocale = locale; 133 } 134 135 140 public void setVar(String var) 141 { 142 _var = var; 143 } 144 145 150 public void setScope(String scope) 151 { 152 _scope = scope; 153 } 154 155 158 public int doEndTag() 159 throws JspException 160 { 161 try { 162 PageContextImpl pc = (PageContextImpl) pageContext; 163 164 JspWriter out = pc.getOut(); 165 166 String string; 167 168 if (_value != null) 169 string = _value; 170 else 171 string = bodyContent.getString().trim(); 172 173 DateFormat format = getFormat(); 174 175 Object value = format.parse(string); 176 177 if (_var == null) 178 out.print(value); 179 else 180 CoreSetTag.setValue(pageContext, _var, _scope, value); 181 } catch (IOException e) { 182 } catch (ParseException e) { 183 throw new JspException (e); 184 } 185 186 return EVAL_PAGE; 187 } 188 189 protected DateFormat getFormat() 190 throws JspException 191 { 192 PageContextImpl pc = (PageContextImpl) pageContext; 193 194 DateFormat format = null; 195 Locale locale = null; 196 197 if (_parseLocale != null) { 198 Object localeObj = _parseLocale; 199 200 if (localeObj instanceof Locale ) 201 locale = (Locale ) localeObj; 202 else if (localeObj instanceof String ) 203 locale = pc.getLocale((String ) localeObj, null); 204 } 205 206 if (locale == null) 207 locale = pc.getLocale(); 208 209 int dateStyle = DateFormat.DEFAULT; 210 if (_dateStyle != null) 211 dateStyle = getDateStyle(_dateStyle); 212 213 int timeStyle = DateFormat.DEFAULT; 214 if (_timeStyle != null) 215 timeStyle = getDateStyle(_timeStyle); 216 217 if (locale != null) { 218 if (_type == null || _type.equals("date")) 219 format = DateFormat.getDateInstance(dateStyle, locale); 220 else if (_type.equals("both")) 221 format = DateFormat.getDateTimeInstance(dateStyle, 222 timeStyle, 223 locale); 224 else if (_type.equals("time")) 225 format = DateFormat.getTimeInstance(timeStyle, locale); 226 else 227 throw new JspException (L.l("illegal type `{0}'", _type)); 228 } 229 else { 230 if (_type == null || _type.equals("date")) 231 format = DateFormat.getDateInstance(dateStyle); 232 else if (_type.equals("both")) 233 format = DateFormat.getDateTimeInstance(dateStyle, timeStyle); 234 else if (_type.equals("time")) 235 format = DateFormat.getTimeInstance(timeStyle); 236 else 237 throw new JspException (L.l("illegal type `{0}'", _type)); 238 } 239 240 if (format == null) 241 return null; 242 243 if (_pattern != null) { 244 try { 245 ((SimpleDateFormat ) format).applyPattern(_pattern); 246 } catch (ClassCastException e) { 247 format = new SimpleDateFormat (_pattern, locale); 248 } 249 } 250 251 TimeZone timeZone = getTimeZone(_timeZone); 252 253 if (timeZone == null) 254 timeZone = (TimeZone ) pageContext.getAttribute("com.caucho.time-zone"); 255 256 if (timeZone == null) 257 timeZone = getTimeZone(Config.find(pageContext, Config.FMT_TIME_ZONE)); 258 259 if (timeZone != null) 260 format.setTimeZone(timeZone); 261 262 return format; 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 | Popular Tags |