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