1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.webapp;
20
21 import javax.faces.application.Application;
22 import javax.faces.component.EditableValueHolder;
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25 import javax.faces.el.ValueBinding;
26 import javax.faces.validator.Validator;
27 import javax.servlet.jsp.JspException;
28 import javax.servlet.jsp.tagext.Tag;
29 import javax.servlet.jsp.tagext.TagSupport;
30
31
32
33
34
35
36
37
38
39 public class ValidatorTag
40 extends TagSupport
41 {
42 private static final long serialVersionUID = 8794036166323016663L;
43 private String _validatorId;
44 private String _binding;
45
46 public void setValidatorId(String validatorId)
47 {
48 _validatorId = validatorId;
49 }
50
51 public int doStartTag()
52 throws javax.servlet.jsp.JspException
53 {
54 UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
55 if (componentTag == null)
56 {
57 throw new JspException("no parent UIComponentTag found");
58 }
59 if (!componentTag.getCreated())
60 {
61 return Tag.SKIP_BODY;
62 }
63
64 Validator validator = createValidator();
65
66 UIComponent component = componentTag.getComponentInstance();
67 if (component == null)
68 {
69 throw new JspException("parent UIComponentTag has no UIComponent");
70 }
71 if (!(component instanceof EditableValueHolder))
72 {
73 throw new JspException("UIComponent is no ValueHolder");
74 }
75 ((EditableValueHolder)component).addValidator(validator);
76
77 return Tag.SKIP_BODY;
78 }
79
80 public void release()
81 {
82 super.release();
83 _validatorId = null;
84 _binding = null;
85 }
86
87 protected Validator createValidator()
88 throws JspException
89 {
90 FacesContext facesContext = FacesContext.getCurrentInstance();
91 Application application = facesContext.getApplication();
92
93 if(_binding != null) {
94 ValueBinding vb = application.createValueBinding(_binding);
95 if(vb != null) {
96 Validator validator = (Validator) vb.getValue(facesContext);
97 if(validator != null) {
98 return validator;
99 }
100 }
101 }
102
103 if (UIComponentTag.isValueReference(_validatorId))
104 {
105 ValueBinding vb = facesContext.getApplication().createValueBinding(_validatorId);
106 return application.createValidator((String)vb.getValue(facesContext));
107 }
108
109 return application.createValidator(_validatorId);
110
111 }
112
113
114
115
116
117
118
119
120 public void setBinding(java.lang.String binding)
121 throws javax.servlet.jsp.JspException
122 {
123 if (binding != null && !UIComponentTag.isValueReference(binding))
124 {
125 throw new IllegalArgumentException("not a valid binding: " + binding);
126 }
127 _binding = binding;
128 }
129 }