KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > datatype > validationruleimpl > RangeValidationRule


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.woody.datatype.validationruleimpl;
17
18 import java.math.BigDecimal JavaDoc;
19
20 import org.apache.cocoon.woody.Constants;
21 import org.apache.cocoon.woody.datatype.ValidationError;
22 import org.apache.cocoon.woody.formmodel.CannotYetResolveWarning;
23 import org.apache.cocoon.woody.util.I18nMessage;
24 import org.outerj.expression.Expression;
25 import org.outerj.expression.ExpressionContext;
26
27 /**
28  * Checks numeric ranges.
29  * Works for Integer, Long, BigDecimal, Float, Double, and Date values.
30  * Numbers are converted to the BigDecimal before comparing.
31  *
32  * <p>This validation rule can perform 3 different checks:
33  * <ul>
34  * <li>check minimum value
35  * <li>check maximum value
36  * <li>check min and max values (range check)
37  * </ul>
38  *
39  * @version $Id: RangeValidationRule.java 30932 2004-07-29 17:35:38Z vgritsenko $
40  */

41 public class RangeValidationRule extends AbstractValidationRule {
42     private Expression minExpr;
43     private Expression maxExpr;
44     
45     private static final String JavaDoc RANGE_ELEM = "range";
46     private static final String JavaDoc MIN_ATTR = "min";
47     private static final String JavaDoc MAX_ATTR = "max";
48     
49
50     public void setMinExpr(Expression minExpr) {
51         this.minExpr = minExpr;
52     }
53
54     public void setMaxExpr(Expression maxExpr) {
55         this.maxExpr = maxExpr;
56     }
57
58     public ValidationError validate(Object JavaDoc value, ExpressionContext expressionContext) {
59         // HACK: JDK's Comparable can't even compare Decimal to Integer
60
Comparable JavaDoc decimal;
61         if (value instanceof Integer JavaDoc) {
62             decimal = new BigDecimal JavaDoc(((Integer JavaDoc) value).intValue());
63         } else if (value instanceof Long JavaDoc) {
64             decimal = new BigDecimal JavaDoc(((Long JavaDoc) value).longValue());
65         } else if (value instanceof Float JavaDoc) {
66             decimal = new BigDecimal JavaDoc(((Float JavaDoc) value).floatValue());
67         } else if (value instanceof Double JavaDoc) {
68             decimal = new BigDecimal JavaDoc(((Double JavaDoc) value).doubleValue());
69         } else {
70             decimal = (Comparable JavaDoc) value;
71         }
72
73         Comparable JavaDoc min = null;
74         if (minExpr != null) {
75             Object JavaDoc result = evaluateComparable(minExpr, expressionContext, MIN_ATTR, RANGE_ELEM);
76             if (result instanceof ValidationError) {
77                 return (ValidationError) result;
78             } else if (result instanceof CannotYetResolveWarning) {
79                 return null;
80             }
81             min = (Comparable JavaDoc) result;
82         }
83         
84         Comparable JavaDoc max = null;
85         if (maxExpr != null) {
86             Object JavaDoc result = evaluateComparable(maxExpr, expressionContext, MAX_ATTR, RANGE_ELEM);
87             if (result instanceof ValidationError) {
88                 return (ValidationError) result;
89             } else if (result instanceof CannotYetResolveWarning) {
90                 return null;
91             }
92             max = (Comparable JavaDoc) result;
93         }
94         
95         if (min != null && max != null) {
96             if (decimal.compareTo(min) < 0 || decimal.compareTo(max) > 0) {
97                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.numeric.range",
98                                                                                  new String JavaDoc[]{min.toString(), max.toString()},
99                                                                                  Constants.I18N_CATALOGUE));
100             }
101
102             return null;
103         } else if (min != null) {
104             if (decimal.compareTo(min) < 0) {
105                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.numeric.min",
106                                                                                  new String JavaDoc[]{min.toString()},
107                                                                                  Constants.I18N_CATALOGUE));
108             }
109
110             return null;
111         } else if (max != null) {
112             if (decimal.compareTo(max) > 0) {
113                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.numeric.max",
114                                                                                  new String JavaDoc[]{max.toString()},
115                                                                                  Constants.I18N_CATALOGUE));
116             }
117
118             return null;
119         }
120
121         return null;
122     }
123
124     public boolean supportsType(Class JavaDoc clazz, boolean arrayType) {
125         return Comparable JavaDoc.class.isAssignableFrom(clazz) && !arrayType;
126     }
127 }
128
Popular Tags