KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > validationruleimpl > ValueCountValidationRuleBuilder


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.forms.datatype.validationruleimpl;
17
18 import org.apache.cocoon.forms.datatype.ValidationRule;
19 import org.apache.cocoon.forms.util.DomHelper;
20 import org.w3c.dom.Element JavaDoc;
21 import org.outerj.expression.Expression;
22
23 /**
24  * Builds {@link ValueCountValidationRule}s.
25  *
26  * @version $Id: ValueCountValidationRuleBuilder.java 56582 2004-11-04 10:16:22Z sylvain $
27  */

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