KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > jsf > ConvertHandler


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;
16
17 import java.io.IOException JavaDoc;
18
19 import javax.el.ELException;
20 import javax.el.ValueExpression;
21 import javax.faces.FacesException;
22 import javax.faces.component.UIComponent;
23 import javax.faces.component.ValueHolder;
24 import javax.faces.context.FacesContext;
25 import javax.faces.convert.Converter;
26
27 import com.sun.facelets.FaceletContext;
28 import com.sun.facelets.FaceletException;
29 import com.sun.facelets.tag.MetaTagHandler;
30 import com.sun.facelets.tag.TagAttribute;
31 import com.sun.facelets.tag.TagConfig;
32 import com.sun.facelets.tag.TagException;
33 import com.sun.facelets.tag.MetaRuleset;
34
35 /**
36  * Handles setting a Converter instance on a ValueHolder. Will wire all
37  * attributes set to the Converter instance created/fetched. Uses the "binding"
38  * attribute for grabbing instances to apply attributes to. <p/> Will only
39  * set/create Converter is the passed UIComponent's parent is null, signifying
40  * that it wasn't restored from an existing tree.
41  *
42  * @see javax.faces.webapp.ConverterELTag
43  * @see javax.faces.convert.Converter
44  * @see javax.faces.component.ValueHolder
45  * @author Jacob Hookom
46  * @version $Id: ConvertHandler.java,v 1.3 2005/08/24 04:38:51 jhook Exp $
47  */

48 public class ConvertHandler extends MetaTagHandler {
49
50     private final TagAttribute binding;
51     
52     private String JavaDoc converterId;
53
54     /**
55      * @param config
56      * @deprecated
57      */

58     public ConvertHandler(TagConfig config) {
59         super(config);
60         this.binding = this.getAttribute("binding");
61         this.converterId = null;
62     }
63     
64     public ConvertHandler(ConverterConfig config) {
65         this((TagConfig) config);
66         this.converterId = config.getConverterId();
67     }
68
69     /**
70      * Set Converter instance on parent ValueHolder if it's not being restored.
71      * <ol>
72      * <li>Cast to ValueHolder</li>
73      * <li>If "binding" attribute was specified, fetch/create and re-bind to
74      * expression.</li>
75      * <li>Otherwise, call
76      * {@link #createConverter(FaceletContext) createConverter}.</li>
77      * <li>Call
78      * {@link ObjectHandler#setAttributes(FaceletContext, Object) setAttributes}
79      * on Converter instance.</li>
80      * <li>Set the Converter on the ValueHolder</li>
81      * <li>If the ValueHolder has a localValue, convert it and set the value</li>
82      * </ol>
83      *
84      * @see ValueHolder
85      * @see Converter
86      * @see #createConverter(FaceletContext)
87      * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
88      * javax.faces.component.UIComponent)
89      */

90     public final void apply(FaceletContext ctx, UIComponent parent)
91             throws IOException JavaDoc, FacesException, FaceletException, ELException {
92         if (parent == null || !(parent instanceof ValueHolder)) {
93             throw new TagException(this.tag,
94                     "Parent not an instance of ValueHolder: " + parent);
95         }
96
97         // only process if it's been created
98
if (parent.getParent() == null) {
99             // cast to a ValueHolder
100
ValueHolder vh = (ValueHolder) parent;
101             ValueExpression ve = null;
102             Converter c = null;
103             if (this.binding != null) {
104                 ve = this.binding.getValueExpression(ctx, Converter.class);
105                 c = (Converter) ve.getValue(ctx);
106             }
107             if (c == null) {
108                 c = this.createConverter(ctx);
109                 if (ve != null) {
110                     ve.setValue(ctx, c);
111                 }
112             }
113             if (c == null) {
114                 throw new TagException(this.tag, "No Converter was created");
115             }
116             this.setAttributes(ctx, c);
117             vh.setConverter(c);
118             Object JavaDoc lv = vh.getLocalValue();
119             FacesContext faces = ctx.getFacesContext();
120             if (lv instanceof String JavaDoc) {
121                 vh.setValue(c.getAsObject(faces, parent, (String JavaDoc) lv));
122             }
123         }
124     }
125
126     /**
127      * Create a Converter instance
128      *
129      * @param ctx
130      * FaceletContext to use
131      * @return Converter instance, cannot be null
132      */

133     protected Converter createConverter(FaceletContext ctx) {
134         if (this.converterId == null) {
135             throw new TagException(
136                     this.tag,
137                     "Default behavior invoked of requiring a converter-id passed in the constructor, must override ConvertHandler(ConverterConfig)");
138         }
139         return ctx.getFacesContext().getApplication().createConverter(this.converterId);
140     }
141
142     protected MetaRuleset createMetaRuleset(Class JavaDoc type) {
143         return super.createMetaRuleset(type).ignore("binding");
144     }
145 }
146
Popular Tags