001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.datatype.validationruleimpl;
018:
019: import org.apache.cocoon.forms.FormsConstants;
020: import org.apache.cocoon.forms.formmodel.CannotYetResolveWarning;
021: import org.apache.cocoon.forms.util.I18nMessage;
022: import org.apache.cocoon.forms.validation.ValidationError;
023: import org.outerj.expression.ExpressionContext;
024: import org.outerj.expression.Expression;
025:
026: import java.math.BigDecimal;
027:
028: /**
029: * Checks the length of a String.
030: *
031: * <p>This validation rule can perform 4 different checks:
032: * <ul>
033: * <li>check exact string length
034: * <li>check minimum string length
035: * <li>check maximum string length
036: * <li>check min and max string length
037: * </ul>
038: *
039: * @version $Id: LengthValidationRule.java 449149 2006-09-23 03:58:05Z crossley $
040: */
041: public class LengthValidationRule extends AbstractValidationRule {
042: private Expression exactExpr;
043: private Expression minExpr;
044: private Expression maxExpr;
045:
046: public void setExactExpr(Expression exactExpr) {
047: this .exactExpr = exactExpr;
048: }
049:
050: public void setMinExpr(Expression minExpr) {
051: this .minExpr = minExpr;
052: }
053:
054: public void setMaxExpr(Expression maxExpr) {
055: this .maxExpr = maxExpr;
056: }
057:
058: public ValidationError validate(Object value,
059: ExpressionContext expressionContext) {
060: String string = (String) value;
061:
062: if (exactExpr != null) {
063: Object result = evaluateNumeric(exactExpr,
064: expressionContext, "exact", "length");
065: if (result instanceof ValidationError) {
066: return (ValidationError) result;
067: } else if (result instanceof CannotYetResolveWarning) {
068: return null;
069: }
070: int length = ((BigDecimal) result).intValue();
071: if (string.length() != length) {
072: return hasFailMessage() ? getFailMessage()
073: : new ValidationError(
074: new I18nMessage(
075: "validation.string.exact-length",
076: new String[] { String
077: .valueOf(length) },
078: FormsConstants.I18N_CATALOGUE));
079: }
080: return null;
081: } else if (minExpr != null && maxExpr != null) {
082: Object result = evaluateNumeric(minExpr, expressionContext,
083: "min", "length");
084: if (result instanceof ValidationError) {
085: return (ValidationError) result;
086: } else if (result instanceof CannotYetResolveWarning) {
087: return null;
088: }
089: int minLength = ((BigDecimal) result).intValue();
090:
091: result = evaluateNumeric(maxExpr, expressionContext, "max",
092: "length");
093: if (result instanceof ValidationError) {
094: return (ValidationError) result;
095: } else if (result instanceof CannotYetResolveWarning) {
096: return null;
097: }
098: int maxLength = ((BigDecimal) result).intValue();
099:
100: if (string.length() < minLength
101: || string.length() > maxLength) {
102: return hasFailMessage() ? getFailMessage()
103: : new ValidationError(new I18nMessage(
104: "validation.string.range-length",
105: new String[] {
106: String.valueOf(minLength),
107: String.valueOf(maxLength) },
108: FormsConstants.I18N_CATALOGUE));
109: }
110: return null;
111: } else if (minExpr != null) {
112: Object result = evaluateNumeric(minExpr, expressionContext,
113: "min", "length");
114: if (result instanceof ValidationError) {
115: return (ValidationError) result;
116: } else if (result instanceof CannotYetResolveWarning) {
117: return null;
118: }
119: int length = ((BigDecimal) result).intValue();
120: if (string.length() < length) {
121: return hasFailMessage() ? getFailMessage()
122: : new ValidationError(
123: new I18nMessage(
124: "validation.string.min-length",
125: new String[] { String
126: .valueOf(length) },
127: FormsConstants.I18N_CATALOGUE));
128: }
129: return null;
130: } else if (maxExpr != null) {
131: Object result = evaluateNumeric(maxExpr, expressionContext,
132: "max", "length");
133: if (result instanceof ValidationError) {
134: return (ValidationError) result;
135: } else if (result instanceof CannotYetResolveWarning) {
136: return null;
137: }
138: int length = ((BigDecimal) result).intValue();
139: if (string.length() > length) {
140: return hasFailMessage() ? getFailMessage()
141: : new ValidationError(
142: new I18nMessage(
143: "validation.string.max-length",
144: new String[] { String
145: .valueOf(length) },
146: FormsConstants.I18N_CATALOGUE));
147: }
148: return null;
149: }
150: return null;
151: }
152:
153: public boolean supportsType(Class clazz, boolean arrayType) {
154: return clazz.isAssignableFrom(String.class) && !arrayType;
155: }
156: }
|