1 16 17 package org.apache.taglibs.datetime; 18 19 import java.util.*; 20 import javax.servlet.*; 21 import javax.servlet.http.*; 22 import javax.servlet.jsp.*; 23 import javax.servlet.jsp.tagext.*; 24 25 76 77 public class TimeZonesTag extends BodyTagSupport 78 { 79 private boolean locale_flag = false; 81 private String localeRef = null; 82 private String style_string = "SHORT"; 83 84 private int style = TimeZone.SHORT; 86 private String [] timeZones = null; 87 private TimeZone timeZone = null; 88 private int zone_num = 0; 89 90 95 public final int doStartTag() throws JspException 96 { 97 zone_num = 0; 99 100 if( style_string == null || style_string.equals("SHORT") ) 101 style = TimeZone.SHORT; 102 else if( style_string.equals("LONG") ) 103 style = TimeZone.LONG; 104 else 105 throw new JspTagException( 106 "Datetime tag timeZones style attribute must be set to" + 107 " either SHORT or LONG"); 108 timeZones = TimeZone.getAvailableIDs(); 109 110 timeZone = TimeZone.getTimeZone(timeZones[zone_num]); 111 if( timeZone == null ) 112 return SKIP_BODY; 113 114 pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE); 115 return EVAL_BODY_TAG; 116 } 117 118 123 public final int doAfterBody() throws JspException 124 { 125 zone_num++; 127 if( zone_num >= timeZones.length ) 128 return SKIP_BODY; 129 130 timeZone = TimeZone.getTimeZone(timeZones[zone_num]); 132 if( timeZone == null ) 133 return SKIP_BODY; 134 135 return EVAL_BODY_TAG; 136 } 137 138 143 public final int doEndTag() throws JspException 144 { 145 pageContext.removeAttribute(id,PageContext.PAGE_SCOPE); 146 try 147 { 148 if(bodyContent != null) 149 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 150 } catch(java.io.IOException e) 151 { 152 throw new JspException("IO Error: " + e.getMessage()); 153 } 154 return EVAL_PAGE; 155 } 156 157 163 public final void setLocale(boolean flag) 164 { 165 locale_flag = flag; 166 } 167 168 174 public void setLocaleRef(String value) 175 { 176 localeRef = value; 177 } 178 179 184 public final void setStyle(String str) 185 { 186 style_string = str; 187 } 188 189 196 public final String getDisplayName() throws JspException 197 { 198 String dn = null; 199 Date now = new Date(); 200 boolean daylight = false; 201 202 if( timeZone.useDaylightTime() ) 203 daylight = timeZone.inDaylightTime(now); 204 205 if( localeRef != null ) { 206 Locale locale = (Locale)pageContext.findAttribute(localeRef); 207 if( locale == null ) { 208 throw new JspException( 209 "datetime amPms tag could not find locale for localeRef \"" + 210 localeRef + "\"."); 211 } 212 dn = timeZone.getDisplayName(daylight,style,locale); 213 } else if( locale_flag) { 214 dn = timeZone.getDisplayName(daylight,style, 215 (Locale)pageContext.getRequest().getLocale()); 216 } else { 217 dn = timeZone.getDisplayName(daylight, style); 218 } 219 220 if( dn == null ) 221 dn = ""; 222 223 return dn; 224 } 225 226 233 public final String getZoneId() 234 { 235 return timeZone.getID(); 236 } 237 238 } 239 | Popular Tags |