KickJava   Java API By Example, From Geeks To Geeks.

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


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.ValidationRule;
19 import org.apache.cocoon.woody.datatype.ValidationError;
20 import org.apache.cocoon.woody.formmodel.CannotYetResolveWarning;
21 import org.apache.excalibur.xml.sax.XMLizable;
22 import org.outerj.expression.Expression;
23 import org.outerj.expression.ExpressionContext;
24 import org.outerj.expression.ExpressionException;
25
26 import java.math.BigDecimal JavaDoc;
27
28 /**
29  * Abstract base class providing common functionality for many {@link ValidationRule}
30  * implementations.
31  *
32  * @version $Id: AbstractValidationRule.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34 public abstract class AbstractValidationRule implements ValidationRule {
35     private XMLizable failMessage;
36
37     /**
38      * Sets the failmessage to use for this validation rule, this will be used
39      * instead of the validation rules' built-in message. The message itself should
40      * be an object impementing XMLizable, such as a SaxBuffer instance. This
41      * allows fail messages to contain mixed content (instead of just
42      * being a string).
43      */

44     public void setFailMessage(XMLizable object) {
45         this.failMessage = object;
46     }
47
48     /**
49      * Returns the failMessage wrapped in a ValidationError object.
50      */

51     public ValidationError getFailMessage() {
52         return new ValidationError(failMessage);
53     }
54
55     /**
56      * Returns true if this validation rule has a user-defined fail message.
57      */

58     public boolean hasFailMessage() {
59         return failMessage != null;
60     }
61
62     /**
63      * Helper method for evaluating expressions whose result is numeric.
64      *
65      * @param exprName a name for the expression that's descriptive for the user, e.g. the name of the attribute in which it was defined
66      * @param ruleName a descriptive name for the validation rule, usually the rule's element name
67      * @return either a ValidationError (because expression evaluation failed) or a CannotYetResolveWarning
68      * (because another, required field referenced in the expression has not yet a value), or a BigDecimal.
69      */

70     protected Object JavaDoc evaluateNumeric(Expression expression, ExpressionContext expressionContext, String JavaDoc exprName, String JavaDoc ruleName) {
71         Object JavaDoc expressionResult;
72         try {
73             expressionResult = expression.evaluate(expressionContext);
74         } catch (CannotYetResolveWarning w) {
75             return w;
76         } catch (ExpressionException e) {
77             return new ValidationError("Error evaluating \"" + exprName + "\" expression on \"" +
78                                        ruleName + "\" validation rule", false);
79         }
80         
81         if (!(expressionResult instanceof BigDecimal JavaDoc)) {
82             return new ValidationError("Got non-numeric result from \"" + exprName + "\" expression on \"" +
83                                        ruleName + "\" validation rule", false);
84         }
85
86         return expressionResult;
87     }
88
89     /**
90      * Helper method for evaluating expressions whose result is comparable.
91      *
92      * @param exprName a name for the expression that's descriptive for the user, e.g. the name of the attribute in which it was defined
93      * @param ruleName a descriptive name for the validation rule, usually the rule's element name
94      * @return either a ValidationError (because expression evaluation failed) or a CannotYetResolveWarning
95      * (because another, required field referenced in the expression has not yet a value), or a BigDecimal.
96      */

97     protected Object JavaDoc evaluateComparable(Expression expression, ExpressionContext expressionContext, String JavaDoc exprName, String JavaDoc ruleName) {
98         Object JavaDoc expressionResult;
99         try {
100             expressionResult = expression.evaluate(expressionContext);
101         } catch (CannotYetResolveWarning w) {
102             return w;
103         } catch (ExpressionException e) {
104             return new ValidationError("Error evaluating \"" + exprName + "\" expression on \"" +
105                                        ruleName + "\" validation rule", false);
106         }
107         
108         if (!(expressionResult instanceof Comparable JavaDoc)) {
109             return new ValidationError("Got non-comparable result from \"" + exprName + "\" expression on \"" +
110                                        ruleName + "\" validation rule", false);
111         }
112
113         return expressionResult;
114     }
115 }
116
Popular Tags