KickJava   Java API By Example, From Geeks To Geeks.

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


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.outerj.expression.ExpressionContext;
21 import org.outerj.expression.ExpressionException;
22 import org.outerj.expression.Expression;
23
24 /**
25  * Generic validation rule that evaluates an expression. If the outcome of the expression is true,
26  * the validation is successful, otherwise not.
27  *
28  * @version $Id: AssertValidationRule.java 30932 2004-07-29 17:35:38Z vgritsenko $
29  */

30 public class AssertValidationRule extends AbstractValidationRule {
31     private Expression testExpression;
32
33     public AssertValidationRule(Expression testExpression) {
34         this.testExpression = testExpression;
35     }
36
37     public ValidationError validate(Object JavaDoc value, ExpressionContext expressionContext) {
38         Object JavaDoc expressionResult;
39         try {
40             expressionResult = testExpression.evaluate(expressionContext);
41         } catch (CannotYetResolveWarning w) {
42             return null;
43         } catch (ExpressionException e) {
44             return new ValidationError("Error evaluating expression on assert validation rule.", false);
45         }
46
47         if (!(expressionResult instanceof Boolean JavaDoc))
48             return new ValidationError("Got non-boolean result from expression on assert validation rule.", false);
49
50         if (((Boolean JavaDoc)expressionResult).booleanValue())
51             return null;
52         else
53             return hasFailMessage() ? getFailMessage() : new ValidationError("Assertion validation rule failed.", false);
54     }
55
56     public boolean supportsType(Class JavaDoc clazz, boolean arrayType) {
57         return true;
58     }
59 }
60
Popular Tags