1 16 17 package org.apache.taglibs.datetime; 18 19 import java.util.*; 20 import java.text.*; 21 import javax.servlet.*; 22 import javax.servlet.http.*; 23 import javax.servlet.jsp.*; 24 import javax.servlet.jsp.tagext.*; 25 26 83 84 public class ParseTag extends BodyTagSupport 85 { 86 private boolean locale_flag = false; 88 private String pattern = null; 90 private String patternid = null; 92 private String timeZone_string; 95 private String localeRef = null; 97 98 private TimeZone timeZone = null; 100 private Date date = null; 102 103 108 public final int doStartTag() throws JspException 109 { 110 return EVAL_BODY_TAG; 111 } 112 113 118 public final int doAfterBody() throws JspException 119 { 120 BodyContent body = getBodyContent(); 122 String s = body.getString().trim(); 123 body.clearBody(); 125 126 SimpleDateFormat sdf; 128 String pat = pattern; 129 130 if( pat == null && patternid != null ) { 131 Object attr = pageContext.findAttribute(patternid); 132 if( attr != null ) 133 pat = attr.toString(); 134 } 135 136 if( pat == null ) { 137 sdf = new SimpleDateFormat(); 138 pat = sdf.toPattern(); 139 } 140 141 if( localeRef != null ) { 143 Locale locale = (Locale)pageContext.findAttribute(localeRef); 144 if( locale == null ) { 145 throw new JspException( 146 "datetime parse tag could not find locale for localeRef \"" + 147 localeRef + "\"."); 148 } 149 150 sdf = new SimpleDateFormat(pat,locale); 151 } else if( locale_flag ) { 152 sdf = new SimpleDateFormat(pat, 153 (Locale)pageContext.getRequest().getLocale()); 154 } else { 155 sdf = new SimpleDateFormat(pat); 156 } 157 158 if( timeZone_string != null ) { 160 timeZone = 161 (TimeZone)pageContext.getAttribute(timeZone_string, 162 PageContext.SESSION_SCOPE); 163 if( timeZone == null ) 164 throw new JspTagException("Datetime parse tag timeZone " + 165 "script variable \"" + timeZone_string + 166 " \" does not exist"); 167 sdf.setTimeZone(timeZone); 168 } 169 170 try { 172 date = null; 173 date = sdf.parse(s); 174 } catch(ParseException e) { 175 } 176 return SKIP_BODY; 177 } 178 179 184 public final int doEndTag() throws JspException 185 { 186 long time = 0; 187 if( date != null ) 188 time = date.getTime(); 189 try { 190 pageContext.getOut().write("" + time); 191 } catch(Exception e) { 192 throw new JspException("IO Error: " + e.getMessage()); 193 } 194 195 return EVAL_PAGE; 196 } 197 198 204 public final void setLocale(boolean flag) 205 { 206 locale_flag = flag; 207 } 208 209 217 public final void setTimeZone(String tz) 218 { 219 timeZone_string = tz; 220 } 221 222 227 public final void setPattern(String str) 228 { 229 pattern = str; 230 } 231 232 238 public final void setPatternId(String str) 239 { 240 patternid = str; 241 } 242 243 249 public void setLocaleRef(String value) 250 { 251 localeRef = value; 252 } 253 254 } 255 | Popular Tags |