Source code: com/sun/facelets/tag/jsf/ValidateHandler.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;
16
17 import java.io.IOException;
18
19 import javax.el.ELException;
20 import javax.el.ValueExpression;
21 import javax.faces.FacesException;
22 import javax.faces.component.EditableValueHolder;
23 import javax.faces.component.UIComponent;
24 import javax.faces.validator.Validator;
25
26 import com.sun.facelets.FaceletContext;
27 import com.sun.facelets.FaceletException;
28 import com.sun.facelets.tag.MetaTagHandler;
29 import com.sun.facelets.tag.TagAttribute;
30 import com.sun.facelets.tag.TagConfig;
31 import com.sun.facelets.tag.TagException;
32 import com.sun.facelets.tag.MetaRuleset;
33
34 /**
35 * Handles setting a Validator instance on a EditableValueHolder. Will wire all
36 * attributes set to the Validator instance created/fetched. Uses the "binding"
37 * attribute for grabbing instances to apply attributes to. <p/> Will only
38 * set/create Validator is the passed UIComponent's parent is null, signifying
39 * that it wasn't restored from an existing tree.
40 *
41 * @author Jacob Hookom
42 * @version $Id: ValidateHandler.java,v 1.3 2005/08/24 04:38:51 jhook Exp $
43 */
44 public class ValidateHandler extends MetaTagHandler {
45
46 private final TagAttribute binding;
47
48 private String validatorId;
49
50 /**
51 *
52 * @param config
53 * @deprecated
54 */
55 public ValidateHandler(TagConfig config) {
56 super(config);
57 this.binding = this.getAttribute("binding");
58 }
59
60 public ValidateHandler(ValidatorConfig config) {
61 this((TagConfig) config);
62 this.validatorId = config.getValidatorId();
63 }
64
65 /**
66 * TODO
67 *
68 * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
69 * javax.faces.component.UIComponent)
70 */
71 public final void apply(FaceletContext ctx, UIComponent parent)
72 throws IOException, FacesException, FaceletException, ELException {
73
74 if (parent == null || !(parent instanceof EditableValueHolder)) {
75 throw new TagException(this.tag,
76 "Parent not an instance of EditableValueHolder: " + parent);
77 }
78
79 // only process if it's been created
80 if (parent.getParent() == null) {
81 // cast to a ValueHolder
82 EditableValueHolder evh = (EditableValueHolder) parent;
83 ValueExpression ve = null;
84 Validator v = null;
85 if (this.binding != null) {
86 ve = this.binding.getValueExpression(ctx, Validator.class);
87 v = (Validator) ve.getValue(ctx);
88 }
89 if (v == null) {
90 v = this.createValidator(ctx);
91 if (ve != null) {
92 ve.setValue(ctx, v);
93 }
94 }
95 if (v == null) {
96 throw new TagException(this.tag, "No Validator was created");
97 }
98 this.setAttributes(ctx, v);
99 evh.addValidator(v);
100 }
101 }
102
103 /**
104 * Template method for creating a Validator instance
105 *
106 * @param ctx
107 * FaceletContext to use
108 * @return a new Validator instance
109 */
110 protected Validator createValidator(FaceletContext ctx) {
111 if (this.validatorId == null) {
112 throw new TagException(
113 this.tag,
114 "Default behavior invoked of requiring a validator-id passed in the constructor, must override ValidateHandler(ValidatorConfig)");
115 }
116 return ctx.getFacesContext().getApplication().createValidator(
117 this.validatorId);
118 }
119
120 protected MetaRuleset createMetaRuleset(Class type) {
121 return super.createMetaRuleset(type).ignore("binding");
122 }
123
124 }