1 16 package org.apache.myfaces.custom.regexprvalidator; 17 18 import javax.faces.application.FacesMessage; 19 import javax.faces.component.StateHolder; 20 import javax.faces.component.UIComponent; 21 import javax.faces.context.FacesContext; 22 import javax.faces.validator.Validator; 23 import javax.faces.validator.ValidatorException; 24 25 import org.apache.myfaces.util.MessageUtils; 26 27 import org.apache.commons.validator.GenericValidator; 28 29 47 public class RegExprValidator implements Validator, StateHolder { 48 51 public static final String VALIDATOR_ID = "org.apache.myfaces.validator.RegExpr"; 52 53 57 public static final String REGEXPR_MESSAGE_ID = "org.apache.myfaces.Regexpr.INVALID"; 58 59 public RegExprValidator(){ 60 } 61 62 private String _pattern= null; 64 65 66 private boolean _transient = false; 68 69 70 71 public void validate( 72 FacesContext facesContext, 73 UIComponent uiComponent, 74 Object value) 75 throws ValidatorException { 76 77 if (facesContext == null) throw new NullPointerException ("facesContext"); 78 if (uiComponent == null) throw new NullPointerException ("uiComponent"); 79 80 if (value == null) 81 { 82 return; 83 } 84 Object [] args = {value.toString()}; 85 if(!GenericValidator.matchRegexp(value.toString(),"^"+_pattern+"$")){ 86 throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,REGEXPR_MESSAGE_ID, args)); 87 88 } 89 90 } 91 92 93 94 96 public Object saveState(FacesContext context) { 97 Object state = _pattern; 98 return state; 99 } 100 101 public void restoreState(FacesContext context, Object state) { 102 _pattern = (String ) state; 103 } 104 105 public boolean isTransient() { 106 return _transient; 107 } 108 109 public void setTransient(boolean newTransientValue) { 110 _transient = newTransientValue; 111 } 112 114 117 public String getPattern() { 118 return _pattern; 119 } 120 121 124 public void setPattern(String string) { 125 _pattern = string; 126 } 127 128 } 129 | Popular Tags |