KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > webapp > ValidatorTag


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 JavaDoc;
25 import javax.servlet.jsp.tagext.Tag JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 /**
29  * @author Manfred Geiler (latest modification by $Author: mwessendorf $)
30  * @version $Revision: 1.4 $ $Date: 2004/07/01 22:00:54 $
31  */

32 public class ValidatorTag
33         extends TagSupport JavaDoc
34 {
35     private String JavaDoc _validatorId;
36
37     public void setValidatorId(String JavaDoc validatorId)
38     {
39         _validatorId = validatorId;
40     }
41
42     public int doStartTag()
43             throws javax.servlet.jsp.JspException JavaDoc
44     {
45         UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
46         if (componentTag == null)
47         {
48             throw new JspException JavaDoc("no parent UIComponentTag found");
49         }
50         if (!componentTag.getCreated())
51         {
52             return Tag.SKIP_BODY;
53         }
54
55         Validator validator = createValidator();
56
57         UIComponent component = componentTag.getComponentInstance();
58         if (component == null)
59         {
60             throw new JspException JavaDoc("parent UIComponentTag has no UIComponent");
61         }
62         if (!(component instanceof EditableValueHolder))
63         {
64             throw new JspException JavaDoc("UIComponent is no ValueHolder");
65         }
66         ((EditableValueHolder)component).addValidator(validator);
67
68         return Tag.SKIP_BODY;
69     }
70
71     public void release()
72     {
73         super.release();
74         _validatorId = null;
75     }
76
77     protected Validator createValidator()
78             throws JspException JavaDoc
79     {
80         FacesContext facesContext = FacesContext.getCurrentInstance();
81         Application application = facesContext.getApplication();
82         if (UIComponentTag.isValueReference(_validatorId))
83         {
84             ValueBinding vb = facesContext.getApplication().createValueBinding(_validatorId);
85             return application.createValidator((String JavaDoc)vb.getValue(facesContext));
86         }
87         else
88         {
89             return application.createValidator(_validatorId);
90         }
91     }
92 }
93
Popular Tags