1 package org.apache.turbine.services.intake.validator; 2 3 18 19 import java.util.Map ; 20 21 import org.apache.commons.lang.StringUtils; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 44 abstract public class DefaultValidator 45 implements Validator, InitableByConstraintMap 46 { 47 48 protected boolean required = false; 49 50 51 protected String requiredMessage = null; 52 53 54 protected int minLength = 0; 55 56 57 protected String minLengthMessage = null; 58 59 60 protected int maxLength = 0; 61 62 63 protected String maxLengthMessage = null; 64 65 66 protected String errorMessage = null; 67 68 69 protected Log log = LogFactory.getLog(this.getClass()); 70 71 79 public DefaultValidator(Map paramMap) 80 throws InvalidMaskException 81 { 82 init(paramMap); 83 } 84 85 88 public DefaultValidator() 89 { 90 } 91 92 100 public void init(Map paramMap) 101 throws InvalidMaskException 102 { 103 Constraint constraint = (Constraint) paramMap.get(REQUIRED_RULE_NAME); 104 if (constraint != null) 105 { 106 String param = constraint.getValue(); 107 required = new Boolean (param).booleanValue(); 108 requiredMessage = constraint.getMessage(); 109 } 110 111 constraint = (Constraint) paramMap.get(MIN_LENGTH_RULE_NAME); 112 if (constraint != null) 113 { 114 String param = constraint.getValue(); 115 minLength = Integer.parseInt(param); 116 minLengthMessage = constraint.getMessage(); 117 } 118 119 constraint = (Constraint) paramMap.get(MAX_LENGTH_RULE_NAME); 120 if (constraint != null) 121 { 122 String param = constraint.getValue(); 123 maxLength = Integer.parseInt(param); 124 maxLengthMessage = constraint.getMessage(); 125 } 126 } 127 128 135 public boolean isValid(String testValue) 136 { 137 boolean valid = false; 138 try 139 { 140 assertValidity(testValue); 141 valid = true; 142 } 143 catch (ValidationException ve) 144 { 145 valid = false; 146 } 147 return valid; 148 } 149 150 158 public void assertValidity(String testValue) 159 throws ValidationException 160 { 161 if (!required && StringUtils.isEmpty(testValue)) 162 { 163 return; 164 } 165 if (required && StringUtils.isEmpty(testValue)) 166 { 167 errorMessage = requiredMessage; 168 throw new ValidationException(requiredMessage); 169 } 170 171 if (minLength > 0 && testValue.length() < minLength) 172 { 173 errorMessage = minLengthMessage; 174 throw new ValidationException(minLengthMessage); 175 } 176 if (maxLength > 0 && testValue.length() > maxLength) 177 { 178 errorMessage = maxLengthMessage; 179 throw new ValidationException(maxLengthMessage); 180 } 181 } 182 183 184 189 public String getMessage() 190 { 191 String retValue = ""; 192 193 if(errorMessage != null) 194 { 195 retValue = errorMessage; 196 } 197 198 return retValue; 199 } 200 201 205 210 public boolean isRequired() 211 { 212 return required; 213 } 214 215 220 public void setRequired(boolean required) 221 { 222 this.required = required; 223 } 224 225 230 public String getRequiredMessage() 231 { 232 return requiredMessage; 233 } 234 235 240 public void setRequiredMessage(String requiredMessage) 241 { 242 this.requiredMessage = requiredMessage; 243 } 244 245 250 public int getMinLength() 251 { 252 return minLength; 253 } 254 255 260 public void setMinLength(int minLength) 261 { 262 this.minLength = minLength; 263 } 264 265 270 public String getMinLengthMessage() 271 { 272 return minLengthMessage; 273 } 274 275 280 public void setMinLengthMessage(String minLengthMessage) 281 { 282 this.minLengthMessage = minLengthMessage; 283 } 284 285 290 public int getMaxLength() 291 { 292 return maxLength; 293 } 294 295 300 public void setMaxLength(int maxLength) 301 { 302 this.maxLength = maxLength; 303 } 304 305 310 public String getMaxLengthMessage() 311 { 312 return maxLengthMessage; 313 } 314 315 320 public void setMaxLengthMessage(String maxLengthMessage) 321 { 322 this.maxLengthMessage = maxLengthMessage; 323 } 324 } 325 | Popular Tags |