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.SimpleDateFormat ; 22 import java.util.Date ; 23 import java.util.Locale ; 24 import java.util.TimeZone ; 25 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspTagException ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.tagext.TagSupport ; 30 31 import org.apache.taglibs.standard.resources.Resources; 32 import org.apache.taglibs.standard.tag.common.core.Util; 33 34 40 41 public abstract class FormatDateSupport extends TagSupport { 42 43 46 private static final String DATE = "date"; 47 private static final String TIME = "time"; 48 private static final String DATETIME = "both"; 49 50 51 54 protected Date value; protected String type; protected String pattern; protected Object timeZone; protected String dateStyle; protected String timeStyle; 61 62 65 private String var; private int scope; 68 69 72 public FormatDateSupport() { 73 super(); 74 init(); 75 } 76 77 private void init() { 78 type = dateStyle = timeStyle = null; 79 pattern = var = null; 80 value = null; 81 timeZone = null; 82 scope = PageContext.PAGE_SCOPE; 83 } 84 85 86 89 public void setVar(String var) { 90 this.var = var; 91 } 92 93 public void setScope(String scope) { 94 this.scope = Util.getScope(scope); 95 } 96 97 98 101 104 public int doEndTag() throws JspException { 105 106 String formatted = null; 107 108 if (value == null) { 109 if (var != null) { 110 pageContext.removeAttribute(var, scope); 111 } 112 return EVAL_PAGE; 113 } 114 115 Locale locale = SetLocaleSupport.getFormattingLocale( 117 pageContext, 118 this, 119 true, 120 DateFormat.getAvailableLocales()); 121 122 if (locale != null) { 123 DateFormat formatter = createFormatter(locale); 124 125 if (pattern != null) { 127 try { 128 ((SimpleDateFormat ) formatter).applyPattern(pattern); 129 } catch (ClassCastException cce) { 130 formatter = new SimpleDateFormat (pattern, locale); 131 } 132 } 133 134 TimeZone tz = null; 136 if ((timeZone instanceof String ) 137 && ((String ) timeZone).equals("")) { 138 timeZone = null; 139 } 140 if (timeZone != null) { 141 if (timeZone instanceof String ) { 142 tz = TimeZone.getTimeZone((String ) timeZone); 143 } else if (timeZone instanceof TimeZone ) { 144 tz = (TimeZone ) timeZone; 145 } else { 146 throw new JspTagException ( 147 Resources.getMessage("FORMAT_DATE_BAD_TIMEZONE")); 148 } 149 } else { 150 tz = TimeZoneSupport.getTimeZone(pageContext, this); 151 } 152 if (tz != null) { 153 formatter.setTimeZone(tz); 154 } 155 formatted = formatter.format(value); 156 } else { 157 formatted = value.toString(); 159 } 160 161 if (var != null) { 162 pageContext.setAttribute(var, formatted, scope); 163 } else { 164 try { 165 pageContext.getOut().print(formatted); 166 } catch (IOException ioe) { 167 throw new JspTagException (ioe.toString(), ioe); 168 } 169 } 170 171 return EVAL_PAGE; 172 } 173 174 public void release() { 176 init(); 177 } 178 179 180 183 private DateFormat createFormatter(Locale loc) throws JspException { 184 DateFormat formatter = null; 185 186 if ((type == null) || DATE.equalsIgnoreCase(type)) { 187 formatter = DateFormat.getDateInstance( 188 Util.getStyle(dateStyle, "FORMAT_DATE_INVALID_DATE_STYLE"), 189 loc); 190 } else if (TIME.equalsIgnoreCase(type)) { 191 formatter = DateFormat.getTimeInstance( 192 Util.getStyle(timeStyle, "FORMAT_DATE_INVALID_TIME_STYLE"), 193 loc); 194 } else if (DATETIME.equalsIgnoreCase(type)) { 195 formatter = DateFormat.getDateTimeInstance( 196 Util.getStyle(dateStyle, "FORMAT_DATE_INVALID_DATE_STYLE"), 197 Util.getStyle(timeStyle, "FORMAT_DATE_INVALID_TIME_STYLE"), 198 loc); 199 } else { 200 throw new JspException ( 201 Resources.getMessage("FORMAT_DATE_INVALID_TYPE", 202 type)); 203 } 204 205 return formatter; 206 } 207 } 208 | Popular Tags |