1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.tag.jsf;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.el.ValueExpression;
26 import javax.faces.FacesWrapper;
27 import javax.faces.component.EditableValueHolder;
28 import javax.faces.component.UIComponent;
29 import javax.faces.context.FacesContext;
30 import javax.faces.validator.Validator;
31 import javax.faces.view.EditableValueHolderAttachedObjectHandler;
32 import javax.faces.view.facelets.ComponentHandler;
33 import javax.faces.view.facelets.FaceletContext;
34 import javax.faces.view.facelets.MetaRuleset;
35 import javax.faces.view.facelets.TagAttribute;
36 import javax.faces.view.facelets.TagException;
37 import javax.faces.view.facelets.TagHandlerDelegate;
38 import javax.faces.view.facelets.ValidatorHandler;
39
40 import org.apache.myfaces.shared.renderkit.JSFAttr;
41 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
42 import org.apache.myfaces.view.facelets.compiler.FaceletsCompilerUtils;
43 import org.apache.myfaces.view.facelets.tag.MetaRulesetImpl;
44
45
46
47
48
49
50
51
52
53
54
55
56 public class ValidatorTagHandlerDelegate extends TagHandlerDelegate
57 implements EditableValueHolderAttachedObjectHandler, FacesWrapper<ValidatorHandler>
58 {
59
60
61
62
63
64
65 public final static String VALIDATOR_ID_EXCLUSION_LIST_KEY
66 = "org.apache.myfaces.validator.VALIDATOR_ID_EXCLUSION_LIST";
67
68 private ValidatorHandler _delegate;
69
70
71
72
73
74 private final boolean _wrapMode;
75
76 public ValidatorTagHandlerDelegate(ValidatorHandler delegate)
77 {
78 _delegate = delegate;
79
80
81
82
83
84
85
86 _wrapMode = FaceletsCompilerUtils.hasChildren(_delegate.getValidatorConfig());
87 }
88
89 @Override
90 public void apply(FaceletContext ctx, UIComponent parent) throws IOException
91 {
92
93 FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx);
94
95 if (_wrapMode)
96 {
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 String validatorId = _delegate.getValidatorConfig().getValidatorId();
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 try
150 {
151 mctx.pushEnclosingValidatorIdToStack(validatorId, this);
152 _delegate.applyNextHandler(ctx, parent);
153 }
154 finally
155 {
156 mctx.popEnclosingValidatorIdToStack();
157 }
158
159
160
161
162
163
164
165
166
167 }
168 else
169 {
170
171 if (!ComponentHandler.isNew(parent))
172 {
173 return;
174 }
175
176
177 if (parent instanceof EditableValueHolder)
178 {
179 applyAttachedObject(ctx.getFacesContext(), parent);
180 }
181 else if (UIComponent.isCompositeComponent(parent))
182 {
183 if (getFor() == null)
184 {
185 throw new TagException(_delegate.getTag(), "is nested inside a composite component"
186 + " but does not have a for attribute.");
187 }
188 mctx.addAttachedObjectHandler(parent, _delegate);
189 }
190 else
191 {
192 throw new TagException(_delegate.getTag(),
193 "Parent not composite component or an instance of EditableValueHolder: " + parent);
194 }
195 }
196 }
197
198
199
200
201
202
203
204 protected Validator createValidator(FaceletContext ctx)
205 {
206 if (_delegate.getValidatorId(ctx) == null)
207 {
208 throw new TagException(_delegate.getTag(), "Default behavior invoked of requiring " +
209 "a validator-id passed in the constructor, must override ValidateHandler(ValidatorConfig)");
210 }
211 return ctx.getFacesContext().getApplication().createValidator(_delegate.getValidatorId(ctx));
212 }
213
214 @Override
215 public MetaRuleset createMetaRuleset(Class type)
216 {
217 MetaRuleset metaRuleset = new MetaRulesetImpl(_delegate.getTag(), type);
218
219
220 metaRuleset.ignore(JSFAttr.BINDING_ATTR).ignore(JSFAttr.DISABLED_ATTR);
221
222 metaRuleset.ignore(JSFAttr.FOR_ATTR);
223
224 return metaRuleset;
225 }
226
227 @SuppressWarnings("unchecked")
228 public void applyAttachedObject(FacesContext context, UIComponent parent)
229 {
230
231 FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(
232 FaceletContext.FACELET_CONTEXT_KEY);
233
234
235
236
237
238 if (_delegate.isDisabled(faceletContext))
239 {
240
241 String validatorId = _delegate.getValidatorConfig().getValidatorId();
242 if (validatorId != null && !"".equals(validatorId))
243 {
244 List<String> exclusionList = (List<String>) parent.getAttributes()
245 .get(VALIDATOR_ID_EXCLUSION_LIST_KEY);
246 if (exclusionList == null)
247 {
248 exclusionList = new ArrayList<String>();
249 parent.getAttributes().put(VALIDATOR_ID_EXCLUSION_LIST_KEY, exclusionList);
250 }
251 exclusionList.add(validatorId);
252 }
253 }
254 else
255 {
256
257
258
259 EditableValueHolder evh = (EditableValueHolder) parent;
260 ValueExpression ve = null;
261 Validator v = null;
262 if (_delegate.getBinding() != null)
263 {
264 ve = _delegate.getBinding().getValueExpression(faceletContext, Validator.class);
265 v = (Validator) ve.getValue(faceletContext);
266 }
267 if (v == null)
268 {
269 v = this.createValidator(faceletContext);
270 if (ve != null)
271 {
272 ve.setValue(faceletContext, v);
273 }
274 }
275 if (v == null)
276 {
277 throw new TagException(_delegate.getTag(), "No Validator was created");
278 }
279 _delegate.setAttributes(faceletContext, v);
280 evh.addValidator(v);
281 }
282 }
283
284 public String getFor()
285 {
286 TagAttribute forAttribute = _delegate.getTagAttribute("for");
287
288 if (forAttribute == null)
289 {
290 return null;
291 }
292 else
293 {
294 return forAttribute.getValue();
295 }
296 }
297
298 public String getValidationGroups(FaceletContext ctx)
299 {
300 TagAttribute attribute = _delegate.getTagAttribute("validationGroups");
301
302 if (attribute == null)
303 {
304 return null;
305 }
306 else
307 {
308 return attribute.getValue(ctx);
309 }
310 }
311
312 public ValidatorHandler getWrapped()
313 {
314 return _delegate;
315 }
316
317 }