View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
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  
17  package org.springmodules.validation.bean.conf.loader.xml.handler;
18  
19  import org.springframework.util.StringUtils;
20  import org.springmodules.validation.bean.conf.ValidationConfigurationException;
21  import org.springmodules.validation.bean.rule.AbstractValidationRule;
22  import org.springmodules.validation.bean.rule.MaxSizeValidationRule;
23  import org.springmodules.validation.bean.rule.MinSizeValidationRule;
24  import org.springmodules.validation.bean.rule.SizeValidationRule;
25  import org.w3c.dom.Element;
26  
27  /**
28   * An {@link AbstractPropertyValidationElementHandler} that parses a size validation rules. This handler creates a
29   * {@link org.springmodules.validation.util.condition.collection.SizeRangeCollectionCondition},
30   * {@link org.springmodules.validation.util.condition.collection.MinSizeCollectionCondition}, or
31   * {@link org.springmodules.validation.util.condition.collection.MaxSizeCollectionCondition} from the <size>
32   * element.
33   *
34   * @author Uri Boness
35   */
36  public class SizeRuleElementHandler extends AbstractPropertyValidationElementHandler {
37  
38      private static final String ELEMENT_NAME = "size";
39  
40      private static final String MIN_ATTR = "min";
41  
42      private static final String MAX_ATTR = "max";
43  
44      /**
45       * Constructs a new SizeRuleElementHandler.
46       */
47      public SizeRuleElementHandler(String namespaceUri) {
48          super(ELEMENT_NAME, namespaceUri);
49      }
50  
51      protected AbstractValidationRule createValidationRule(Element element) {
52  
53          String minText = element.getAttribute(MIN_ATTR);
54          String maxText = element.getAttribute(MAX_ATTR);
55  
56          Integer min = (StringUtils.hasText(minText)) ? new Integer(minText) : null;
57          Integer max = (StringUtils.hasText(maxText)) ? new Integer(maxText) : null;
58  
59          if (min != null && max != null) {
60              return new SizeValidationRule(min.intValue(), max.intValue());
61          }
62  
63          if (min != null) {
64              return new MinSizeValidationRule(min.intValue());
65          }
66  
67          if (max != null) {
68              return new MaxSizeValidationRule(max.intValue());
69          }
70  
71          throw new ValidationConfigurationException("Element '" + ELEMENT_NAME +
72              "' must have either 'min' attribute, 'max' attribute, or both");
73  
74      }
75  
76  }