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 public class ValidatorTag extends TagSupport
37 {
38 private static final long serialVersionUID = 8794036166323016663L;
39 private String _validatorId;
40 private String _binding;
41
42 public void setValidatorId(String validatorId)
43 {
44 _validatorId = validatorId;
45 }
46
47 @Override
48 public int doStartTag() throws JspException
49 {
50 UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
51 if (componentTag == null)
52 {
53 throw new JspException("no parent UIComponentTag found");
54 }
55 if (!componentTag.getCreated())
56 {
57 return Tag.SKIP_BODY;
58 }
59
60 Validator validator = createValidator();
61
62 UIComponent component = componentTag.getComponentInstance();
63 if (component == null)
64 {
65 throw new JspException("parent UIComponentTag has no UIComponent");
66 }
67 if (!(component instanceof EditableValueHolder))
68 {
69 throw new JspException("UIComponent is no ValueHolder");
70 }
71 ((EditableValueHolder)component).addValidator(validator);
72
73 return Tag.SKIP_BODY;
74 }
75
76 @Override
77 public void release()
78 {
79 super.release();
80 _validatorId = null;
81 _binding = null;
82 }
83
84
85
86
87 protected Validator createValidator() throws JspException
88 {
89 FacesContext facesContext = FacesContext.getCurrentInstance();
90 Application application = facesContext.getApplication();
91
92 if (_binding != null)
93 {
94 ValueBinding vb = application.createValueBinding(_binding);
95 if (vb != null)
96 {
97 Validator validator = (Validator)vb.getValue(facesContext);
98 if (validator != null)
99 {
100 return validator;
101 }
102 }
103 }
104
105 if (UIComponentTag.isValueReference(_validatorId))
106 {
107 ValueBinding vb = facesContext.getApplication().createValueBinding(_validatorId);
108 return application.createValidator((String)vb.getValue(facesContext));
109 }
110
111 return application.createValidator(_validatorId);
112
113 }
114
115
116
117
118
119
120
121
122 public void setBinding(java.lang.String binding) throws javax.servlet.jsp.JspException
123 {
124 if (binding != null && !UIComponentTag.isValueReference(binding))
125 {
126 throw new IllegalArgumentException("not a valid binding: " + binding);
127 }
128 _binding = binding;
129 }
130 }