KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > 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.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 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 326838 2005-10-20 06:26:53Z sylvain $
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             }
67             int length = ((BigDecimal JavaDoc)result).intValue();
68             if (string.length() != length) {
69                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.string.exact-length", new String JavaDoc[] {String.valueOf(length)}, FormsConstants.I18N_CATALOGUE));
70             }
71             return null;
72         } else if (minExpr != null && maxExpr != null) {
73             Object JavaDoc result = evaluateNumeric(minExpr, expressionContext, "min", "length");
74             if (result instanceof ValidationError) {
75                 return (ValidationError)result;
76             } else if (result instanceof CannotYetResolveWarning) {
77                 return null;
78             }
79             int minLength = ((BigDecimal JavaDoc)result).intValue();
80
81             result = evaluateNumeric(maxExpr, expressionContext, "max", "length");
82             if (result instanceof ValidationError) {
83                 return (ValidationError)result;
84             } else if (result instanceof CannotYetResolveWarning) {
85                 return null;
86             }
87             int maxLength = ((BigDecimal JavaDoc)result).intValue();
88
89             if (string.length() < minLength || string.length() > maxLength) {
90                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.string.range-length", new String JavaDoc[] {String.valueOf(minLength), String.valueOf(maxLength)}, FormsConstants.I18N_CATALOGUE));
91             }
92             return null;
93         } else if (minExpr != null) {
94             Object JavaDoc result = evaluateNumeric(minExpr, expressionContext, "min", "length");
95             if (result instanceof ValidationError) {
96                 return (ValidationError)result;
97             } else if (result instanceof CannotYetResolveWarning) {
98                 return null;
99             }
100             int length = ((BigDecimal JavaDoc)result).intValue();
101             if (string.length() < length) {
102                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.string.min-length", new String JavaDoc[] {String.valueOf(length)}, FormsConstants.I18N_CATALOGUE));
103             }
104             return null;
105         } else if (maxExpr != null) {
106             Object JavaDoc result = evaluateNumeric(maxExpr, expressionContext, "max", "length");
107             if (result instanceof ValidationError) {
108                 return (ValidationError)result;
109             } else if (result instanceof CannotYetResolveWarning) {
110                 return null;
111             }
112             int length = ((BigDecimal JavaDoc)result).intValue();
113             if (string.length() > length) {
114                 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.string.max-length", new String JavaDoc[] {String.valueOf(length)}, FormsConstants.I18N_CATALOGUE));
115             }
116             return null;
117         }
118         return null;
119     }
120
121     public boolean supportsType(Class JavaDoc clazz, boolean arrayType) {
122         return clazz.isAssignableFrom(String JavaDoc.class) && !arrayType;
123     }
124 }
125
Popular Tags