001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.datatype.validationruleimpl;
018:
019: import java.math.BigDecimal;
020:
021: import org.apache.cocoon.forms.FormsConstants;
022: import org.apache.cocoon.forms.formmodel.CannotYetResolveWarning;
023: import org.apache.cocoon.forms.util.I18nMessage;
024: import org.apache.cocoon.forms.validation.ValidationError;
025: import org.outerj.expression.Expression;
026: import org.outerj.expression.ExpressionContext;
027:
028: /**
029: * Checks numeric ranges.
030: * Works for Integer, Long, BigDecimal, Float, Double, and Date values.
031: * Numbers are converted to the BigDecimal before comparing.
032: *
033: * <p>This validation rule can perform 3 different checks:
034: * <ul>
035: * <li>check minimum value
036: * <li>check maximum value
037: * <li>check min and max values (range check)
038: * </ul>
039: *
040: * @version $Id: RangeValidationRule.java 449149 2006-09-23 03:58:05Z crossley $
041: */
042: public class RangeValidationRule extends AbstractValidationRule {
043: private Expression minExpr;
044: private Expression maxExpr;
045:
046: private static final String RANGE_ELEM = "range";
047: private static final String MIN_ATTR = "min";
048: private static final String MAX_ATTR = "max";
049:
050: public void setMinExpr(Expression minExpr) {
051: this .minExpr = minExpr;
052: }
053:
054: public void setMaxExpr(Expression maxExpr) {
055: this .maxExpr = maxExpr;
056: }
057:
058: public ValidationError validate(Object value,
059: ExpressionContext expressionContext) {
060: // HACK: JDK's Comparable can't even compare Decimal to Integer
061: Comparable decimal;
062: if (value instanceof Integer) {
063: decimal = new BigDecimal(((Integer) value).intValue());
064: } else if (value instanceof Long) {
065: decimal = new BigDecimal(((Long) value).longValue());
066: } else if (value instanceof Float) {
067: decimal = new BigDecimal(((Float) value).floatValue());
068: } else if (value instanceof Double) {
069: decimal = new BigDecimal(((Double) value).doubleValue());
070: } else {
071: decimal = (Comparable) value;
072: }
073:
074: Comparable min = null;
075: if (minExpr != null) {
076: Object result = evaluateComparable(minExpr,
077: expressionContext, MIN_ATTR, RANGE_ELEM);
078: if (result instanceof ValidationError) {
079: return (ValidationError) result;
080: } else if (result instanceof CannotYetResolveWarning) {
081: return null;
082: }
083: min = (Comparable) result;
084: }
085:
086: Comparable max = null;
087: if (maxExpr != null) {
088: Object result = evaluateComparable(maxExpr,
089: expressionContext, MAX_ATTR, RANGE_ELEM);
090: if (result instanceof ValidationError) {
091: return (ValidationError) result;
092: } else if (result instanceof CannotYetResolveWarning) {
093: return null;
094: }
095: max = (Comparable) result;
096: }
097:
098: if (min != null && max != null) {
099: if (decimal.compareTo(min) < 0
100: || decimal.compareTo(max) > 0) {
101: return hasFailMessage() ? getFailMessage()
102: : new ValidationError(new I18nMessage(
103: "validation.numeric.range",
104: new String[] { min.toString(),
105: max.toString() },
106: FormsConstants.I18N_CATALOGUE));
107: }
108:
109: return null;
110: } else if (min != null) {
111: if (decimal.compareTo(min) < 0) {
112: return hasFailMessage() ? getFailMessage()
113: : new ValidationError(new I18nMessage(
114: "validation.numeric.min",
115: new String[] { min.toString() },
116: FormsConstants.I18N_CATALOGUE));
117: }
118: return null;
119: } else if (max != null) {
120: if (decimal.compareTo(max) > 0) {
121: return hasFailMessage() ? getFailMessage()
122: : new ValidationError(new I18nMessage(
123: "validation.numeric.max",
124: new String[] { max.toString() },
125: FormsConstants.I18N_CATALOGUE));
126: }
127: return null;
128: }
129: return null;
130: }
131:
132: public boolean supportsType(Class clazz, boolean arrayType) {
133: return Comparable.class.isAssignableFrom(clazz) && !arrayType;
134: }
135: }
|