1 18 package org.apache.beehive.netui.tags.html; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import org.apache.beehive.netui.util.Bundle; 23 import org.apache.beehive.netui.util.logging.Logger; 24 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.tagext.JspTag ; 27 import javax.servlet.jsp.tagext.SimpleTagSupport ; 28 import java.text.DateFormat ; 29 import java.text.ParsePosition ; 30 import java.text.SimpleDateFormat ; 31 import java.util.Locale ; 32 33 35 187 public class FormatDate extends FormatTag 188 { 189 private static final Logger logger = Logger.getInstance(FormatDate.class); 190 191 private static final String [] commonFormats = {"MM/dd/yy", "yyyy-MM-dd", "MMddyy", 192 null}; 193 194 private String _stringInput; 195 196 199 public String getTagName() 200 { 201 return "FormatDate"; 202 } 203 204 214 public void setStringInputPattern(String inputPattern) 215 throws JspException 216 { 217 _stringInput = setRequiredValueAttribute(inputPattern, "stringInputPattern"); 218 } 219 220 224 public void doTag() 225 throws JspException 226 { 227 JspTag parentTag = SimpleTagSupport.findAncestorWithClass(this, IFormattable.class); 228 229 if (hasErrors()) { 231 if (parentTag instanceof IFormattable) { 232 IFormattable parent = (IFormattable) parentTag; 233 parent.formatterHasError(); 234 } 235 reportErrors(); 236 return; 237 } 238 239 if (parentTag instanceof IFormattable) { 240 IFormattable parent = (IFormattable) parentTag; 241 DateFormatter dateFmt = new DateFormatter(); 242 dateFmt.setPattern(_pattern); 243 dateFmt.setLocale(getLocale()); 244 dateFmt.setInputPattern(_stringInput); 245 parent.addFormatter(dateFmt); 246 } 247 else { 248 String s = Bundle.getString("Tags_FormattableParentRequired"); 249 registerTagError(s, null); 250 reportErrors(); 251 } 252 } 253 254 257 public static class DateFormatter extends FormatTag.Formatter 258 { 259 private Locale locale; 260 private String inputPattern; 261 262 public void setLocale(Locale locale) 263 { 264 this.locale = locale; 265 } 266 267 public void setInputPattern(String pattern) 268 { 269 inputPattern = pattern; 270 } 271 272 public String format(Object dataToFormat) throws JspException 273 { 274 if (dataToFormat == null) { 275 return null; 276 } 277 InternalStringBuilder formattedString = new InternalStringBuilder(32); 278 279 SimpleDateFormat dateFormat = null; 280 if (getPattern() != null) { 281 try { 282 if (locale != null) { 283 dateFormat = new SimpleDateFormat (getPattern(), locale); 284 } 285 else { 286 dateFormat = new SimpleDateFormat (getPattern()); 287 } 288 } 289 catch (IllegalArgumentException e) { 290 String s = Bundle.getString("Tags_DateFormatPatternException", new Object []{e.getMessage()}); 291 logger.warn(s); 292 throw new JspException (s); 293 } 294 } 295 else { 296 dateFormat = new SimpleDateFormat (); 297 } 298 299 if (dataToFormat instanceof java.sql.Date ) { 300 301 java.sql.Date date = (java.sql.Date ) dataToFormat; 302 formattedString.append(dateFormat.format(date)); 303 } 304 else if (dataToFormat instanceof java.util.Date ) { 305 306 java.util.Date date = (java.util.Date ) dataToFormat; 307 formattedString.append(dateFormat.format(date)); 308 } 309 else if (dataToFormat instanceof java.util.Calendar ) { 310 java.util.Calendar c = (java.util.Calendar ) dataToFormat; 311 java.util.Date date = new java.util.Date (c.getTimeInMillis()); 312 formattedString.append(dateFormat.format(date)); 313 } 314 else if (dataToFormat instanceof String ) { 315 if (dataToFormat.equals("")) { 316 return ""; 317 } 318 319 DateFormat df = null; 320 if (inputPattern != null) { 321 try { 322 df = new SimpleDateFormat (inputPattern); 323 } 324 catch (IllegalArgumentException e) { 325 String s = Bundle.getString("Tags_formatDate_StringPatternError", 326 new Object []{inputPattern, e.getMessage()}); 327 logger.warn(s); 328 throw new JspException (s); 329 } 330 331 java.util.Date date = df.parse((String ) dataToFormat, 333 new ParsePosition (0)); 334 if (date != null) { 335 formattedString.append(dateFormat.format(date)); 336 return formattedString.toString(); 337 } 338 } 339 340 341 int i; 344 for (i = 0; i < commonFormats.length; i++) { 345 346 if (commonFormats[i] != null) { 347 df = new SimpleDateFormat (commonFormats[i]); 348 } 349 else { 350 df = new SimpleDateFormat (); 351 352 } 353 354 java.util.Date date = df.parse((String ) dataToFormat, 356 new ParsePosition (0)); 357 if (date != null) { 358 formattedString.append(dateFormat.format(date)); 359 break; 360 } 361 } 362 if (i == commonFormats.length) { 363 String s = Bundle.getString("Tags_formatDate_String_Error", 364 new Object []{dataToFormat}); 365 logger.error(s); 366 throw new JspException (s); 367 } 368 } 369 else { 370 String s = Bundle.getString("Tags_formatDate_Type_Error", 371 new Object []{dataToFormat.getClass().getName()}); 372 logger.error(s); 373 throw new JspException (s); 374 } 375 376 return formattedString.toString(); 377 } 378 } 379 } 380 | Popular Tags |