View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.taglib.core;
20  
21  import javax.el.ELContext;
22  import javax.el.ValueExpression;
23  import javax.faces.context.FacesContext;
24  import javax.faces.validator.Validator;
25  import javax.servlet.jsp.JspException;
26  
27  /**
28   * This is the base Tag for all ValidatorTags which got a minimum and a maimum attribute.
29   * 
30   * @author Andreas Berger (latest modification by $Author: skitching $)
31   * @version $Revision: 684465 $ $Date: 2008-08-10 06:38:21 -0500 (Sun, 10 Aug 2008) $
32   * @since 1.2
33   */
34  public abstract class GenericMinMaxValidatorTag<T>
35      extends ValidatorTag
36  {
37      protected  ValueExpression _minimum;
38      protected  ValueExpression _maximum;
39      protected T _min = null;
40      protected T _max = null;
41  
42      public void setMinimum(ValueExpression minimum)
43      {
44          _minimum = minimum;
45      }
46  
47      public void setMaximum(ValueExpression maximum)
48      {
49          _maximum = maximum;
50      }
51  
52      public void release()
53      {
54          _minimum = null;
55          _maximum = null;
56          _min = null;
57          _max = null;
58      }
59  
60      /**
61       * This method returns the Validator, you have to cast it to the correct type
62       * and apply the min and max values.
63       *  
64       * @return
65       * @throws JspException
66       */
67      protected Validator createValidator() throws JspException
68      {
69          if (null == _minimum && null == _maximum){
70              throw new JspException("a minimum and / or a maximum have to be specified");
71          }
72          ELContext elContext = FacesContext.getCurrentInstance().getELContext();
73          if (null != _minimum){
74              _min = getValue(_minimum.getValue(elContext));
75          }
76          if (null != _maximum){
77              _max = getValue(_maximum.getValue(elContext));
78          }
79          if (null != _minimum && null != _maximum){
80              if (!isMinLTMax()){
81                  throw new JspException("maximum limit must be greater than the minimum limit");
82              }
83          }
84          return super.createValidator();
85      }
86  
87      /**
88       * @return true if min is lower than max
89       */
90      protected abstract boolean isMinLTMax();
91  
92      /**
93       * Wrapper method.
94       * @param value
95       * @return
96       */
97      protected abstract T getValue(Object value);
98  }