1 20 package org.enhydra.barracuda.core.forms.validators; 21 22 import java.util.*; 23 24 import org.enhydra.barracuda.core.forms.*; 25 import org.enhydra.barracuda.plankton.*; 26 27 30 public class MaxLengthValidator extends DefaultFormValidator { 31 32 protected int max = 0; 33 34 37 public MaxLengthValidator() { 38 this(0, null); 39 } 40 41 46 public MaxLengthValidator(int imax) { 47 this(imax, null); 48 } 49 50 56 public MaxLengthValidator(int imax, String ierrmsg) { 57 super(ierrmsg); 58 max = imax; 59 } 60 61 67 public int getMaxLength() { 68 return max; 69 } 70 71 84 public void validateFormElement(Object val, FormElement element, boolean deferExceptions) throws ValidationException { 85 if (this.isNull(val, element)) 90 return; 91 92 if (element==null) throw new ValidationException(val, "Object val:"+val+" is associated with a null FormElement"); 96 if (element.getType().equals(FormType.BOOLEAN)) throw new ValidationException(val, "Unsupported validation: "+val+" is of FormType.BOOLEAN and cannot be validated by this validator"); 97 if (element.getType().equals(FormType.DATE)) throw new ValidationException(val, "Unsupported validation: "+val+" is of FormType.DATE and cannot be validated by this validator"); 98 99 String s = val.toString(); 101 if (localLogger.isInfoEnabled()) localLogger.info("validating to see if length of val {"+s+"} <= "+max); 102 if (s.length()>max) { 103 throw this.generateException(element, deferExceptions, "Length of val {"+val+"} exceeds maximum length of "+max); 104 } 105 } 106 107 } 108 | Popular Tags |