View Javadoc

1   /*
2    * Copyright 2002-2007 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.springmodules.validation.bean.rule.AbstractValidationRule;
20  import org.springmodules.validation.bean.rule.ConditionReferenceValidationRule;
21  import org.w3c.dom.Element;
22  import org.springframework.context.ApplicationContextAware;
23  import org.springframework.context.ApplicationContext;
24  import org.springframework.beans.BeansException;
25  
26  /**
27   * An {@link org.springmodules.validation.bean.conf.loader.xml.handler.AbstractPropertyValidationElementHandler} that
28   * can handle {@link org.springmodules.validation.bean.conf.loader.annotation.handler.ConditionRef} xml element and
29   * create the appropriate property validation rule.
30   *
31   * @author Uri Boness
32   */
33  public class ConditionReferenceRuleElementHandler extends AbstractPropertyValidationElementHandler implements ApplicationContextAware {
34  
35      private static final String ELEMENT_NAME = "condition-ref";
36  
37      private static final String NAME_ATTR = "name";
38  
39      private ApplicationContext applicationContext;
40  
41      public ConditionReferenceRuleElementHandler(String namespace) {
42          super(ELEMENT_NAME, namespace);
43      }
44  
45      protected AbstractValidationRule createValidationRule(Element element) {
46          if (applicationContext == null) {
47              throw new UnsupportedOperationException("This handler can only be used when deployed within a " +
48                  "spring application context");
49          }
50          String beanName = element.getAttribute(NAME_ATTR);
51          return new ConditionReferenceValidationRule(beanName, applicationContext);
52      }
53  
54      //================================= Interfacet: ApplicationContextAware ============================================
55  
56      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
57          this.applicationContext = applicationContext;
58      }
59  }