KickJava   Java API By Example, From Geeks To Geeks.

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


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 length of a String.
29  *
30  * <p>This validation rule can perform 4 different checks:
31  * <ul>
32  * <li>check exact string length
33  * <li>check minimum string length
34  * <li>check maximum string length
35  * <li>check min and max string length
36  * </ul>
37  *
38  * @version $Id: LengthValidationRule.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

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