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.woody.datatype.validationruleimpl;
018:
019: import org.apache.cocoon.woody.datatype.ValidationError;
020: import org.apache.cocoon.woody.formmodel.CannotYetResolveWarning;
021: import org.apache.cocoon.woody.util.I18nMessage;
022: import org.apache.cocoon.woody.Constants;
023: import org.outerj.expression.ExpressionContext;
024: import org.outerj.expression.Expression;
025:
026: import java.math.BigDecimal;
027:
028: /**
029: * Checks the number of values (i.e. the size of the array). This only works for Datatypes
030: * for which {@link org.apache.cocoon.woody.datatype.Datatype#isArrayType()} returns
031: * true.
032: *
033: * <p>This validation rule can perform 4 different checks:
034: * <ul>
035: * <li>check exact array size
036: * <li>check minimum array size
037: * <li>check maximum array size
038: * <li>check min and max array size
039: * </ul>
040: *
041: * @version $Id: ValueCountValidationRule.java 433543 2006-08-22 06:22:54Z crossley $
042: */
043: public class ValueCountValidationRule extends AbstractValidationRule {
044: private Expression exactExpr;
045: private Expression minExpr;
046: private Expression maxExpr;
047:
048: public void setExactExpr(Expression exactExpr) {
049: this .exactExpr = exactExpr;
050: }
051:
052: public void setMinExpr(Expression minExpr) {
053: this .minExpr = minExpr;
054: }
055:
056: public void setMaxExpr(Expression maxExpr) {
057: this .maxExpr = maxExpr;
058: }
059:
060: public ValidationError validate(Object value,
061: ExpressionContext expressionContext) {
062: Object[] array = (Object[]) value;
063:
064: if (exactExpr != null) {
065: Object result = evaluateNumeric(exactExpr,
066: expressionContext, "exact", "value-count");
067: if (result instanceof ValidationError)
068: return (ValidationError) result;
069: else if (result instanceof CannotYetResolveWarning)
070: return null;
071: int length = ((BigDecimal) result).intValue();
072: if (array.length != length)
073: return hasFailMessage() ? getFailMessage()
074: : new ValidationError(
075: new I18nMessage(
076: "validation.array.exact-valuecount",
077: new String[] { String
078: .valueOf(length) },
079: Constants.I18N_CATALOGUE));
080: return null;
081: } else if (minExpr != null && maxExpr != null) {
082: Object result = evaluateNumeric(minExpr, expressionContext,
083: "min", "value-count");
084: if (result instanceof ValidationError)
085: return (ValidationError) result;
086: else if (result instanceof CannotYetResolveWarning)
087: return null;
088: int minLength = ((BigDecimal) result).intValue();
089:
090: result = evaluateNumeric(maxExpr, expressionContext, "max",
091: "value-count");
092: if (result instanceof ValidationError)
093: return (ValidationError) result;
094: else if (result instanceof CannotYetResolveWarning)
095: return null;
096: int maxLength = ((BigDecimal) result).intValue();
097:
098: if (array.length < minLength || array.length > maxLength)
099: return hasFailMessage() ? getFailMessage()
100: : new ValidationError(new I18nMessage(
101: "validation.array.range-valuecount",
102: new String[] {
103: String.valueOf(minLength),
104: String.valueOf(maxLength) },
105: Constants.I18N_CATALOGUE));
106: return null;
107: } else if (minExpr != null) {
108: Object result = evaluateNumeric(minExpr, expressionContext,
109: "min", "value-count");
110: if (result instanceof ValidationError)
111: return (ValidationError) result;
112: else if (result instanceof CannotYetResolveWarning)
113: return null;
114: int length = ((BigDecimal) result).intValue();
115: if (array.length < length)
116: return hasFailMessage() ? getFailMessage()
117: : new ValidationError(
118: new I18nMessage(
119: "validation.array.min-valuecount",
120: new String[] { String
121: .valueOf(length) },
122: Constants.I18N_CATALOGUE));
123: return null;
124: } else if (maxExpr != null) {
125: Object result = evaluateNumeric(maxExpr, expressionContext,
126: "max", "value-count");
127: if (result instanceof ValidationError)
128: return (ValidationError) result;
129: else if (result instanceof CannotYetResolveWarning)
130: return null;
131: int length = ((BigDecimal) result).intValue();
132: if (array.length > length)
133: return hasFailMessage() ? getFailMessage()
134: : new ValidationError(
135: new I18nMessage(
136: "validation.array.max-valuecount",
137: new String[] { String
138: .valueOf(length) },
139: Constants.I18N_CATALOGUE));
140: return null;
141: }
142: return null;
143: }
144:
145: public boolean supportsType(Class clazz, boolean arrayType) {
146: return arrayType;
147: }
148: }
|