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 69 70 public class AmPmsTag extends BodyTagSupport 71 { 72 private static String PATTERN = "yyyy"; 74 75 private boolean locale_flag = false; 77 private String localeRef = null; 78 79 private String [] amPms = null; 81 private int count = 0; 82 83 88 public final int doStartTag() throws JspException 89 { 90 count = 0; 92 93 SimpleDateFormat sdf; 94 if( localeRef != null ) { 96 Locale locale = (Locale)pageContext.findAttribute(localeRef); 97 if( locale == null ) { 98 throw new JspException( 99 "datetime amPms tag could not find locale for localeRef \"" + 100 localeRef + "\"."); 101 } 102 103 sdf = new SimpleDateFormat(PATTERN,locale); 104 } else if( locale_flag ) { 105 sdf = new SimpleDateFormat(PATTERN, 106 (Locale)pageContext.getRequest().getLocale()); 107 } else { 108 sdf = new SimpleDateFormat(PATTERN); 109 } 110 111 DateFormatSymbols dfs = sdf.getDateFormatSymbols(); 112 amPms = dfs.getAmPmStrings(); 113 while( count < amPms.length && 115 (amPms[count] == null || amPms[count].length() == 0) ) 116 count++; 117 if( count >= amPms.length ) 118 return SKIP_BODY; 119 120 pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE); 121 return EVAL_BODY_TAG; 122 } 123 124 129 public final int doAfterBody() throws JspException 130 { 131 count++; 133 if( count >= amPms.length ) 134 return SKIP_BODY; 135 while( count < amPms.length && 137 (amPms[count] == null || amPms[count].length() == 0) ) 138 count++; 139 140 if( count >= amPms.length ) 141 return SKIP_BODY; 142 143 return EVAL_BODY_TAG; 145 } 146 147 151 public final int doEndTag() throws JspException 152 { 153 pageContext.removeAttribute(id,PageContext.PAGE_SCOPE); 154 try 155 { 156 if(bodyContent != null) 157 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 158 } catch(java.io.IOException e) 159 { 160 throw new JspException("IO Error: " + e.getMessage()); 161 } 162 return EVAL_PAGE; 163 } 164 165 171 public final void setLocale(boolean flag) 172 { 173 locale_flag = flag; 174 } 175 176 182 public void setLocaleRef(String value) 183 { 184 localeRef = value; 185 } 186 187 194 public final String getName() 195 { 196 return amPms[count]; 197 } 198 199 } 200 | Popular Tags |