KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > taglib > ConvertDateTimeTag


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import javax.faces.convert.Converter;
25 import javax.faces.convert.DateTimeConverter;
26 import java.util.Locale JavaDoc;
27 import java.util.TimeZone JavaDoc;
28
29 /**
30  * @version CVS $Id: ConvertDateTimeTag.java 56374 2004-11-02 14:22:26Z vgritsenko $
31  */

32 public class ConvertDateTimeTag extends ConverterTag {
33     private String JavaDoc dateStyle;
34     private String JavaDoc locale;
35     private String JavaDoc pattern;
36     private String JavaDoc timeStyle;
37     private String JavaDoc timeZone;
38     private String JavaDoc type;
39
40     public void setDateStyle(String JavaDoc dateStyle) {
41         this.dateStyle = dateStyle;
42     }
43
44     public void setLocale(String JavaDoc locale) {
45         this.locale = locale;
46     }
47
48     public void setPattern(String JavaDoc pattern) {
49         this.pattern = pattern;
50     }
51
52     public void setTimeStyle(String JavaDoc timeStyle) {
53         this.timeStyle = timeStyle;
54     }
55
56     public void setTimeZone(String JavaDoc timeZone) {
57         this.timeZone = timeZone;
58     }
59
60     public void setType(String JavaDoc type) {
61         this.type = type;
62     }
63
64
65     public int doStartTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
66     throws SAXException JavaDoc {
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 JavaDoc) tag.evaluate(pattern));
76
77         String JavaDoc ds = dateStyle == null? "default": (String JavaDoc) tag.evaluate(dateStyle);
78         String JavaDoc ts = timeStyle == null? "default": (String JavaDoc) tag.evaluate(timeStyle);
79         converter.setDateStyle(ds);
80         converter.setTimeStyle(ts);
81
82         String JavaDoc t = null;
83         if (type != null) {
84             t = (String JavaDoc) 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 JavaDoc l = null;
93         if (locale != null) {
94             if (FacesUtils.isExpression(locale)) {
95                 l = (Locale JavaDoc) tag.evaluate(locale);
96             } else {
97                 l = I18nUtils.parseLocale(locale);
98             }
99         }
100         converter.setLocale(l);
101
102         TimeZone JavaDoc tz = null;
103         if (timeZone != null) {
104             if (FacesUtils.isExpression(timeZone)) {
105                 tz = (TimeZone JavaDoc) 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