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.Collections; |
23 | |
import java.util.EnumSet; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
import java.util.Set; |
28 | |
import java.util.logging.Level; |
29 | |
import java.util.logging.Logger; |
30 | |
|
31 | |
import javax.el.ValueExpression; |
32 | |
import javax.faces.FacesWrapper; |
33 | |
import javax.faces.application.Application; |
34 | |
import javax.faces.application.ProjectStage; |
35 | |
import javax.faces.component.ActionSource; |
36 | |
import javax.faces.component.EditableValueHolder; |
37 | |
import javax.faces.component.UIComponent; |
38 | |
import javax.faces.component.UIOutput; |
39 | |
import javax.faces.component.UniqueIdVendor; |
40 | |
import javax.faces.component.ValueHolder; |
41 | |
import javax.faces.component.behavior.ClientBehaviorHolder; |
42 | |
import javax.faces.component.visit.VisitCallback; |
43 | |
import javax.faces.component.visit.VisitContext; |
44 | |
import javax.faces.component.visit.VisitHint; |
45 | |
import javax.faces.component.visit.VisitResult; |
46 | |
import javax.faces.context.FacesContext; |
47 | |
import javax.faces.event.PhaseId; |
48 | |
import javax.faces.validator.BeanValidator; |
49 | |
import javax.faces.validator.Validator; |
50 | |
import javax.faces.view.EditableValueHolderAttachedObjectHandler; |
51 | |
import javax.faces.view.facelets.ComponentConfig; |
52 | |
import javax.faces.view.facelets.ComponentHandler; |
53 | |
import javax.faces.view.facelets.FaceletContext; |
54 | |
import javax.faces.view.facelets.MetaRuleset; |
55 | |
import javax.faces.view.facelets.TagAttribute; |
56 | |
import javax.faces.view.facelets.TagException; |
57 | |
import javax.faces.view.facelets.TagHandlerDelegate; |
58 | |
import javax.faces.view.facelets.ValidatorHandler; |
59 | |
|
60 | |
import org.apache.myfaces.util.ExternalSpecifications; |
61 | |
import org.apache.myfaces.view.facelets.AbstractFaceletContext; |
62 | |
import org.apache.myfaces.view.facelets.ComponentState; |
63 | |
import org.apache.myfaces.view.facelets.DefaultFaceletsStateManagementStrategy; |
64 | |
import org.apache.myfaces.view.facelets.FaceletCompositionContext; |
65 | |
import org.apache.myfaces.view.facelets.FaceletDynamicComponentRefreshTransientBuildEvent; |
66 | |
import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage; |
67 | |
import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguageBase; |
68 | |
import org.apache.myfaces.view.facelets.el.CompositeComponentELUtils; |
69 | |
import org.apache.myfaces.view.facelets.tag.MetaRulesetImpl; |
70 | |
import org.apache.myfaces.view.facelets.tag.jsf.core.AjaxHandler; |
71 | |
import org.apache.myfaces.view.facelets.tag.jsf.core.FacetHandler; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public class ComponentTagHandlerDelegate extends TagHandlerDelegate |
84 | |
{ |
85 | 0 | private final static Logger log = Logger.getLogger(ComponentTagHandlerDelegate.class.getName()); |
86 | |
|
87 | 0 | private static final Set<VisitHint> VISIT_HINTS_DYN_REFRESH = Collections.unmodifiableSet( |
88 | |
EnumSet.of(VisitHint.SKIP_ITERATION)); |
89 | |
|
90 | |
private final ComponentHandler _delegate; |
91 | |
|
92 | |
private final String _componentType; |
93 | |
|
94 | |
private final TagAttribute _id; |
95 | |
|
96 | |
private final String _rendererType; |
97 | |
|
98 | |
private final ComponentBuilderHandler _componentBuilderHandlerDelegate; |
99 | |
|
100 | |
private final RelocatableResourceHandler _relocatableResourceHandler; |
101 | |
|
102 | |
@SuppressWarnings("unchecked") |
103 | |
public ComponentTagHandlerDelegate(ComponentHandler delegate) |
104 | 0 | { |
105 | 0 | _delegate = delegate; |
106 | 0 | ComponentConfig delegateComponentConfig = delegate.getComponentConfig(); |
107 | 0 | _componentType = delegateComponentConfig.getComponentType(); |
108 | 0 | _rendererType = delegateComponentConfig.getRendererType(); |
109 | 0 | _id = delegate.getTagAttribute("id"); |
110 | |
|
111 | 0 | ComponentHandler handler = _delegate; |
112 | 0 | boolean found = false; |
113 | 0 | while(handler != null && !found) |
114 | |
{ |
115 | 0 | if (handler instanceof ComponentBuilderHandler) |
116 | |
{ |
117 | 0 | found = true; |
118 | |
} |
119 | 0 | else if (handler instanceof FacesWrapper) |
120 | |
{ |
121 | 0 | handler = ((FacesWrapper<? extends ComponentHandler>)handler).getWrapped(); |
122 | |
} |
123 | |
else |
124 | |
{ |
125 | 0 | handler = null; |
126 | |
} |
127 | |
} |
128 | 0 | if (found) |
129 | |
{ |
130 | 0 | _componentBuilderHandlerDelegate = (ComponentBuilderHandler) handler; |
131 | |
} |
132 | |
else |
133 | |
{ |
134 | 0 | _componentBuilderHandlerDelegate = null; |
135 | |
} |
136 | |
|
137 | |
|
138 | 0 | handler = _delegate; |
139 | 0 | found = false; |
140 | 0 | while(handler != null && !found) |
141 | |
{ |
142 | 0 | if (handler instanceof RelocatableResourceHandler) |
143 | |
{ |
144 | 0 | found = true; |
145 | |
} |
146 | 0 | else if (handler instanceof FacesWrapper) |
147 | |
{ |
148 | 0 | handler = ((FacesWrapper<? extends ComponentHandler>)handler).getWrapped(); |
149 | |
} |
150 | |
else |
151 | |
{ |
152 | 0 | handler = null; |
153 | |
} |
154 | |
} |
155 | 0 | if (found) |
156 | |
{ |
157 | 0 | _relocatableResourceHandler = (RelocatableResourceHandler) handler; |
158 | |
} |
159 | |
else |
160 | |
{ |
161 | |
|
162 | 0 | if (_componentType != null && _rendererType != null && |
163 | |
(_rendererType.equals("javax.faces.resource.Script") || |
164 | |
_rendererType.equals("javax.faces.resource.Stylesheet")) && |
165 | |
_componentType.equals(UIOutput.COMPONENT_TYPE)) |
166 | |
{ |
167 | 0 | _relocatableResourceHandler = ComponentRelocatableResourceHandler.INSTANCE; |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | 0 | _relocatableResourceHandler = null; |
172 | |
} |
173 | |
} |
174 | 0 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
@SuppressWarnings("unchecked") |
204 | |
@Override |
205 | |
public void apply(FaceletContext ctx, UIComponent parent) throws IOException |
206 | |
{ |
207 | |
|
208 | 0 | if (parent == null) |
209 | |
{ |
210 | 0 | throw new TagException(_delegate.getTag(), "Parent UIComponent was null"); |
211 | |
} |
212 | |
|
213 | 0 | FacesContext facesContext = ctx.getFacesContext(); |
214 | |
|
215 | |
|
216 | 0 | String facetName = this.getFacetName(ctx, parent); |
217 | |
|
218 | |
|
219 | 0 | String id = ctx.generateUniqueId(_delegate.getTagId()); |
220 | |
|
221 | |
|
222 | 0 | FaceletCompositionContext mctx = (FaceletCompositionContext) FaceletCompositionContext.getCurrentInstance(ctx); |
223 | |
|
224 | |
|
225 | 0 | UIComponent c = null; |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | 0 | UIComponent oldParent = parent; |
231 | |
|
232 | 0 | if (mctx.isRefreshingSection()) |
233 | |
{ |
234 | 0 | if (_relocatableResourceHandler != null) |
235 | |
{ |
236 | 0 | c = _relocatableResourceHandler.findChildByTagId(ctx, parent, id); |
237 | |
} |
238 | |
else |
239 | |
{ |
240 | 0 | if (facetName != null) |
241 | |
{ |
242 | 0 | c = ComponentSupport.findChildInFacetByTagId(parent, id, facetName); |
243 | |
} |
244 | |
else |
245 | |
{ |
246 | 0 | c = ComponentSupport.findChildInChildrenByTagId(parent, id); |
247 | |
} |
248 | |
} |
249 | |
} |
250 | 0 | boolean componentFound = false; |
251 | 0 | if (c != null) |
252 | |
{ |
253 | 0 | componentFound = true; |
254 | |
|
255 | |
|
256 | 0 | if (_delegate.getBinding() != null && |
257 | |
c.getAttributes().containsKey( |
258 | |
FaceletDynamicComponentRefreshTransientBuildEvent.DYNAMIC_COMPONENT_BINDING_NEEDS_REFRESH)) |
259 | |
{ |
260 | 0 | VisitContext visitContext = (VisitContext) mctx.getVisitContextFactory(). |
261 | |
getVisitContext(facesContext, null, VISIT_HINTS_DYN_REFRESH); |
262 | 0 | c.visitTree(visitContext, new PublishFaceletDynamicComponentRefreshTransientBuildCallback()); |
263 | |
} |
264 | |
|
265 | 0 | mctx.incrementUniqueComponentId(); |
266 | |
|
267 | |
|
268 | 0 | if (log.isLoggable(Level.FINE)) |
269 | |
{ |
270 | 0 | log.fine(_delegate.getTag() + " Component[" + id + "] Found, marking children for cleanup"); |
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | 0 | mctx.markForDeletion(c); |
277 | |
|
278 | 0 | if (_relocatableResourceHandler != null) |
279 | |
{ |
280 | 0 | mctx.markRelocatableResourceForDeletion(c); |
281 | |
} |
282 | |
} |
283 | |
else |
284 | |
{ |
285 | 0 | c = this.createComponent(ctx); |
286 | 0 | if (log.isLoggable(Level.FINE)) |
287 | |
{ |
288 | 0 | log.fine(_delegate.getTag() + " Component[" + id + "] Created: " + c.getClass().getName()); |
289 | |
} |
290 | |
|
291 | 0 | _delegate.setAttributes(ctx, c); |
292 | |
|
293 | |
|
294 | 0 | c.getAttributes().put(ComponentSupport.MARK_CREATED, id); |
295 | |
|
296 | 0 | if (facesContext.isProjectStage(ProjectStage.Development)) |
297 | |
{ |
298 | 0 | c.getAttributes().put(UIComponent.VIEW_LOCATION_KEY, |
299 | |
_delegate.getTag().getLocation()); |
300 | |
} |
301 | |
|
302 | |
|
303 | 0 | if (this._id != null) |
304 | |
{ |
305 | 0 | mctx.incrementUniqueComponentId(); |
306 | 0 | c.setId(this._id.getValue(ctx)); |
307 | |
} |
308 | |
else |
309 | |
{ |
310 | 0 | String componentId = mctx.generateUniqueComponentId(); |
311 | 0 | UniqueIdVendor uniqueIdVendor = mctx.getUniqueIdVendorFromStack(); |
312 | 0 | if (uniqueIdVendor == null) |
313 | |
{ |
314 | 0 | uniqueIdVendor = facesContext.getViewRoot(); |
315 | |
|
316 | 0 | if (uniqueIdVendor == null) |
317 | |
{ |
318 | |
|
319 | |
|
320 | |
|
321 | 0 | uniqueIdVendor = ComponentSupport.getViewRoot(ctx, parent); |
322 | |
} |
323 | |
} |
324 | 0 | if (uniqueIdVendor != null) |
325 | |
{ |
326 | |
|
327 | |
|
328 | 0 | String uid = uniqueIdVendor.createUniqueId(facesContext, componentId); |
329 | 0 | c.setId(uid); |
330 | |
} |
331 | |
} |
332 | |
|
333 | 0 | if (this._rendererType != null) |
334 | |
{ |
335 | 0 | c.setRendererType(this._rendererType); |
336 | |
} |
337 | |
|
338 | |
|
339 | 0 | _delegate.onComponentCreated(ctx, c, parent); |
340 | |
|
341 | 0 | if (_relocatableResourceHandler != null && |
342 | |
_relocatableResourceHandler instanceof ComponentRelocatableResourceHandler) |
343 | |
{ |
344 | 0 | UIComponent parentCompositeComponent |
345 | |
= mctx.getCompositeComponentFromStack(); |
346 | 0 | if (parentCompositeComponent != null) |
347 | |
{ |
348 | 0 | c.getAttributes().put(CompositeComponentELUtils.LOCATION_KEY, |
349 | |
parentCompositeComponent.getAttributes().get(CompositeComponentELUtils.LOCATION_KEY)); |
350 | |
} |
351 | |
} |
352 | |
|
353 | 0 | if (mctx.isRefreshingTransientBuild() && _relocatableResourceHandler != null) |
354 | |
{ |
355 | 0 | mctx.markRelocatableResourceForDeletion(c); |
356 | |
} |
357 | |
} |
358 | 0 | c.pushComponentToEL(facesContext, c); |
359 | |
|
360 | 0 | if (c instanceof UniqueIdVendor) |
361 | |
{ |
362 | 0 | mctx.pushUniqueIdVendorToStack((UniqueIdVendor)c); |
363 | |
} |
364 | |
|
365 | 0 | if (mctx.isDynamicComponentTopLevel()) |
366 | |
{ |
367 | 0 | mctx.setDynamicComponentTopLevel(false); |
368 | 0 | _delegate.applyNextHandler(ctx, c); |
369 | 0 | mctx.setDynamicComponentTopLevel(true); |
370 | |
} |
371 | |
else |
372 | |
{ |
373 | |
|
374 | 0 | _delegate.applyNextHandler(ctx, c); |
375 | |
} |
376 | |
|
377 | 0 | boolean oldProcessingEvents = facesContext.isProcessingEvents(); |
378 | |
|
379 | 0 | if (componentFound && !mctx.isDynamicComponentTopLevel()) |
380 | |
{ |
381 | 0 | mctx.finalizeForDeletion(c); |
382 | |
|
383 | |
|
384 | |
|
385 | 0 | if (mctx.isRefreshingSection()) |
386 | |
{ |
387 | 0 | facesContext.setProcessingEvents(false); |
388 | 0 | if (_relocatableResourceHandler != null && |
389 | |
parent != null && !parent.equals(c.getParent())) |
390 | |
{ |
391 | |
|
392 | 0 | parent = c.getParent(); |
393 | |
|
394 | |
|
395 | |
|
396 | 0 | UIComponent c1 = ComponentSupport.findChildInChildrenByTagId(parent, id); |
397 | 0 | if (c1 == null) |
398 | |
{ |
399 | 0 | facetName = ComponentSupport.findChildInFacetsByTagId(parent, id); |
400 | |
} |
401 | |
else |
402 | |
{ |
403 | 0 | facetName = null; |
404 | |
} |
405 | |
} |
406 | 0 | ComponentSupport.setCachedFacesContext(c, facesContext); |
407 | |
} |
408 | 0 | if (facetName == null) |
409 | |
{ |
410 | 0 | parent.getChildren().remove(c); |
411 | |
} |
412 | |
else |
413 | |
{ |
414 | 0 | ComponentSupport.removeFacet(ctx, parent, c, facetName); |
415 | |
} |
416 | 0 | if (mctx.isRefreshingSection()) |
417 | |
{ |
418 | 0 | ComponentSupport.setCachedFacesContext(c, null); |
419 | 0 | facesContext.setProcessingEvents(oldProcessingEvents); |
420 | |
} |
421 | |
|
422 | |
} |
423 | |
|
424 | |
|
425 | 0 | if (!componentFound) |
426 | |
{ |
427 | 0 | if (c instanceof ClientBehaviorHolder && !UIComponent.isCompositeComponent(c)) |
428 | |
{ |
429 | 0 | Iterator<AjaxHandler> it = ((AbstractFaceletContext) ctx).getAjaxHandlers(); |
430 | 0 | if (it != null) |
431 | |
{ |
432 | 0 | while(it.hasNext()) |
433 | |
{ |
434 | 0 | it.next().applyAttachedObject(facesContext, c); |
435 | |
} |
436 | |
} |
437 | |
} |
438 | |
|
439 | 0 | if (c instanceof EditableValueHolder) |
440 | |
{ |
441 | |
|
442 | |
|
443 | 0 | addEnclosingAndDefaultValidators(ctx, mctx, facesContext, (EditableValueHolder) c); |
444 | |
} |
445 | |
} |
446 | |
|
447 | 0 | _delegate.onComponentPopulated(ctx, c, oldParent); |
448 | |
|
449 | 0 | if (!mctx.isDynamicComponentTopLevel() || !componentFound) |
450 | |
{ |
451 | 0 | if (componentFound && mctx.isRefreshingSection()) |
452 | |
{ |
453 | 0 | facesContext.setProcessingEvents(false); |
454 | 0 | ComponentSupport.setCachedFacesContext(c, facesContext); |
455 | |
} |
456 | 0 | if (facetName == null) |
457 | |
{ |
458 | 0 | parent.getChildren().add(c); |
459 | |
} |
460 | |
else |
461 | |
{ |
462 | 0 | ComponentSupport.addFacet(ctx, parent, c, facetName); |
463 | |
} |
464 | 0 | if (componentFound && mctx.isRefreshingSection()) |
465 | |
{ |
466 | 0 | ComponentSupport.setCachedFacesContext(c, null); |
467 | 0 | facesContext.setProcessingEvents(oldProcessingEvents); |
468 | |
} |
469 | |
} |
470 | |
|
471 | 0 | if (c instanceof UniqueIdVendor) |
472 | |
{ |
473 | 0 | mctx.popUniqueIdVendorToStack(); |
474 | |
} |
475 | |
|
476 | 0 | c.popComponentFromEL(facesContext); |
477 | |
|
478 | 0 | if (mctx.isMarkInitialState()) |
479 | |
{ |
480 | |
|
481 | 0 | c.markInitialState(); |
482 | |
} |
483 | 0 | } |
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
protected final String getFacetName(FaceletContext ctx, UIComponent parent) |
492 | |
{ |
493 | 0 | return (String) parent.getAttributes().get(FacetHandler.KEY); |
494 | |
} |
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
protected UIComponent createComponent(FaceletContext ctx) |
508 | |
{ |
509 | 0 | if (_componentBuilderHandlerDelegate != null) |
510 | |
{ |
511 | |
|
512 | |
|
513 | 0 | return _componentBuilderHandlerDelegate.createComponent(ctx); |
514 | |
} |
515 | 0 | UIComponent c = null; |
516 | 0 | FacesContext faces = ctx.getFacesContext(); |
517 | 0 | Application app = faces.getApplication(); |
518 | 0 | if (_delegate.getBinding() != null) |
519 | |
{ |
520 | 0 | ValueExpression ve = _delegate.getBinding().getValueExpression(ctx, Object.class); |
521 | 0 | if (PhaseId.RESTORE_VIEW.equals(faces.getCurrentPhaseId())) |
522 | |
{ |
523 | 0 | if (!ve.isReadOnly(faces.getELContext())) |
524 | |
{ |
525 | |
try |
526 | |
{ |
527 | |
|
528 | |
|
529 | 0 | ve.setValue(faces.getELContext(), null); |
530 | |
} |
531 | 0 | catch (Exception e) |
532 | |
{ |
533 | |
|
534 | 0 | } |
535 | |
} |
536 | |
} |
537 | 0 | if (this._rendererType == null) |
538 | |
{ |
539 | 0 | c = app.createComponent(ve, faces, this._componentType); |
540 | |
} |
541 | |
else |
542 | |
{ |
543 | 0 | c = app.createComponent(ve, faces, this._componentType, this._rendererType); |
544 | |
} |
545 | 0 | if (c != null) |
546 | |
{ |
547 | 0 | c.setValueExpression("binding", ve); |
548 | |
|
549 | 0 | if (!ve.isReadOnly(faces.getELContext())) |
550 | |
{ |
551 | 0 | ComponentSupport.getViewRoot(ctx, c).getAttributes().put("oam.CALL_PRE_DISPOSE_VIEW", Boolean.TRUE); |
552 | 0 | c.subscribeToEvent(PreDisposeViewEvent.class, new ClearBindingValueExpressionListener()); |
553 | |
} |
554 | |
|
555 | 0 | if (c.getChildCount() > 0 || c.getFacetCount() > 0) |
556 | |
{ |
557 | |
|
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
|
563 | 0 | c.getAttributes().put(DefaultFaceletsStateManagementStrategy.COMPONENT_ADDED_AFTER_BUILD_VIEW, |
564 | |
ComponentState.REMOVE_ADD); |
565 | |
|
566 | 0 | if (FaceletViewDeclarationLanguageBase.isDynamicComponentNeedsRefresh(ctx.getFacesContext())) |
567 | |
{ |
568 | 0 | FaceletViewDeclarationLanguageBase.resetDynamicComponentNeedsRefreshFlag( |
569 | |
ctx.getFacesContext()); |
570 | 0 | FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx); |
571 | 0 | if (mctx.isUsingPSSOnThisView()) |
572 | |
{ |
573 | 0 | FaceletViewDeclarationLanguage.cleanTransientBuildOnRestore(faces); |
574 | |
} |
575 | |
else |
576 | |
{ |
577 | 0 | FaceletViewDeclarationLanguageBase.activateDynamicComponentRefreshTransientBuild(faces); |
578 | |
} |
579 | |
|
580 | |
|
581 | |
|
582 | |
|
583 | 0 | c.getAttributes().put( |
584 | |
FaceletDynamicComponentRefreshTransientBuildEvent. |
585 | |
DYNAMIC_COMPONENT_BINDING_NEEDS_REFRESH, |
586 | |
Boolean.TRUE); |
587 | |
} |
588 | |
} |
589 | |
} |
590 | 0 | } |
591 | |
else |
592 | |
{ |
593 | |
|
594 | |
|
595 | |
|
596 | |
|
597 | |
|
598 | |
|
599 | |
|
600 | |
|
601 | |
|
602 | |
|
603 | |
|
604 | |
|
605 | 0 | c = app.createComponent(faces, this._componentType, this._rendererType); |
606 | |
|
607 | |
} |
608 | 0 | return c; |
609 | |
} |
610 | |
|
611 | |
|
612 | |
|
613 | |
|
614 | |
|
615 | |
|
616 | |
|
617 | |
|
618 | |
|
619 | |
protected String getId(FaceletContext ctx) |
620 | |
{ |
621 | 0 | if (this._id != null) |
622 | |
{ |
623 | 0 | return this._id.getValue(ctx); |
624 | |
} |
625 | 0 | return ctx.generateUniqueId(_delegate.getTagId()); |
626 | |
} |
627 | |
|
628 | |
@Override |
629 | |
public MetaRuleset createMetaRuleset(Class type) |
630 | |
{ |
631 | 0 | MetaRuleset m = new MetaRulesetImpl(_delegate.getTag(), type); |
632 | |
|
633 | 0 | m.ignore("binding").ignore("id"); |
634 | |
|
635 | |
|
636 | 0 | m.addRule(ComponentRule.INSTANCE); |
637 | |
|
638 | |
|
639 | 0 | m.addRule(PassthroughRuleImpl.INSTANCE); |
640 | |
|
641 | |
|
642 | 0 | if (ActionSource.class.isAssignableFrom(type)) |
643 | |
{ |
644 | 0 | m.addRule(ActionSourceRule.INSTANCE); |
645 | |
} |
646 | |
|
647 | |
|
648 | 0 | if (ValueHolder.class.isAssignableFrom(type)) |
649 | |
{ |
650 | 0 | m.addRule(ValueHolderRule.INSTANCE); |
651 | |
|
652 | |
|
653 | 0 | if (EditableValueHolder.class.isAssignableFrom(type)) |
654 | |
{ |
655 | 0 | m.ignore("submittedValue"); |
656 | 0 | m.ignore("valid"); |
657 | 0 | m.addRule(EditableValueHolderRule.INSTANCE); |
658 | |
} |
659 | |
} |
660 | |
|
661 | |
|
662 | 0 | m.alias("class", "styleClass"); |
663 | |
|
664 | 0 | return m; |
665 | |
} |
666 | |
|
667 | |
|
668 | |
|
669 | |
|
670 | |
|
671 | |
|
672 | |
|
673 | |
|
674 | |
|
675 | |
|
676 | |
private void addEnclosingAndDefaultValidators(FaceletContext ctx, |
677 | |
FaceletCompositionContext mctx, |
678 | |
FacesContext context, |
679 | |
EditableValueHolder component) |
680 | |
{ |
681 | |
|
682 | 0 | Iterator<Map.Entry<String, EditableValueHolderAttachedObjectHandler>> enclosingValidatorIds = |
683 | |
mctx.getEnclosingValidatorIdsAndHandlers(); |
684 | 0 | if (enclosingValidatorIds != null) |
685 | |
{ |
686 | 0 | while (enclosingValidatorIds.hasNext()) |
687 | |
{ |
688 | 0 | Map.Entry<String, EditableValueHolderAttachedObjectHandler> entry = enclosingValidatorIds.next(); |
689 | 0 | addEnclosingValidator(context, component, entry.getKey(), entry.getValue()); |
690 | 0 | } |
691 | |
} |
692 | |
|
693 | 0 | Map<String, String> defaultValidators = context.getApplication().getDefaultValidatorInfo(); |
694 | 0 | if (defaultValidators != null && defaultValidators.size() != 0) |
695 | |
{ |
696 | 0 | for (Map.Entry<String, String> entry : defaultValidators.entrySet()) |
697 | |
{ |
698 | 0 | if (!mctx.containsEnclosingValidatorId(entry.getKey())) |
699 | |
{ |
700 | 0 | addDefaultValidator(ctx, mctx, context, component, entry.getKey(), entry.getValue()); |
701 | |
} |
702 | 0 | } |
703 | |
} |
704 | 0 | } |
705 | |
|
706 | |
private void addDefaultValidator(FaceletContext ctx, FaceletCompositionContext mctx, FacesContext context, |
707 | |
EditableValueHolder component, String validatorId, String validatorClassName) |
708 | |
{ |
709 | 0 | Validator enclosingValidator = null; |
710 | |
|
711 | 0 | if (validatorClassName == null) |
712 | |
{ |
713 | |
|
714 | |
|
715 | |
|
716 | 0 | enclosingValidator = context.getApplication().createValidator(validatorId); |
717 | 0 | validatorClassName = enclosingValidator.getClass().getName(); |
718 | |
} |
719 | |
|
720 | |
|
721 | |
|
722 | 0 | Validator validator = null; |
723 | 0 | for (Validator v : component.getValidators()) |
724 | |
{ |
725 | 0 | if (v.getClass().getName().equals(validatorClassName)) |
726 | |
{ |
727 | |
|
728 | 0 | validator = v; |
729 | 0 | break; |
730 | |
} |
731 | |
} |
732 | |
|
733 | 0 | if (validator == null) |
734 | |
{ |
735 | 0 | if (shouldAddDefaultValidator(ctx, mctx, component, validatorId)) |
736 | |
{ |
737 | 0 | if (enclosingValidator != null) |
738 | |
{ |
739 | |
|
740 | 0 | validator = enclosingValidator; |
741 | |
} |
742 | |
else |
743 | |
{ |
744 | |
|
745 | 0 | validator = context.getApplication().createValidator(validatorId); |
746 | |
} |
747 | |
|
748 | 0 | component.addValidator(validator); |
749 | |
} |
750 | |
else |
751 | |
{ |
752 | |
|
753 | 0 | return; |
754 | |
} |
755 | |
} |
756 | |
|
757 | |
|
758 | 0 | if (validator instanceof BeanValidator) |
759 | |
{ |
760 | 0 | BeanValidator beanValidator = (BeanValidator) validator; |
761 | |
|
762 | |
|
763 | 0 | String validationGroups = beanValidator.getValidationGroups(); |
764 | 0 | if (validationGroups == null |
765 | |
|| validationGroups.matches(BeanValidator.EMPTY_VALIDATION_GROUPS_PATTERN)) |
766 | |
{ |
767 | |
|
768 | |
|
769 | |
|
770 | |
|
771 | |
|
772 | |
|
773 | |
|
774 | |
|
775 | |
|
776 | |
|
777 | |
|
778 | 0 | validationGroups = javax.validation.groups.Default.class.getName(); |
779 | |
|
780 | 0 | beanValidator.setValidationGroups(validationGroups); |
781 | |
} |
782 | |
} |
783 | 0 | } |
784 | |
|
785 | |
|
786 | |
|
787 | |
|
788 | |
|
789 | |
|
790 | |
|
791 | |
|
792 | |
|
793 | |
|
794 | |
@SuppressWarnings("unchecked") |
795 | |
private boolean shouldAddDefaultValidator(FaceletContext ctx, FaceletCompositionContext mctx, |
796 | |
EditableValueHolder component, |
797 | |
String validatorId) |
798 | |
{ |
799 | |
|
800 | 0 | List<String> exclusionList |
801 | |
= (List<String>) ((UIComponent) component).getAttributes() |
802 | |
.get(ValidatorTagHandlerDelegate.VALIDATOR_ID_EXCLUSION_LIST_KEY); |
803 | 0 | if (exclusionList != null) |
804 | |
{ |
805 | 0 | for (String excludedId : exclusionList) |
806 | |
{ |
807 | 0 | if (excludedId.equals(validatorId)) |
808 | |
{ |
809 | 0 | return false; |
810 | |
} |
811 | 0 | } |
812 | |
} |
813 | |
|
814 | |
|
815 | |
|
816 | |
|
817 | |
|
818 | |
|
819 | |
|
820 | |
|
821 | |
|
822 | |
|
823 | |
|
824 | |
|
825 | |
|
826 | |
|
827 | |
|
828 | 0 | Iterator<Map.Entry<String, EditableValueHolderAttachedObjectHandler>> enclosingValidatorIds = |
829 | |
mctx.getEnclosingValidatorIdsAndHandlers(); |
830 | 0 | if (enclosingValidatorIds != null) |
831 | |
{ |
832 | 0 | while (enclosingValidatorIds.hasNext()) |
833 | |
{ |
834 | 0 | Map.Entry<String, EditableValueHolderAttachedObjectHandler> entry = enclosingValidatorIds.next(); |
835 | 0 | boolean validatorIdAvailable = entry.getKey() != null && !"".equals(entry.getKey()); |
836 | 0 | if (validatorIdAvailable && entry.getKey().equals(validatorId)) |
837 | |
{ |
838 | 0 | if (((ValidatorHandler)((FacesWrapper<ValidatorHandler>)entry.getValue()).getWrapped()) |
839 | |
.isDisabled(ctx)) |
840 | |
{ |
841 | 0 | return false; |
842 | |
} |
843 | |
} |
844 | 0 | } |
845 | |
} |
846 | |
|
847 | |
|
848 | 0 | if (validatorId.equals(BeanValidator.VALIDATOR_ID)) |
849 | |
{ |
850 | 0 | if (!ExternalSpecifications.isBeanValidationAvailable()) |
851 | |
{ |
852 | |
|
853 | |
|
854 | |
|
855 | 0 | log.log(Level.WARNING, "Bean validation is not available on the " + |
856 | |
"classpath, thus the BeanValidator will not be added for " + |
857 | |
"the component " + component); |
858 | 0 | return false; |
859 | |
} |
860 | |
} |
861 | |
|
862 | |
|
863 | 0 | return true; |
864 | |
} |
865 | |
|
866 | |
private void addEnclosingValidator(FacesContext context, |
867 | |
EditableValueHolder component, String validatorId, |
868 | |
EditableValueHolderAttachedObjectHandler attachedObjectHandler) |
869 | |
{ |
870 | 0 | if (shouldAddEnclosingValidator(component, validatorId)) |
871 | |
{ |
872 | 0 | if (attachedObjectHandler != null) |
873 | |
{ |
874 | 0 | attachedObjectHandler.applyAttachedObject(context, (UIComponent) component); |
875 | |
} |
876 | |
else |
877 | |
{ |
878 | 0 | Validator validator = null; |
879 | |
|
880 | 0 | validator = context.getApplication().createValidator(validatorId); |
881 | |
|
882 | |
|
883 | 0 | if (validator instanceof BeanValidator) |
884 | |
{ |
885 | 0 | BeanValidator beanValidator = (BeanValidator) validator; |
886 | |
|
887 | |
|
888 | 0 | String validationGroups = beanValidator.getValidationGroups(); |
889 | 0 | if (validationGroups == null |
890 | |
|| validationGroups.matches(BeanValidator.EMPTY_VALIDATION_GROUPS_PATTERN)) |
891 | |
{ |
892 | |
|
893 | |
|
894 | |
|
895 | |
|
896 | |
|
897 | |
|
898 | |
|
899 | |
|
900 | |
|
901 | |
|
902 | |
|
903 | 0 | validationGroups = javax.validation.groups.Default.class.getName(); |
904 | |
|
905 | 0 | beanValidator.setValidationGroups(validationGroups); |
906 | |
} |
907 | |
} |
908 | |
|
909 | |
|
910 | 0 | component.addValidator(validator); |
911 | |
} |
912 | |
} |
913 | 0 | } |
914 | |
|
915 | |
|
916 | |
|
917 | |
|
918 | |
|
919 | |
|
920 | |
|
921 | |
|
922 | |
|
923 | |
|
924 | |
|
925 | |
|
926 | |
|
927 | |
|
928 | |
@SuppressWarnings("unchecked") |
929 | |
private boolean shouldAddEnclosingValidator( |
930 | |
EditableValueHolder component, |
931 | |
String validatorId) |
932 | |
{ |
933 | |
|
934 | 0 | List<String> exclusionList = (List<String>) ((UIComponent) component) |
935 | |
.getAttributes() |
936 | |
.get(ValidatorTagHandlerDelegate.VALIDATOR_ID_EXCLUSION_LIST_KEY); |
937 | 0 | if (exclusionList != null) |
938 | |
{ |
939 | 0 | for (String excludedId : exclusionList) |
940 | |
{ |
941 | 0 | if (excludedId.equals(validatorId)) |
942 | |
{ |
943 | 0 | return false; |
944 | |
} |
945 | 0 | } |
946 | |
} |
947 | |
|
948 | |
|
949 | 0 | if (validatorId.equals(BeanValidator.VALIDATOR_ID) && |
950 | |
!ExternalSpecifications.isBeanValidationAvailable()) |
951 | |
{ |
952 | |
|
953 | |
|
954 | |
|
955 | 0 | log.log(Level.WARNING, |
956 | |
"Bean validation is not available on the " |
957 | |
+ "classpath, thus the BeanValidator will not be added for " |
958 | |
+ "the component " + component); |
959 | 0 | return false; |
960 | |
} |
961 | |
|
962 | |
|
963 | 0 | return true; |
964 | |
} |
965 | |
|
966 | 0 | private static class PublishFaceletDynamicComponentRefreshTransientBuildCallback implements VisitCallback |
967 | |
{ |
968 | |
public VisitResult visit(VisitContext context, UIComponent target) |
969 | |
{ |
970 | 0 | context.getFacesContext().getApplication().publishEvent( |
971 | |
context.getFacesContext(), FaceletDynamicComponentRefreshTransientBuildEvent.class, |
972 | |
target.getClass(), target); |
973 | 0 | return VisitResult.ACCEPT; |
974 | |
} |
975 | |
} |
976 | |
} |