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: slessard $)
31   * @version $Revision: 701829 $ $Date: 2008-10-05 12:06:02 -0500 (Sun, 05 Oct 2008) $
32   * @since 1.2
33   */
34  public abstract class GenericMinMaxValidatorTag<T> extends ValidatorTag
35  {
36      protected ValueExpression _minimum;
37      protected ValueExpression _maximum;
38      protected T _min = null;
39      protected T _max = null;
40  
41      public void setMinimum(ValueExpression minimum)
42      {
43          _minimum = minimum;
44      }
45  
46      public void setMaximum(ValueExpression maximum)
47      {
48          _maximum = maximum;
49      }
50  
51      @Override
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 and apply the min and max values.
62       * 
63       * @return
64       * @throws JspException
65       */
66      @Override
67      protected Validator createValidator() throws JspException
68      {
69          if (null == _minimum && null == _maximum)
70          {
71              throw new JspException("a minimum and / or a maximum have to be specified");
72          }
73          ELContext elContext = FacesContext.getCurrentInstance().getELContext();
74          if (null != _minimum)
75          {
76              _min = getValue(_minimum.getValue(elContext));
77          }
78          if (null != _maximum)
79          {
80              _max = getValue(_maximum.getValue(elContext));
81          }
82          if (null != _minimum && null != _maximum)
83          {
84              if (!isMinLTMax())
85              {
86                  throw new JspException("maximum limit must be greater than the minimum limit");
87              }
88          }
89          return super.createValidator();
90      }
91  
92      /**
93       * @return true if min is lower than max
94       */
95      protected abstract boolean isMinLTMax();
96  
97      /**
98       * Wrapper method.
99       * 
100      * @param value
101      * @return
102      */
103     protected abstract T getValue(Object value);
104 }