KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.DomHelper;
20 import org.w3c.dom.Element JavaDoc;
21 import org.outerj.expression.Expression;
22
23 /**
24  * Builds {@link RangeValidationRule}s.
25  *
26  * @version $Id: RangeValidationRuleBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
27  */

28 public class RangeValidationRuleBuilder extends AbstractValidationRuleBuilder {
29     public ValidationRule build(Element JavaDoc validationRuleElement) throws Exception JavaDoc {
30         RangeValidationRule rule = new RangeValidationRule();
31
32         String JavaDoc minExprString = validationRuleElement.getAttribute("min");
33         String JavaDoc maxExprString = validationRuleElement.getAttribute("max");
34
35         if (minExprString.length() > 0 && maxExprString.length() > 0) {
36             Expression expression = parseExpression(minExprString, validationRuleElement, "min");
37             rule.setMinExpr(expression);
38             expression = parseExpression(maxExprString, validationRuleElement, "max");
39             rule.setMaxExpr(expression);
40         } else if (minExprString.length() > 0) {
41             Expression expression = parseExpression(minExprString, validationRuleElement, "min");
42             rule.setMinExpr(expression);
43         } else if (maxExprString.length() > 0) {
44             Expression expression = parseExpression(maxExprString, validationRuleElement, "max");
45             rule.setMaxExpr(expression);
46         } else {
47             throw new Exception JavaDoc("range validation rule requires a min and/or max attribute at " + DomHelper.getLocation(validationRuleElement));
48         }
49
50         buildFailMessage(validationRuleElement, rule);
51
52         return rule;
53     }
54 }
55
Popular Tags