KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.datatype.validationruleimpl;
17
18 import org.apache.cocoon.woody.datatype.ValidationError;
19 import org.apache.cocoon.woody.formmodel.CannotYetResolveWarning;
20 import org.apache.cocoon.woody.util.I18nMessage;
21 import org.apache.cocoon.woody.Constants;
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.woody.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 30932 2004-07-29 17:35:38Z vgritsenko $
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             int length = ((BigDecimal JavaDoc)result).intValue();
69             if (array.length != length)
70                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.array.exact-valuecount", new String JavaDoc[] {String.valueOf(length)}, Constants.I18N_CATALOGUE));
71             return null;
72         } else if (minExpr != null && maxExpr != null) {
73             Object JavaDoc result = evaluateNumeric(minExpr, expressionContext, "min", "value-count");
74             if (result instanceof ValidationError)
75                 return (ValidationError)result;
76             else if (result instanceof CannotYetResolveWarning)
77                 return null;
78             int minLength = ((BigDecimal JavaDoc)result).intValue();
79
80             result = evaluateNumeric(maxExpr, expressionContext, "max", "value-count");
81             if (result instanceof ValidationError)
82                 return (ValidationError)result;
83             else if (result instanceof CannotYetResolveWarning)
84                 return null;
85             int maxLength = ((BigDecimal JavaDoc)result).intValue();
86
87             if (array.length < minLength || array.length > maxLength)
88                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.array.range-valuecount", new String JavaDoc[] {String.valueOf(minLength), String.valueOf(maxLength)}, Constants.I18N_CATALOGUE));
89             return null;
90         } else if (minExpr != null) {
91             Object JavaDoc result = evaluateNumeric(minExpr, expressionContext, "min", "value-count");
92             if (result instanceof ValidationError)
93                 return (ValidationError)result;
94             else if (result instanceof CannotYetResolveWarning)
95                 return null;
96             int length = ((BigDecimal JavaDoc)result).intValue();
97             if (array.length < length)
98                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.array.min-valuecount", new String JavaDoc[] {String.valueOf(length)}, Constants.I18N_CATALOGUE));
99             return null;
100         } else if (maxExpr != null) {
101             Object JavaDoc result = evaluateNumeric(maxExpr, expressionContext, "max", "value-count");
102             if (result instanceof ValidationError)
103                 return (ValidationError)result;
104             else if (result instanceof CannotYetResolveWarning)
105                 return null;
106             int length = ((BigDecimal JavaDoc)result).intValue();
107             if (array.length > length)
108                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.array.max-valuecount", new String JavaDoc[] {String.valueOf(length)}, Constants.I18N_CATALOGUE));
109             return null;
110         }
111         return null;
112     }
113
114     public boolean supportsType(Class JavaDoc clazz, boolean arrayType) {
115         return arrayType;
116     }
117 }
118
Popular Tags