Source code: javax/faces/webapp/ValidatorTag.java
1 /*
2 * Copyright 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 javax.faces.webapp;
17
18 import javax.faces.application.Application;
19 import javax.faces.component.EditableValueHolder;
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.FacesContext;
22 import javax.faces.el.ValueBinding;
23 import javax.faces.validator.Validator;
24 import javax.servlet.jsp.JspException;
25 import javax.servlet.jsp.tagext.Tag;
26 import javax.servlet.jsp.tagext.TagSupport;
27
28 /**
29 * @author Manfred Geiler (latest modification by $Author: bdudney $)
30 * @version $Revision: 225333 $ $Date: 2005-07-26 11:49:19 -0400 (Tue, 26 Jul 2005) $
31 */
32 public class ValidatorTag
33 extends TagSupport
34 {
35 private static final long serialVersionUID = 8794036166323016663L;
36 private String _validatorId;
37
38 public void setValidatorId(String validatorId)
39 {
40 _validatorId = validatorId;
41 }
42
43 public int doStartTag()
44 throws javax.servlet.jsp.JspException
45 {
46 UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
47 if (componentTag == null)
48 {
49 throw new JspException("no parent UIComponentTag found");
50 }
51 if (!componentTag.getCreated())
52 {
53 return Tag.SKIP_BODY;
54 }
55
56 Validator validator = createValidator();
57
58 UIComponent component = componentTag.getComponentInstance();
59 if (component == null)
60 {
61 throw new JspException("parent UIComponentTag has no UIComponent");
62 }
63 if (!(component instanceof EditableValueHolder))
64 {
65 throw new JspException("UIComponent is no ValueHolder");
66 }
67 ((EditableValueHolder)component).addValidator(validator);
68
69 return Tag.SKIP_BODY;
70 }
71
72 public void release()
73 {
74 super.release();
75 _validatorId = null;
76 }
77
78 protected Validator createValidator()
79 throws JspException
80 {
81 FacesContext facesContext = FacesContext.getCurrentInstance();
82 Application application = facesContext.getApplication();
83 if (UIComponentTag.isValueReference(_validatorId))
84 {
85 ValueBinding vb = facesContext.getApplication().createValueBinding(_validatorId);
86 return application.createValidator((String)vb.getValue(facesContext));
87 }
88 else
89 {
90 return application.createValidator(_validatorId);
91 }
92 }
93 }