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 | 0 | 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 | 0 | _validatorId = validatorId; |
45 | 0 | } |
46 | |
|
47 | |
@Override |
48 | |
public int doStartTag() throws JspException |
49 | |
{ |
50 | 0 | UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext); |
51 | 0 | if (componentTag == null) |
52 | |
{ |
53 | 0 | throw new JspException("no parent UIComponentTag found"); |
54 | |
} |
55 | 0 | if (!componentTag.getCreated()) |
56 | |
{ |
57 | 0 | return Tag.SKIP_BODY; |
58 | |
} |
59 | |
|
60 | 0 | Validator validator = createValidator(); |
61 | |
|
62 | 0 | UIComponent component = componentTag.getComponentInstance(); |
63 | 0 | if (component == null) |
64 | |
{ |
65 | 0 | throw new JspException("parent UIComponentTag has no UIComponent"); |
66 | |
} |
67 | 0 | if (!(component instanceof EditableValueHolder)) |
68 | |
{ |
69 | 0 | throw new JspException("UIComponent is no ValueHolder"); |
70 | |
} |
71 | 0 | ((EditableValueHolder)component).addValidator(validator); |
72 | |
|
73 | 0 | return Tag.SKIP_BODY; |
74 | |
} |
75 | |
|
76 | |
@Override |
77 | |
public void release() |
78 | |
{ |
79 | 0 | super.release(); |
80 | 0 | _validatorId = null; |
81 | 0 | _binding = null; |
82 | 0 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
protected Validator createValidator() throws JspException |
88 | |
{ |
89 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
90 | 0 | Application application = facesContext.getApplication(); |
91 | |
|
92 | 0 | if (_binding != null) |
93 | |
{ |
94 | 0 | ValueBinding vb = application.createValueBinding(_binding); |
95 | 0 | if (vb != null) |
96 | |
{ |
97 | 0 | Validator validator = (Validator)vb.getValue(facesContext); |
98 | 0 | if (validator != null) |
99 | |
{ |
100 | 0 | return validator; |
101 | |
} |
102 | |
} |
103 | |
} |
104 | |
|
105 | 0 | if (UIComponentTag.isValueReference(_validatorId)) |
106 | |
{ |
107 | 0 | ValueBinding vb = facesContext.getApplication().createValueBinding(_validatorId); |
108 | 0 | return application.createValidator((String)vb.getValue(facesContext)); |
109 | |
} |
110 | |
|
111 | 0 | 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 | 0 | if (binding != null && !UIComponentTag.isValueReference(binding)) |
125 | |
{ |
126 | 0 | throw new IllegalArgumentException("not a valid binding: " + binding); |
127 | |
} |
128 | 0 | _binding = binding; |
129 | 0 | } |
130 | |
} |