1 package org.apache.fulcrum.intake.validator; 2 3 56 57 import java.util.Map ; 58 import org.apache.regexp.RE; 59 import org.apache.fulcrum.ServiceException; 60 61 77 public class DefaultValidator 78 implements Validator, InitableByConstraintMap 79 { 80 protected boolean required; 81 protected String requiredMessage; 82 protected RE mask; 83 protected String maskMessage; 84 protected int minLength; 85 protected String minLengthMessage; 86 protected int maxLength; 87 protected String maxLengthMessage; 88 89 protected String message; 90 91 public DefaultValidator(Map paramMap) 92 throws ServiceException 93 { 94 init(paramMap); 95 } 96 97 public DefaultValidator() 98 { 99 } 100 101 102 103 111 public void init(Map paramMap) 112 throws ServiceException 113 { 114 mask = null; 115 maskMessage = null; 116 minLength = 0; 117 minLengthMessage = null; 118 maxLength = 0; 119 maxLengthMessage = null; 120 121 Constraint constraint = (Constraint)paramMap.get("mask"); 122 if ( constraint != null ) 123 { 124 String param = constraint.getValue(); 125 setMask(param); 126 maskMessage = constraint.getMessage(); 127 } 128 129 constraint = (Constraint)paramMap.get("minLength"); 130 if ( constraint != null ) 131 { 132 String param = constraint.getValue(); 133 minLength = Integer.parseInt(param); 134 minLengthMessage = constraint.getMessage(); 135 } 136 137 constraint = (Constraint)paramMap.get("maxLength"); 138 if ( constraint != null ) 139 { 140 String param = constraint.getValue(); 141 maxLength = Integer.parseInt(param); 142 maxLengthMessage = constraint.getMessage(); 143 } 144 145 constraint = (Constraint)paramMap.get("required"); 146 if ( constraint == null ) 147 { 148 required = false; 149 } 150 else 151 { 152 String param = constraint.getValue(); 153 required = new Boolean (param).booleanValue(); 154 requiredMessage = constraint.getMessage(); 155 } 156 } 157 158 165 public boolean isValid(String testValue) 166 { 167 boolean valid = false; 168 try 169 { 170 assertValidity(testValue); 171 valid = true; 172 } 173 catch (ValidationException ve) 174 { 175 valid = false; 176 } 177 return valid; 178 } 179 180 188 public void assertValidity(String testValue) 189 throws ValidationException 190 { 191 message = null; 192 193 if ( (!required && minLength == 0) 194 && ( testValue == null || testValue.length() == 0) ) 195 { 196 return; 197 } 198 else if ( required 199 && ( testValue == null || testValue.length() == 0)) 200 { 201 message = requiredMessage; 202 throw new ValidationException(requiredMessage); 203 } 204 205 doAssertValidity(testValue); 207 208 if ( mask != null && !mask.match(testValue) ) 209 { 210 message = maskMessage; 211 throw new ValidationException(maskMessage); 212 } 213 if ( minLength > 0 && testValue.length() < minLength ) 214 { 215 message = minLengthMessage; 216 throw new ValidationException(minLengthMessage); 217 } 218 if ( maxLength > 0 && testValue.length() > maxLength ) 219 { 220 message = maxLengthMessage; 221 throw new ValidationException(maxLengthMessage); 222 } 223 } 224 225 230 public String getMessage() 231 { 232 if ( message == null ) 233 { 234 return ""; 235 } 236 return message; 237 } 238 239 240 243 protected void doAssertValidity(String testValue) 244 throws ValidationException 245 { 246 } 247 248 252 256 public boolean isRequired() 257 { 258 return required; 259 } 260 261 265 public void setRequired(boolean v) 266 { 267 this.required = v; 268 } 269 270 274 public String getRequiredMessage() 275 { 276 return requiredMessage; 277 } 278 279 283 public void setRequiredMessage(String v) 284 { 285 this.requiredMessage = v; 286 } 287 288 292 public String getMask() 293 { 294 return mask.toString(); 295 } 296 297 301 public void setMask(String v) 302 throws ServiceException 303 { 304 try 305 { 306 mask = new RE(v); 307 } 308 catch (org.apache.regexp.RESyntaxException e) 309 { 310 throw new ServiceException(e); 311 } 312 } 313 314 318 public String getMaskMessage() 319 { 320 return maskMessage; 321 } 322 323 327 public void setMaskMessage(String v) 328 { 329 this.maskMessage = v; 330 } 331 332 336 public int getMinLength() 337 { 338 return minLength; 339 } 340 341 345 public void setMinLength(int v) 346 { 347 this.minLength = v; 348 } 349 350 354 public String getMinLengthMessage() 355 { 356 return minLengthMessage; 357 } 358 359 363 public void setMinLengthMessage(String v) 364 { 365 this.minLengthMessage = v; 366 } 367 368 372 public int getMaxLength() 373 { 374 return maxLength; 375 } 376 377 381 public void setMaxLength(int v) 382 { 383 this.maxLength = v; 384 } 385 386 390 public String getMaxLengthMessage() 391 { 392 return maxLengthMessage; 393 } 394 395 399 public void setMaxLengthMessage(String v) 400 { 401 this.maxLengthMessage = v; 402 } 403 } 404 | Popular Tags |