1 18 19 package org.apache.struts.taglib.bean; 20 21 import java.math.BigDecimal ; 22 import java.math.BigInteger ; 23 import java.text.DecimalFormat ; 24 import java.text.Format ; 25 import java.text.NumberFormat ; 26 import java.text.SimpleDateFormat ; 27 import java.util.Locale ; 28 29 import javax.servlet.jsp.JspException ; 30 import javax.servlet.jsp.tagext.TagSupport ; 31 32 import org.apache.struts.taglib.TagUtils; 33 import org.apache.struts.util.MessageResources; 34 35 42 public class WriteTag extends TagSupport { 43 44 47 public static final String SQL_TIMESTAMP_FORMAT_KEY = 48 "org.apache.struts.taglib.bean.format.sql.timestamp"; 49 50 53 public static final String SQL_DATE_FORMAT_KEY = 54 "org.apache.struts.taglib.bean.format.sql.date"; 55 56 59 public static final String SQL_TIME_FORMAT_KEY = 60 "org.apache.struts.taglib.bean.format.sql.time"; 61 62 65 public static final String DATE_FORMAT_KEY = 66 "org.apache.struts.taglib.bean.format.date"; 67 68 71 public static final String INT_FORMAT_KEY = 72 "org.apache.struts.taglib.bean.format.int"; 73 74 78 public static final String FLOAT_FORMAT_KEY = 79 "org.apache.struts.taglib.bean.format.float"; 80 81 84 protected static MessageResources messages = 85 MessageResources.getMessageResources( 86 "org.apache.struts.taglib.bean.LocalStrings"); 87 88 90 93 protected boolean filter = true; 94 95 public boolean getFilter() { 96 return (this.filter); 97 } 98 99 public void setFilter(boolean filter) { 100 this.filter = filter; 101 } 102 103 106 protected boolean ignore = false; 107 108 public boolean getIgnore() { 109 return (this.ignore); 110 } 111 112 public void setIgnore(boolean ignore) { 113 this.ignore = ignore; 114 } 115 116 119 protected String name = null; 120 121 public String getName() { 122 return (this.name); 123 } 124 125 public void setName(String name) { 126 this.name = name; 127 } 128 129 132 protected String property = null; 133 134 public String getProperty() { 135 return (this.property); 136 } 137 138 public void setProperty(String property) { 139 this.property = property; 140 } 141 142 145 protected String scope = null; 146 147 public String getScope() { 148 return (this.scope); 149 } 150 151 public void setScope(String scope) { 152 this.scope = scope; 153 } 154 155 159 protected String formatStr = null; 160 161 public String getFormat() { 162 return (this.formatStr); 163 } 164 165 public void setFormat(String formatStr) { 166 this.formatStr = formatStr; 167 } 168 169 172 protected String formatKey = null; 173 174 public String getFormatKey() { 175 return (this.formatKey); 176 } 177 178 public void setFormatKey(String formatKey) { 179 this.formatKey = formatKey; 180 } 181 182 185 protected String localeKey = null; 186 187 public String getLocale() { 188 return (this.localeKey); 189 } 190 191 public void setLocale(String localeKey) { 192 this.localeKey = localeKey; 193 } 194 195 198 protected String bundle = null; 199 200 public String getBundle() { 201 return (this.bundle); 202 } 203 204 public void setBundle(String bundle) { 205 this.bundle = bundle; 206 } 207 208 210 215 public int doStartTag() throws JspException { 216 217 if (ignore) { 219 if (TagUtils.getInstance().lookup(pageContext, name, scope) == null) { 220 return (SKIP_BODY); } 222 } 223 224 Object value = TagUtils.getInstance().lookup(pageContext, name, property, scope); 226 227 if (value == null) { 228 return (SKIP_BODY); } 230 231 String output = formatValue(value); 233 234 if (filter) { 236 TagUtils.getInstance().write(pageContext, TagUtils.getInstance().filter(output)); 237 } else { 238 TagUtils.getInstance().write(pageContext, output); 239 } 240 241 return (SKIP_BODY); 243 244 } 245 246 253 protected String retrieveFormatString(String formatKey) throws JspException { 254 String result = 255 TagUtils.getInstance().message( 256 pageContext, 257 this.bundle, 258 this.localeKey, 259 formatKey); 260 261 if ((result != null) 262 && !(result.startsWith("???") && result.endsWith("???"))) { 263 264 return result; 265 266 } else { 267 return null; 268 } 269 270 } 271 272 287 protected String formatValue(Object valueToFormat) throws JspException { 288 Format format = null; 289 Object value = valueToFormat; 290 Locale locale = TagUtils.getInstance().getUserLocale(pageContext, this.localeKey); 291 boolean formatStrFromResources = false; 292 String formatString = formatStr; 293 294 if (value instanceof java.lang.String ) { 296 return (String ) value; 297 } else { 298 299 if ((formatString == null) && (formatKey != null)) { 301 formatString = retrieveFormatString(this.formatKey); 302 if (formatString != null) { 303 formatStrFromResources = true; 304 } 305 } 306 307 if (value instanceof Number ) { 309 310 if (formatString == null) { 311 if ((value instanceof Byte ) 312 || (value instanceof Short ) 313 || (value instanceof Integer ) 314 || (value instanceof Long ) 315 || (value instanceof BigInteger )) { 316 317 formatString = retrieveFormatString(INT_FORMAT_KEY); 318 319 } else if ( 320 (value instanceof Float ) 321 || (value instanceof Double ) 322 || (value instanceof BigDecimal )) { 323 324 formatString = retrieveFormatString(FLOAT_FORMAT_KEY); 325 } 326 327 if (formatString != null) { 328 formatStrFromResources = true; 329 } 330 } 331 332 if (formatString != null) { 333 try { 334 format = NumberFormat.getNumberInstance(locale); 335 if (formatStrFromResources) { 336 ((DecimalFormat ) format).applyLocalizedPattern( 337 formatString); 338 } else { 339 ((DecimalFormat ) format).applyPattern(formatString); 340 } 341 342 } catch (IllegalArgumentException e) { 343 JspException ex = 344 new JspException ( 345 messages.getMessage("write.format", formatString)); 346 TagUtils.getInstance().saveException(pageContext, ex); 347 throw ex; 348 } 349 } 350 351 } else if (value instanceof java.util.Date ) { 352 353 if (formatString == null) { 354 355 if (value instanceof java.sql.Timestamp ) { 356 formatString = 357 retrieveFormatString(SQL_TIMESTAMP_FORMAT_KEY); 358 359 } else if (value instanceof java.sql.Date ) { 360 formatString = retrieveFormatString(SQL_DATE_FORMAT_KEY); 361 362 } else if (value instanceof java.sql.Time ) { 363 formatString = retrieveFormatString(SQL_TIME_FORMAT_KEY); 364 365 } else if (value instanceof java.util.Date ) { 366 formatString = retrieveFormatString(DATE_FORMAT_KEY); 367 } 368 369 } 370 371 if (formatString != null) { 372 format = new SimpleDateFormat (formatString, locale); 373 } 374 } 375 } 376 377 if (format != null) { 378 return format.format(value); 379 } else { 380 return value.toString(); 381 } 382 383 } 384 385 388 public void release() { 389 390 super.release(); 391 filter = true; 392 ignore = false; 393 name = null; 394 property = null; 395 scope = null; 396 formatStr = null; 397 formatKey = null; 398 localeKey = null; 399 bundle = null; 400 401 } 402 403 } 404 | Popular Tags |