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.ExpressionValidationRule;
23  import org.w3c.dom.Element;
24  
25  /**
26   * An {@link AbstractPropertyValidationElementHandler} that can handle elements that represent valang validation rules.
27   * This handler creates a valang conditoin out of a <valang> element.
28   *
29   * @author Uri Boness
30   */
31  public class ExpressionPropertyValidationElementHandler extends AbstractPropertyValidationElementHandler {
32  
33      private static final String ELEMENT_NAME = "expression";
34  
35      private static final String CONDITION_ATTR = "condition";
36  
37      private static final String SCOPE_ATTR = "scope";
38  
39      private static final String PROPERTY_SCOPE_VALUE = "property";
40  
41      private static final String GLOBAL_SCOPE_VALUE = "global";
42  
43      /**
44       * Constructs a new ExpressionPropertyValidationElementHandler.
45       */
46      public ExpressionPropertyValidationElementHandler(String namespaceUri) {
47          super(ELEMENT_NAME, namespaceUri);
48      }
49  
50      protected AbstractValidationRule createValidationRule(Element element) {
51          String expression = element.getAttribute(CONDITION_ATTR);
52          if (!StringUtils.hasText(expression)) {
53              throw new ValidationConfigurationException("Element '" + ELEMENT_NAME + "' must have a '" + CONDITION_ATTR + "' attribute");
54          }
55          return new ExpressionValidationRule(getConditionExpressionParser(), expression);
56      }
57  
58      public boolean isConditionGloballyScoped(Element element) {
59          if (!element.hasAttribute(SCOPE_ATTR)) {
60              return true;
61          }
62          String value = element.getAttribute(SCOPE_ATTR);
63          if (GLOBAL_SCOPE_VALUE.equals(value)) {
64              return true;
65          }
66          if (PROPERTY_SCOPE_VALUE.equals(value)) {
67              return false;
68          }
69          throw new ValidationConfigurationException("Unknown value '" + value + "' for attribute '" + SCOPE_ATTR + "'");
70      }
71  
72  }