Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConvertHandler |
|
| 3.4;3.4 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | package org.apache.myfaces.view.facelets.tag.jsf; | |
20 | ||
21 | import java.io.IOException; | |
22 | ||
23 | import javax.el.ELException; | |
24 | import javax.el.ValueExpression; | |
25 | import javax.faces.FacesException; | |
26 | import javax.faces.component.UIComponent; | |
27 | import javax.faces.component.ValueHolder; | |
28 | import javax.faces.context.FacesContext; | |
29 | import javax.faces.convert.Converter; | |
30 | import javax.faces.view.facelets.ConverterConfig; | |
31 | import javax.faces.view.facelets.FaceletContext; | |
32 | import javax.faces.view.facelets.FaceletException; | |
33 | import javax.faces.view.facelets.MetaRuleset; | |
34 | import javax.faces.view.facelets.TagAttribute; | |
35 | import javax.faces.view.facelets.TagConfig; | |
36 | import javax.faces.view.facelets.TagException; | |
37 | ||
38 | import org.apache.myfaces.view.facelets.tag.MetaTagHandlerImpl; | |
39 | ||
40 | /** | |
41 | * Handles setting a Converter instance on a ValueHolder. Will wire all attributes set to the Converter instance | |
42 | * created/fetched. Uses the "binding" attribute for grabbing instances to apply attributes to. <p/> Will only | |
43 | * set/create Converter is the passed UIComponent's parent is null, signifying that it wasn't restored from an existing | |
44 | * tree. | |
45 | * | |
46 | * @deprecated use javax.faces.view.facelets.ConverterHandler instead | |
47 | * @see javax.faces.webapp.ConverterELTag | |
48 | * @see javax.faces.convert.Converter | |
49 | * @see javax.faces.component.ValueHolder | |
50 | * @author Jacob Hookom | |
51 | * @version $Id$ | |
52 | */ | |
53 | @Deprecated | |
54 | public class ConvertHandler extends MetaTagHandlerImpl | |
55 | { | |
56 | ||
57 | private final TagAttribute binding; | |
58 | ||
59 | private String converterId; | |
60 | ||
61 | /** | |
62 | * @param config | |
63 | * @deprecated | |
64 | */ | |
65 | public ConvertHandler(TagConfig config) | |
66 | { | |
67 | 0 | super(config); |
68 | 0 | this.binding = this.getAttribute("binding"); |
69 | 0 | this.converterId = null; |
70 | 0 | } |
71 | ||
72 | public ConvertHandler(ConverterConfig config) | |
73 | { | |
74 | 0 | this((TagConfig) config); |
75 | 0 | this.converterId = config.getConverterId(); |
76 | 0 | } |
77 | ||
78 | /** | |
79 | * Set Converter instance on parent ValueHolder if it's not being restored. | |
80 | * <ol> | |
81 | * <li>Cast to ValueHolder</li> | |
82 | * <li>If "binding" attribute was specified, fetch/create and re-bind to expression.</li> | |
83 | * <li>Otherwise, call {@link #createConverter(FaceletContext) createConverter}.</li> | |
84 | * <li>Call {@link ObjectHandler#setAttributes(FaceletContext, Object) setAttributes} on Converter instance.</li> | |
85 | * <li>Set the Converter on the ValueHolder</li> | |
86 | * <li>If the ValueHolder has a localValue, convert it and set the value</li> | |
87 | * </ol> | |
88 | * | |
89 | * @see ValueHolder | |
90 | * @see Converter | |
91 | * @see #createConverter(FaceletContext) | |
92 | * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent) | |
93 | */ | |
94 | public final void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, | |
95 | FaceletException, ELException | |
96 | { | |
97 | 0 | if (parent == null || !(parent instanceof ValueHolder)) |
98 | { | |
99 | 0 | throw new TagException(this.tag, "Parent not an instance of ValueHolder: " + parent); |
100 | } | |
101 | ||
102 | // only process if it's been created | |
103 | 0 | if (parent.getParent() == null) |
104 | { | |
105 | // cast to a ValueHolder | |
106 | 0 | ValueHolder vh = (ValueHolder) parent; |
107 | 0 | ValueExpression ve = null; |
108 | 0 | Converter c = null; |
109 | 0 | if (this.binding != null) |
110 | { | |
111 | 0 | ve = this.binding.getValueExpression(ctx, Converter.class); |
112 | 0 | c = (Converter) ve.getValue(ctx); |
113 | } | |
114 | 0 | if (c == null) |
115 | { | |
116 | 0 | c = this.createConverter(ctx); |
117 | 0 | if (ve != null) |
118 | { | |
119 | 0 | ve.setValue(ctx, c); |
120 | } | |
121 | } | |
122 | 0 | if (c == null) |
123 | { | |
124 | 0 | throw new TagException(this.tag, "No Converter was created"); |
125 | } | |
126 | 0 | this.setAttributes(ctx, c); |
127 | 0 | vh.setConverter(c); |
128 | 0 | Object lv = vh.getLocalValue(); |
129 | 0 | FacesContext faces = ctx.getFacesContext(); |
130 | 0 | if (lv instanceof String) |
131 | { | |
132 | 0 | vh.setValue(c.getAsObject(faces, parent, (String) lv)); |
133 | } | |
134 | } | |
135 | 0 | } |
136 | ||
137 | /** | |
138 | * Create a Converter instance | |
139 | * | |
140 | * @param ctx | |
141 | * FaceletContext to use | |
142 | * @return Converter instance, cannot be null | |
143 | */ | |
144 | protected Converter createConverter(FaceletContext ctx) | |
145 | { | |
146 | 0 | if (this.converterId == null) |
147 | { | |
148 | 0 | throw new TagException(this.tag, |
149 | "Default behavior invoked of requiring a converter-id passed in the constructor, " | |
150 | + "must override ConvertHandler(ConverterConfig)"); | |
151 | } | |
152 | 0 | return ctx.getFacesContext().getApplication().createConverter(this.converterId); |
153 | } | |
154 | ||
155 | protected MetaRuleset createMetaRuleset(Class type) | |
156 | { | |
157 | 0 | return super.createMetaRuleset(type).ignore("binding"); |
158 | } | |
159 | } |