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.jodatime;
18  
19  import java.beans.PropertyDescriptor;
20  
21  import org.joda.time.Instant;
22  import org.springmodules.validation.bean.conf.loader.xml.handler.AbstractPropertyValidationElementHandler;
23  import org.springmodules.validation.bean.rule.AbstractValidationRule;
24  import org.springmodules.validation.bean.rule.InstantInThePastValidationRule;
25  import org.w3c.dom.Element;
26  
27  /**
28   * An {@link AbstractPropertyValidationElementHandler} implementation that can handle an element that represents an "in the
29   * past" joda-time instant validation rule.
30   *
31   * @author Uri Boness
32   */
33  public class InstantInPastRuleElementHandler extends AbstractPropertyValidationElementHandler {
34  
35      private static final String ELEMENT_NAME = "in-past";
36  
37      /**
38       * Constructs a new InstantInPastRuleElementHandler.
39       */
40      public InstantInPastRuleElementHandler(String namespaceUri) {
41          super(ELEMENT_NAME, namespaceUri);
42      }
43  
44      /**
45       * In addition to the element name and namespace check, this handler only support properties of type
46       * {@link org.joda.time.Instant}.
47       *
48       * @see org.springmodules.validation.bean.conf.loader.xml.handler.PropertyValidationElementHandler#supports(org.w3c.dom.Element, Class, java.beans.PropertyDescriptor)
49       */
50      public boolean supports(Element element, Class clazz, PropertyDescriptor descriptor) {
51          return super.supports(element, clazz, descriptor) && Instant.class.isAssignableFrom(descriptor.getPropertyType());
52      }
53  
54      protected AbstractValidationRule createValidationRule(Element element) {
55          return new InstantInThePastValidationRule();
56      }
57  
58  }