1 14 15 package com.sun.facelets.tag.jsf; 16 17 import java.io.IOException ; 18 19 import javax.el.ELException; 20 import javax.el.ValueExpression; 21 import javax.faces.FacesException; 22 import javax.faces.component.EditableValueHolder; 23 import javax.faces.component.UIComponent; 24 import javax.faces.validator.Validator; 25 26 import com.sun.facelets.FaceletContext; 27 import com.sun.facelets.FaceletException; 28 import com.sun.facelets.tag.MetaTagHandler; 29 import com.sun.facelets.tag.TagAttribute; 30 import com.sun.facelets.tag.TagConfig; 31 import com.sun.facelets.tag.TagException; 32 import com.sun.facelets.tag.MetaRuleset; 33 34 44 public class ValidateHandler extends MetaTagHandler { 45 46 private final TagAttribute binding; 47 48 private String validatorId; 49 50 55 public ValidateHandler(TagConfig config) { 56 super(config); 57 this.binding = this.getAttribute("binding"); 58 } 59 60 public ValidateHandler(ValidatorConfig config) { 61 this((TagConfig) config); 62 this.validatorId = config.getValidatorId(); 63 } 64 65 71 public final void apply(FaceletContext ctx, UIComponent parent) 72 throws IOException , FacesException, FaceletException, ELException { 73 74 if (parent == null || !(parent instanceof EditableValueHolder)) { 75 throw new TagException(this.tag, 76 "Parent not an instance of EditableValueHolder: " + parent); 77 } 78 79 if (parent.getParent() == null) { 81 EditableValueHolder evh = (EditableValueHolder) parent; 83 ValueExpression ve = null; 84 Validator v = null; 85 if (this.binding != null) { 86 ve = this.binding.getValueExpression(ctx, Validator.class); 87 v = (Validator) ve.getValue(ctx); 88 } 89 if (v == null) { 90 v = this.createValidator(ctx); 91 if (ve != null) { 92 ve.setValue(ctx, v); 93 } 94 } 95 if (v == null) { 96 throw new TagException(this.tag, "No Validator was created"); 97 } 98 this.setAttributes(ctx, v); 99 evh.addValidator(v); 100 } 101 } 102 103 110 protected Validator createValidator(FaceletContext ctx) { 111 if (this.validatorId == null) { 112 throw new TagException( 113 this.tag, 114 "Default behavior invoked of requiring a validator-id passed in the constructor, must override ValidateHandler(ValidatorConfig)"); 115 } 116 return ctx.getFacesContext().getApplication().createValidator( 117 this.validatorId); 118 } 119 120 protected MetaRuleset createMetaRuleset(Class type) { 121 return super.createMetaRuleset(type).ignore("binding"); 122 } 123 124 } 125 | Popular Tags |