1 16 package javax.faces.validator; 17 18 import javax.faces.component.StateHolder; 19 import javax.faces.component.UIComponent; 20 import javax.faces.context.FacesContext; 21 22 34 public class LengthValidator 35 implements Validator, StateHolder 36 { 37 public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MAXIMUM"; 39 public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MINIMUM"; 40 public static final String VALIDATOR_ID = "javax.faces.Length"; 41 42 private Integer _minimum = null; 43 private Integer _maximum = null; 44 private boolean _transient = false; 45 46 public LengthValidator() 48 { 49 } 50 51 public LengthValidator(int maximum) 52 { 53 _maximum = new Integer (maximum); 54 } 55 56 public LengthValidator(int maximum, 57 int minimum) 58 { 59 _maximum = new Integer (maximum); 60 _minimum = new Integer (minimum); 61 } 62 63 public void validate(FacesContext facesContext, 65 UIComponent uiComponent, 66 Object value) 67 throws ValidatorException 68 { 69 if (facesContext == null) throw new NullPointerException ("facesContext"); 70 if (uiComponent == null) throw new NullPointerException ("uiComponent"); 71 72 if (value == null) 73 { 74 return; 75 } 76 77 int length = value instanceof String ? 78 ((String )value).length() : value.toString().length(); 79 80 if (_minimum != null) 81 { 82 if (length < _minimum.intValue()) 83 { 84 Object [] args = {_minimum,uiComponent.getId()}; 85 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args)); 86 } 87 } 88 89 if (_maximum != null) 90 { 91 if (length > _maximum.intValue()) 92 { 93 Object [] args = {_maximum,uiComponent.getId()}; 94 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args)); 95 } 96 } 97 } 98 99 public int getMaximum() 101 { 102 return _maximum != null ? _maximum.intValue() : Integer.MAX_VALUE; 103 } 104 105 public void setMaximum(int maximum) 106 { 107 _maximum = new Integer (maximum); 108 } 109 110 public int getMinimum() 111 { 112 return _minimum != null ? _minimum.intValue() : 0; 113 } 114 115 public void setMinimum(int minimum) 116 { 117 _minimum = new Integer (minimum); 118 } 119 120 public boolean isTransient() 121 { 122 return _transient; 123 } 124 125 public void setTransient(boolean transientValue) 126 { 127 _transient = transientValue; 128 } 129 130 public Object saveState(FacesContext context) 132 { 133 Object values[] = new Object [2]; 134 values[0] = _maximum; 135 values[1] = _minimum; 136 return values; 137 } 138 139 public void restoreState(FacesContext context, 140 Object state) 141 { 142 Object values[] = (Object [])state; 143 _maximum = (Integer )values[0]; 144 _minimum = (Integer )values[1]; 145 } 146 147 public boolean equals(Object o) 149 { 150 if (this == o) return true; 151 if (!(o instanceof LengthValidator)) return false; 152 153 final LengthValidator lengthValidator = (LengthValidator)o; 154 155 if (_maximum != null ? !_maximum.equals(lengthValidator._maximum) : lengthValidator._maximum != null) return false; 156 if (_minimum != null ? !_minimum.equals(lengthValidator._minimum) : lengthValidator._minimum != null) return false; 157 158 return true; 159 } 160 } 161 | Popular Tags |