1 16 package javax.faces.webapp; 17 18 import javax.faces.application.Application; 19 import javax.faces.component.EditableValueHolder; 20 import javax.faces.component.UIComponent; 21 import javax.faces.context.FacesContext; 22 import javax.faces.el.ValueBinding; 23 import javax.faces.validator.Validator; 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.tagext.Tag ; 26 import javax.servlet.jsp.tagext.TagSupport ; 27 28 32 public class ValidatorTag 33 extends TagSupport 34 { 35 private String _validatorId; 36 37 public void setValidatorId(String validatorId) 38 { 39 _validatorId = validatorId; 40 } 41 42 public int doStartTag() 43 throws javax.servlet.jsp.JspException 44 { 45 UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext); 46 if (componentTag == null) 47 { 48 throw new JspException ("no parent UIComponentTag found"); 49 } 50 if (!componentTag.getCreated()) 51 { 52 return Tag.SKIP_BODY; 53 } 54 55 Validator validator = createValidator(); 56 57 UIComponent component = componentTag.getComponentInstance(); 58 if (component == null) 59 { 60 throw new JspException ("parent UIComponentTag has no UIComponent"); 61 } 62 if (!(component instanceof EditableValueHolder)) 63 { 64 throw new JspException ("UIComponent is no ValueHolder"); 65 } 66 ((EditableValueHolder)component).addValidator(validator); 67 68 return Tag.SKIP_BODY; 69 } 70 71 public void release() 72 { 73 super.release(); 74 _validatorId = null; 75 } 76 77 protected Validator createValidator() 78 throws JspException 79 { 80 FacesContext facesContext = FacesContext.getCurrentInstance(); 81 Application application = facesContext.getApplication(); 82 if (UIComponentTag.isValueReference(_validatorId)) 83 { 84 ValueBinding vb = facesContext.getApplication().createValueBinding(_validatorId); 85 return application.createValidator((String )vb.getValue(facesContext)); 86 } 87 else 88 { 89 return application.createValidator(_validatorId); 90 } 91 } 92 } 93 | Popular Tags |