KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > validationruleimpl > ValueCountValidationRule


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.forms.datatype.validationruleimpl;
17
18 import org.apache.cocoon.forms.FormsConstants;
19 import org.apache.cocoon.forms.formmodel.CannotYetResolveWarning;
20 import org.apache.cocoon.forms.util.I18nMessage;
21 import org.apache.cocoon.forms.validation.ValidationError;
22 import org.outerj.expression.ExpressionContext;
23 import org.outerj.expression.Expression;
24
25 import java.math.BigDecimal JavaDoc;
26
27 /**
28  * Checks the number of values (i.e. the size of the array). This only works for Datatypes
29  * for which {@link org.apache.cocoon.forms.datatype.Datatype#isArrayType()} returns
30  * true.
31  *
32  * <p>This validation rule can perform 4 different checks:
33  * <ul>
34  * <li>check exact array size
35  * <li>check minimum array size
36  * <li>check maximum array size
37  * <li>check min and max array size
38  * </ul>
39  *
40  * @version $Id: ValueCountValidationRule.java 326838 2005-10-20 06:26:53Z sylvain $
41  */

42 public class ValueCountValidationRule extends AbstractValidationRule {
43     private Expression exactExpr;
44     private Expression minExpr;
45     private Expression maxExpr;
46
47     public void setExactExpr(Expression exactExpr) {
48         this.exactExpr = exactExpr;
49     }
50
51     public void setMinExpr(Expression minExpr) {
52         this.minExpr = minExpr;
53     }
54
55     public void setMaxExpr(Expression maxExpr) {
56         this.maxExpr = maxExpr;
57     }
58
59     public ValidationError validate(Object JavaDoc value, ExpressionContext expressionContext) {
60         Object JavaDoc[] array = (Object JavaDoc[])value;
61
62         if (exactExpr != null) {
63             Object JavaDoc result = evaluateNumeric(exactExpr, expressionContext, "exact", "value-count");
64             if (result instanceof ValidationError) {
65                 return (ValidationError)result;
66             } else if (result instanceof CannotYetResolveWarning) {
67                 return null;
68             }
69             int length = ((BigDecimal JavaDoc)result).intValue();
70             if (array.length != length) {
71                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.array.exact-valuecount", new String JavaDoc[] {String.valueOf(length)}, FormsConstants.I18N_CATALOGUE));
72             }
73             return null;
74         } else if (minExpr != null && maxExpr != null) {
75             Object JavaDoc result = evaluateNumeric(minExpr, expressionContext, "min", "value-count");
76             if (result instanceof ValidationError) {
77                 return (ValidationError)result;
78             } else if (result instanceof CannotYetResolveWarning) {
79                 return null;
80             }
81             int minLength = ((BigDecimal JavaDoc)result).intValue();
82
83             result = evaluateNumeric(maxExpr, expressionContext, "max", "value-count");
84             if (result instanceof ValidationError) {
85                 return (ValidationError)result;
86             } else if (result instanceof CannotYetResolveWarning) {
87                 return null;
88             }
89             int maxLength = ((BigDecimal JavaDoc)result).intValue();
90
91             if (array.length < minLength || array.length > maxLength) {
92                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.array.range-valuecount", new String JavaDoc[] {String.valueOf(minLength), String.valueOf(maxLength)}, FormsConstants.I18N_CATALOGUE));
93             }
94             return null;
95         } else if (minExpr != null) {
96             Object JavaDoc result = evaluateNumeric(minExpr, expressionContext, "min", "value-count");
97             if (result instanceof ValidationError) {
98                 return (ValidationError)result;
99             } else if (result instanceof CannotYetResolveWarning) {
100                 return null;
101             }
102             int length = ((BigDecimal JavaDoc)result).intValue();
103             if (array.length < length) {
104                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.array.min-valuecount", new String JavaDoc[] {String.valueOf(length)}, FormsConstants.I18N_CATALOGUE));
105             }
106             return null;
107         } else if (maxExpr != null) {
108             Object JavaDoc result = evaluateNumeric(maxExpr, expressionContext, "max", "value-count");
109             if (result instanceof ValidationError) {
110                 return (ValidationError)result;
111             } else if (result instanceof CannotYetResolveWarning) {
112                 return null;
113             }
114             int length = ((BigDecimal JavaDoc)result).intValue();
115             if (array.length > length) {
116                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.array.max-valuecount", new String JavaDoc[] {String.valueOf(length)}, FormsConstants.I18N_CATALOGUE));
117             }
118             return null;
119         }
120         return null;
121     }
122
123     public boolean supportsType(Class JavaDoc clazz, boolean arrayType) {
124         return arrayType;
125     }
126 }
127
Popular Tags