KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > validators > RangeValidator


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: RangeValidator.java,v 1.13 2004/02/01 05:16:28 christianc Exp $
19  */

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 /**
28  * This validator ensures that a value is within a given number range
29  */

30 public class RangeValidator extends DefaultFormValidator {
31
32     protected Number JavaDoc n1 = null;
33     protected Number JavaDoc n2 = null;
34
35     /**
36      * Public no-args constructor.
37      */

38     public RangeValidator() {
39         this(null, null, null);
40     }
41
42     /**
43      * Public constructor.
44      *
45      * @param low the low end of the range
46      * @param high the high end of the range
47      */

48     public RangeValidator(Number JavaDoc low, Number JavaDoc high) {
49         this(low, high, null);
50     }
51
52     /**
53      * Public constructor.
54      *
55      * @param low the low end of the range
56      * @param high the high end of the range
57      * @param ierrmsg the message associated with this error
58      */

59     public RangeValidator(Number JavaDoc low, Number JavaDoc high, String JavaDoc ierrmsg) {
60         super(ierrmsg);
61         n1 = low;
62         n2 = high;
63     }
64
65     //bw_102501.1 - added
66
/**
67      * Get the minimum allowed value
68      *
69      * @return Number containing the minimum value
70      */

71     public Number JavaDoc getMinimum() {
72        return n1;
73     }
74
75     //bw_102501.1 - added
76
/**
77      * Get the maximum allowed value
78      *
79      * @return Number containing the maximum value
80      */

81     public Number JavaDoc getMaximum() {
82        return n2;
83     }
84
85     /**
86      * Validate a FormElement to make see if the element equals() a
87      * given object
88      *
89      * @param val the value to compare the element value to
90      * @param element the form element that contains the val
91      * to validate elements by comparing them with other elements)
92      * @param deferExceptions do we want to deferValidation exceptions
93      * and attempt to validate all elements so that we can process
94      * all the exceptions at once
95      * @throws ValidationException if the element is not valid
96      */

97     public void validateFormElement(Object JavaDoc val, FormElement element, boolean deferExceptions) throws ValidationException {
98         //eliminate the obvious
99
if (n1==null || n2==null) {
100             throw this.generateException(element, deferExceptions, "Invalid range:"+n1+" to "+n2);
101         }
102         // cannot validate a null value, use NotNullvalidator if you want to disallow nulls
103
// ilc_022702.1_start
104
// use isNull to test nullness
105
//if (val==null) return;
106
if (this.isNull(val, element))
107           return;
108         // ilc_022702.1_end
109

110         //figure out what type of element we're dealing with
111
if (localLogger.isInfoEnabled()) localLogger.info("validating val="+val+" is in range between "+n1+" and "+n2);
112         FormType formType = element.getType();
113         String JavaDoc defaultErrMsg = "Value: "+val+ " is not in the range between "+n1+" and "+n2;
114         // ilc_022702.2_start
115

116         // make sure parsing was successful
117
if (element.getParseException()==null) {
118           if (formType==FormType.INTEGER) {
119               // val is always origVal need to use getVal
120
//int i = ((Integer) val).intValue();
121
int i = ((Integer JavaDoc)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               // val is always origVal need to use getVal
126
//long l = ((Long) val).longValue();
127
long l = ((Long JavaDoc)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               // val is always origVal need to use getVal
132
//short s = ((Short) val).shortValue();
133
short s = ((Short JavaDoc)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               // val is always origVal need to use getVal
138
//double d = ((Double) val).doubleValue();
139
double d = ((Double JavaDoc)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               // val is always origVal need to use getVal
144
//float f = ((Float) val).floatValue();
145
float f = ((Float JavaDoc)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         // ilc_022702.2_end;
156
}
157
158 }
159
Popular Tags