1 16 package org.apache.cocoon.faces.taglib; 17 18 import org.apache.cocoon.i18n.I18nUtils; 19 20 import org.apache.cocoon.faces.FacesUtils; 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.SAXException ; 23 24 import javax.faces.convert.Converter; 25 import javax.faces.convert.DateTimeConverter; 26 import java.util.Locale ; 27 import java.util.TimeZone ; 28 29 32 public class ConvertDateTimeTag extends ConverterTag { 33 private String dateStyle; 34 private String locale; 35 private String pattern; 36 private String timeStyle; 37 private String timeZone; 38 private String type; 39 40 public void setDateStyle(String dateStyle) { 41 this.dateStyle = dateStyle; 42 } 43 44 public void setLocale(String locale) { 45 this.locale = locale; 46 } 47 48 public void setPattern(String pattern) { 49 this.pattern = pattern; 50 } 51 52 public void setTimeStyle(String timeStyle) { 53 this.timeStyle = timeStyle; 54 } 55 56 public void setTimeZone(String timeZone) { 57 this.timeZone = timeZone; 58 } 59 60 public void setType(String type) { 61 this.type = type; 62 } 63 64 65 public int doStartTag(String namespaceURI, String localName, String qName, Attributes atts) 66 throws SAXException { 67 setConverterId("javax.faces.DateTime"); 68 return super.doStartTag(namespaceURI, localName, qName, atts); 69 } 70 71 protected Converter createConverter() { 72 final UIComponentTag tag = FacesUtils.findParentUIComponentTag(this); 73 DateTimeConverter converter = (DateTimeConverter) super.createConverter(); 74 75 converter.setPattern((String ) tag.evaluate(pattern)); 76 77 String ds = dateStyle == null? "default": (String ) tag.evaluate(dateStyle); 78 String ts = timeStyle == null? "default": (String ) tag.evaluate(timeStyle); 79 converter.setDateStyle(ds); 80 converter.setTimeStyle(ts); 81 82 String t = null; 83 if (type != null) { 84 t = (String ) tag.evaluate(type); 85 } else if (timeStyle != null) { 86 t = dateStyle != null? "both" : "time"; 87 } else { 88 t = "date"; 89 } 90 converter.setType(t); 91 92 Locale l = null; 93 if (locale != null) { 94 if (FacesUtils.isExpression(locale)) { 95 l = (Locale ) tag.evaluate(locale); 96 } else { 97 l = I18nUtils.parseLocale(locale); 98 } 99 } 100 converter.setLocale(l); 101 102 TimeZone tz = null; 103 if (timeZone != null) { 104 if (FacesUtils.isExpression(timeZone)) { 105 tz = (TimeZone ) tag.evaluate(timeZone); 106 } else { 107 tz = TimeZone.getTimeZone(timeZone); 108 } 109 } 110 converter.setTimeZone(tz); 111 112 return converter; 113 } 114 115 public void recycle() { 116 super.recycle(); 117 this.dateStyle = null; 118 this.locale = null; 119 this.pattern = null; 120 this.timeStyle = null; 121 this.timeZone = null; 122 this.type = null; 123 } 124 } 125 | Popular Tags |