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 RangeValidator extends DefaultFormValidator { 31 32 protected Number n1 = null; 33 protected Number n2 = null; 34 35 38 public RangeValidator() { 39 this(null, null, null); 40 } 41 42 48 public RangeValidator(Number low, Number high) { 49 this(low, high, null); 50 } 51 52 59 public RangeValidator(Number low, Number high, String ierrmsg) { 60 super(ierrmsg); 61 n1 = low; 62 n2 = high; 63 } 64 65 71 public Number getMinimum() { 72 return n1; 73 } 74 75 81 public Number getMaximum() { 82 return n2; 83 } 84 85 97 public void validateFormElement(Object val, FormElement element, boolean deferExceptions) throws ValidationException { 98 if (n1==null || n2==null) { 100 throw this.generateException(element, deferExceptions, "Invalid range:"+n1+" to "+n2); 101 } 102 if (this.isNull(val, element)) 107 return; 108 110 if (localLogger.isInfoEnabled()) localLogger.info("validating val="+val+" is in range between "+n1+" and "+n2); 112 FormType formType = element.getType(); 113 String defaultErrMsg = "Value: "+val+ " is not in the range between "+n1+" and "+n2; 114 116 if (element.getParseException()==null) { 118 if (formType==FormType.INTEGER) { 119 int i = ((Integer )element.getVal()).intValue(); 122 if (i<n1.intValue() || i>n2.intValue()) throw this.generateException(element, deferExceptions, defaultErrMsg); 123 124 } else if (formType==FormType.LONG) { 125 long l = ((Long )element.getVal()).longValue(); 128 if (l<n1.longValue() || l>n2.longValue()) throw this.generateException(element, deferExceptions, defaultErrMsg); 129 130 } else if (formType==FormType.SHORT) { 131 short s = ((Short )element.getVal()).shortValue(); 134 if (s<n1.shortValue() || s>n2.shortValue()) throw this.generateException(element, deferExceptions, defaultErrMsg); 135 136 } else if (formType==FormType.DOUBLE) { 137 double d = ((Double )element.getVal()).doubleValue(); 140 if (d<n1.doubleValue() || d>n2.doubleValue()) throw this.generateException(element, deferExceptions, defaultErrMsg); 141 142 } else if (formType==FormType.FLOAT) { 143 float f = ((Float )element.getVal()).floatValue(); 146 if (f<n1.floatValue() || f>n2.floatValue()) throw this.generateException(element, deferExceptions, defaultErrMsg); 147 148 } else { 149 throw this.generateException(element, deferExceptions, "Invalid form type:"+formType); 150 } 151 } 152 else { 153 throw this.generateException(element, deferExceptions, "Parse error " + element.getParseException()); 154 } 155 } 157 158 } 159 | Popular Tags |