1 16 17 package org.apache.taglibs.standard.tag.common.fmt; 18 19 import java.io.IOException ; 20 import java.text.DateFormat ; 21 import java.text.ParseException ; 22 import java.text.SimpleDateFormat ; 23 import java.util.Date ; 24 import java.util.Locale ; 25 import java.util.TimeZone ; 26 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.JspTagException ; 29 import javax.servlet.jsp.PageContext ; 30 import javax.servlet.jsp.tagext.BodyTagSupport ; 31 32 import org.apache.taglibs.standard.resources.Resources; 33 import org.apache.taglibs.standard.tag.common.core.Util; 34 35 41 42 public abstract class ParseDateSupport extends BodyTagSupport { 43 44 47 private static final String DATE = "date"; 48 private static final String TIME = "time"; 49 private static final String DATETIME = "both"; 50 51 52 55 protected String value; protected boolean valueSpecified; protected String type; protected String pattern; protected Object timeZone; protected Locale parseLocale; protected String dateStyle; protected String timeStyle; 64 65 68 private String var; private int scope; 71 72 75 public ParseDateSupport() { 76 super(); 77 init(); 78 } 79 80 private void init() { 81 type = dateStyle = timeStyle = null; 82 value = pattern = var = null; 83 valueSpecified = false; 84 timeZone = null; 85 scope = PageContext.PAGE_SCOPE; 86 parseLocale = null; 87 } 88 89 90 93 public void setVar(String var) { 94 this.var = var; 95 } 96 97 public void setScope(String scope) { 98 this.scope = Util.getScope(scope); 99 } 100 101 102 105 public int doEndTag() throws JspException { 106 107 String input = null; 108 109 if (valueSpecified) { 111 input = value; 113 } else { 114 if (bodyContent != null && bodyContent.getString() != null) 116 input = bodyContent.getString().trim(); 117 } 118 119 if ((input == null) || input.equals("")) { 120 if (var != null) { 121 pageContext.removeAttribute(var, scope); 122 } 123 return EVAL_PAGE; 124 } 125 126 130 Locale locale = parseLocale; 131 if (locale == null) 132 locale = SetLocaleSupport.getFormattingLocale( 133 pageContext, 134 this, 135 false, 136 DateFormat.getAvailableLocales()); 137 if (locale == null) { 138 throw new JspException ( 139 Resources.getMessage("PARSE_DATE_NO_PARSE_LOCALE")); 140 } 141 142 DateFormat parser = createParser(locale); 144 145 if (pattern != null) { 147 try { 148 ((SimpleDateFormat ) parser).applyPattern(pattern); 149 } catch (ClassCastException cce) { 150 parser = new SimpleDateFormat (pattern, locale); 151 } 152 } 153 154 TimeZone tz = null; 156 if ((timeZone instanceof String ) && ((String ) timeZone).equals("")) { 157 timeZone = null; 158 } 159 if (timeZone != null) { 160 if (timeZone instanceof String ) { 161 tz = TimeZone.getTimeZone((String ) timeZone); 162 } else if (timeZone instanceof TimeZone ) { 163 tz = (TimeZone ) timeZone; 164 } else { 165 throw new JspException ( 166 Resources.getMessage("PARSE_DATE_BAD_TIMEZONE")); 167 } 168 } else { 169 tz = TimeZoneSupport.getTimeZone(pageContext, this); 170 } 171 if (tz != null) { 172 parser.setTimeZone(tz); 173 } 174 175 Date parsed = null; 177 try { 178 parsed = parser.parse(input); 179 } catch (ParseException pe) { 180 throw new JspException ( 181 Resources.getMessage("PARSE_DATE_PARSE_ERROR", input), 182 pe); 183 } 184 185 if (var != null) { 186 pageContext.setAttribute(var, parsed, scope); 187 } else { 188 try { 189 pageContext.getOut().print(parsed); 190 } catch (IOException ioe) { 191 throw new JspTagException (ioe.toString(), ioe); 192 } 193 } 194 195 return EVAL_PAGE; 196 } 197 198 public void release() { 200 init(); 201 } 202 203 204 207 private DateFormat createParser(Locale loc) throws JspException { 208 DateFormat parser = null; 209 210 if ((type == null) || DATE.equalsIgnoreCase(type)) { 211 parser = DateFormat.getDateInstance( 212 Util.getStyle(dateStyle, "PARSE_DATE_INVALID_DATE_STYLE"), 213 loc); 214 } else if (TIME.equalsIgnoreCase(type)) { 215 parser = DateFormat.getTimeInstance( 216 Util.getStyle(timeStyle, "PARSE_DATE_INVALID_TIME_STYLE"), 217 loc); 218 } else if (DATETIME.equalsIgnoreCase(type)) { 219 parser = DateFormat.getDateTimeInstance( 220 Util.getStyle(dateStyle, "PARSE_DATE_INVALID_DATE_STYLE"), 221 Util.getStyle(timeStyle, "PARSE_DATE_INVALID_TIME_STYLE"), 222 loc); 223 } else { 224 throw new JspException ( 225 Resources.getMessage("PARSE_DATE_INVALID_TYPE", type)); 226 } 227 228 parser.setLenient(false); 229 230 return parser; 231 } 232 } 233 | Popular Tags |