001: package org.apache.turbine.services.intake.validator;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Map;
020:
021: import org.apache.commons.lang.StringUtils;
022:
023: /**
024: * Validates Doubles with the following constraints in addition to those
025: * listed in NumberValidator and DefaultValidator.
026: *
027: * <table>
028: * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
029: * <tr><td>minValue</td><td>greater than Double.MIN_VALUE</td>
030: * <td> </td></tr>
031: * <tr><td>maxValue</td><td>less than Double.MAX_VALUE</td>
032: * <td> </td></tr>
033: * <tr><td>invalidNumberMessage</td><td>Some text</td>
034: * <td>Entry was not a valid number</td></tr>
035: * </table>
036: *
037: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
038: * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
039: * @version $Id: DoubleValidator.java 264148 2005-08-29 14:21:04Z henning $
040: */
041: public class DoubleValidator extends NumberValidator {
042: /* Init the minValue to that for a Double */
043: private double minValue = Double.NEGATIVE_INFINITY;
044:
045: /* Init the maxValue to that for a Double */
046: private double maxValue = Double.POSITIVE_INFINITY;
047:
048: /**
049: * Constructor to use when initialising Object
050: *
051: * @param paramMap
052: * @throws InvalidMaskException
053: */
054: public DoubleValidator(Map paramMap) throws InvalidMaskException {
055: invalidNumberMessage = "Entry was not a valid Double";
056: init(paramMap);
057: }
058:
059: /**
060: * Default Constructor
061: */
062: public DoubleValidator() {
063: }
064:
065: /**
066: * Method to initialise Object
067: *
068: * @param paramMap
069: * @throws InvalidMaskException
070: */
071: public void init(Map paramMap) throws InvalidMaskException {
072: super .init(paramMap);
073:
074: Constraint constraint = (Constraint) paramMap
075: .get(MIN_VALUE_RULE_NAME);
076: if (constraint != null) {
077: String param = constraint.getValue();
078: minValue = Double.parseDouble(param);
079: minValueMessage = constraint.getMessage();
080: }
081:
082: constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME);
083: if (constraint != null) {
084: String param = constraint.getValue();
085: maxValue = Double.parseDouble(param);
086: maxValueMessage = constraint.getMessage();
087: }
088: }
089:
090: /**
091: * Determine whether a testValue meets the criteria specified
092: * in the constraints defined for this validator
093: *
094: * @param testValue a <code>String</code> to be tested
095: * @exception ValidationException containing an error message if the
096: * testValue did not pass the validation tests.
097: */
098: public void assertValidity(String testValue)
099: throws ValidationException {
100: super .assertValidity(testValue);
101:
102: double d = 0.0D;
103:
104: if (required || StringUtils.isNotEmpty(testValue)) {
105: try {
106: d = Double.parseDouble(testValue);
107: } catch (RuntimeException e) {
108: errorMessage = invalidNumberMessage;
109: throw new ValidationException(invalidNumberMessage);
110: }
111:
112: if (d < minValue) {
113: errorMessage = minValueMessage;
114: throw new ValidationException(minValueMessage);
115: }
116: if (d > maxValue) {
117: errorMessage = maxValueMessage;
118: throw new ValidationException(maxValueMessage);
119: }
120: }
121: }
122:
123: // ************************************************************
124: // ** Bean accessor methods **
125: // ************************************************************
126:
127: /**
128: * Get the value of minValue.
129: *
130: * @return value of minValue.
131: */
132: public double getMinValue() {
133: return minValue;
134: }
135:
136: /**
137: * Set the value of minValue.
138: *
139: * @param value Value to assign to minValue.
140: */
141: public void setMinValue(double value) {
142: this .minValue = value;
143: }
144:
145: /**
146: * Get the value of maxValue.
147: *
148: * @return value of maxValue.
149: */
150: public double getMaxValue() {
151: return maxValue;
152: }
153:
154: /**
155: * Set the value of maxValue.
156: *
157: * @param value Value to assign to maxValue.
158: */
159: public void setMaxValue(double value) {
160: this.maxValue = value;
161: }
162: }
|