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 java.beans.PropertyDescriptor;
20  import java.util.Calendar;
21  import java.util.Date;
22  
23  import org.springmodules.validation.bean.rule.AbstractValidationRule;
24  import org.springmodules.validation.bean.rule.DateInThePastValidationRule;
25  import org.w3c.dom.Element;
26  
27  /**
28   * An {@link AbstractPropertyValidationElementHandler} implementation that can handle an element that represents a "date in
29   * the past" validation rule - a rule that validates that a given date occured in the past.
30   *
31   * @author Uri Boness
32   */
33  public class DateInPastRuleElementHandler extends AbstractPropertyValidationElementHandler {
34  
35      private static final String ELEMENT_NAME = "in-past";
36  
37      /**
38       * Constructs a new DateInPastRuleElementHandler.
39       */
40      public DateInPastRuleElementHandler(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 types
46       * {@link java.util.Date} and {@link java.util.Calendar}.
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)
52              &&
53              (
54                  Date.class.isAssignableFrom(descriptor.getPropertyType())
55                      ||
56                      Calendar.class.isAssignableFrom(descriptor.getPropertyType())
57              );
58      }
59  
60      protected AbstractValidationRule createValidationRule(Element element) {
61          return new DateInThePastValidationRule();
62      }
63  
64  }