Source code: com/sun/facelets/tag/jsf/core/ConvertDateTimeHandler.java
1 /**
2 * Licensed under the Common Development and Distribution License,
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.sun.com/cddl/
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11 * implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15 package com.sun.facelets.tag.jsf.core;
16
17 import java.util.TimeZone;
18
19 import javax.el.ELException;
20 import javax.faces.FacesException;
21 import javax.faces.convert.Converter;
22 import javax.faces.convert.DateTimeConverter;
23
24 import com.sun.facelets.FaceletContext;
25 import com.sun.facelets.FaceletException;
26 import com.sun.facelets.tag.MetaRuleset;
27 import com.sun.facelets.tag.TagAttribute;
28 import com.sun.facelets.tag.TagAttributeException;
29 import com.sun.facelets.tag.jsf.ComponentSupport;
30 import com.sun.facelets.tag.jsf.ConvertHandler;
31 import com.sun.facelets.tag.jsf.ConverterConfig;
32
33 /**
34 * Register a DateTimeConverter instance on the UIComponent associated with the
35 * closest parent UIComponent custom action. <p/> See <a target="_new"
36 * href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/tlddocs/f/convertDateTime.html">tag
37 * documentation</a>.
38 *
39 * @author Jacob Hookom
40 * @version $Id: ConvertDateTimeHandler.java,v 1.4 2005/11/30 23:36:38 jhook Exp $
41 */
42 public final class ConvertDateTimeHandler extends ConvertHandler {
43
44 private final TagAttribute dateStyle;
45
46 private final TagAttribute locale;
47
48 private final TagAttribute pattern;
49
50 private final TagAttribute timeStyle;
51
52 private final TagAttribute timeZone;
53
54 private final TagAttribute type;
55
56 /**
57 * @param config
58 */
59 public ConvertDateTimeHandler(ConverterConfig config) {
60 super(config);
61 this.dateStyle = this.getAttribute("dateStyle");
62 this.locale = this.getAttribute("locale");
63 this.pattern = this.getAttribute("pattern");
64 this.timeStyle = this.getAttribute("timeStyle");
65 this.timeZone = this.getAttribute("timeZone");
66 this.type = this.getAttribute("type");
67 }
68
69 /**
70 * Returns a new DateTimeConverter
71 *
72 * @see DateTimeConverter
73 * @see com.sun.facelets.tag.jsf.ConvertHandler#createConverter(com.sun.facelets.FaceletContext)
74 */
75 protected Converter createConverter(FaceletContext ctx)
76 throws FacesException, ELException, FaceletException {
77 return ctx.getFacesContext().getApplication().createConverter(DateTimeConverter.CONVERTER_ID);
78
79 }
80
81 /**
82 * Implements tag spec, see taglib documentation.
83 *
84 * @see com.sun.facelets.tag.ObjectHandler#setAttributes(com.sun.facelets.FaceletContext,
85 * java.lang.Object)
86 */
87 protected void setAttributes(FaceletContext ctx, Object obj) {
88 DateTimeConverter c = (DateTimeConverter) obj;
89 if (this.locale != null) {
90 c.setLocale(ComponentSupport.getLocale(ctx, this.locale));
91 }
92 if (this.pattern != null) {
93 c.setPattern(this.pattern.getValue(ctx));
94 } else {
95 if (this.type != null) {
96 c.setType(this.type.getValue(ctx));
97 }
98 if (this.dateStyle != null) {
99 c.setDateStyle(this.dateStyle.getValue(ctx));
100 }
101 if (this.timeStyle != null) {
102 c.setTimeStyle(this.timeStyle.getValue(ctx));
103 }
104 }
105
106 if (this.timeZone != null) {
107 Object t = this.timeZone.getObject(ctx);
108 if (t instanceof TimeZone) {
109 c.setTimeZone((TimeZone) t);
110 } else if (t instanceof String) {
111 TimeZone tz = TimeZone.getTimeZone((String) t);
112 c.setTimeZone(tz);
113 } else {
114 throw new TagAttributeException(
115 this.tag,
116 this.timeZone,
117 "Illegal TimeZone, must evaluate to either a java.util.TimeZone or String, is type: "
118 + t);
119 }
120 }
121 }
122
123 protected MetaRuleset createMetaRuleset(Class type) {
124 return super.createMetaRuleset(type).ignoreAll();
125 }
126 }