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 javax.faces.validator;
20  
21  import javax.faces.component.StateHolder;
22  import javax.faces.component.UIComponent;
23  import javax.faces.context.FacesContext;
24  
25  /**
26   * Creates a validator and associateds it with the nearest parent
27   * UIComponent.  When invoked, the validator ensures that values are
28   * valid doubles that lie within the minimum and maximum values specified.
29   * 
30   * Commonly associated with a h:inputText entity.
31   * 
32   * Unless otherwise specified, all attributes accept static values or EL expressions.
33   * 
34   * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
35   *
36   * @JSFValidator
37   *   name="f:validateDoubleRange"
38   *   bodyContent="empty"
39   *   tagClass="org.apache.myfaces.taglib.core.ValidateDoubleRangeTag" 
40   *
41   * @author Manfred Geiler (latest modification by $Author: skitching $)
42   * @author Thomas Spiegl
43   * @version $Revision: 676278 $ $Date: 2008-07-13 03:35:04 -0500 (Sun, 13 Jul 2008) $
44   */
45  public class DoubleRangeValidator
46          implements Validator, StateHolder
47  {
48      // FIELDS
49      public static final String VALIDATOR_ID       = "javax.faces.DoubleRange";
50      public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MAXIMUM";
51      public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MINIMUM";
52      public static final String TYPE_MESSAGE_ID    = "javax.faces.validator.DoubleRangeValidator.TYPE";
53  
54      private Double _minimum = null;
55      private Double _maximum = null;
56      private boolean _transient = false;
57  
58      // CONSTRUCTORS
59      public DoubleRangeValidator()
60      {
61      }
62  
63      public DoubleRangeValidator(double maximum)
64      {
65          _maximum = new Double(maximum);
66      }
67  
68      public DoubleRangeValidator(double maximum,
69                                  double minimum)
70      {
71          _maximum = new Double(maximum);
72          _minimum = new Double(minimum);
73      }
74  
75      // VALIDATE
76      public void validate(FacesContext facesContext,
77                           UIComponent uiComponent,
78                           Object value)
79              throws ValidatorException
80      {
81          if (facesContext == null) throw new NullPointerException("facesContext");
82          if (uiComponent == null) throw new NullPointerException("uiComponent");
83  
84          if (value == null)
85          {
86              return;
87          }
88  
89          double dvalue = parseDoubleValue(facesContext, uiComponent,value);
90          if (_minimum != null && _maximum != null)
91          {
92              if (dvalue < _minimum.doubleValue() ||
93                  dvalue > _maximum.doubleValue())
94              {
95                  Object[] args = {_minimum, _maximum,uiComponent.getId()};
96                  throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, NOT_IN_RANGE_MESSAGE_ID, args));
97              }
98          }
99          else if (_minimum != null)
100         {
101             if (dvalue < _minimum.doubleValue())
102             {
103                 Object[] args = {_minimum,uiComponent.getId()};
104                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
105             }
106         }
107         else if (_maximum != null)
108         {
109             if (dvalue > _maximum.doubleValue())
110             {
111                 Object[] args = {_maximum,uiComponent.getId()};
112                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
113             }
114         }
115     }
116 
117     private double parseDoubleValue(FacesContext facesContext, UIComponent uiComponent, Object value)
118         throws ValidatorException
119     {
120         if (value instanceof Number)
121         {
122             return ((Number)value).doubleValue();
123         }
124         else
125         {
126             try
127             {
128                 return Double.parseDouble(value.toString());
129             }
130             catch (NumberFormatException e)
131             {
132                 Object[] args = {uiComponent.getId()};
133                throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, TYPE_MESSAGE_ID, args));
134             }
135         }
136     }
137 
138 
139     // GETTER & SETTER
140     
141     /** 
142      * The largest value that should be considered valid.
143      * 
144      * @JSFProperty
145      */
146     public double getMaximum()
147     {
148         return _maximum != null ? _maximum.doubleValue() : Double.MAX_VALUE;
149     }
150 
151     public void setMaximum(double maximum)
152     {
153         _maximum = new Double(maximum);
154     }
155 
156     /**
157      * The smallest value that should be considered valid.
158      *  
159      * @JSFProperty
160      */
161     public double getMinimum()
162     {
163         return _minimum != null ? _minimum.doubleValue() : Double.MIN_VALUE;
164     }
165 
166     public void setMinimum(double minimum)
167     {
168         _minimum = new Double(minimum);
169     }
170 
171 
172     // RESTORE/SAVE STATE
173     public Object saveState(FacesContext context)
174     {
175         Object values[] = new Object[2];
176         values[0] = _maximum;
177         values[1] = _minimum;
178         return values;
179     }
180 
181     public void restoreState(FacesContext context,
182                              Object state)
183     {
184         Object values[] = (Object[])state;
185         _maximum = (Double)values[0];
186         _minimum = (Double)values[1];
187     }
188 
189     public boolean isTransient()
190     {
191         return _transient;
192     }
193 
194     public void setTransient(boolean transientValue)
195     {
196         _transient = transientValue;
197     }
198 
199     // MISC
200     public boolean equals(Object o)
201     {
202         if (this == o) return true;
203         if (!(o instanceof DoubleRangeValidator)) return false;
204 
205         final DoubleRangeValidator doubleRangeValidator = (DoubleRangeValidator)o;
206 
207         if (_maximum != null ? !_maximum.equals(doubleRangeValidator._maximum) : doubleRangeValidator._maximum != null) return false;
208         if (_minimum != null ? !_minimum.equals(doubleRangeValidator._minimum) : doubleRangeValidator._minimum != null) return false;
209 
210         return true;
211     }
212 
213 }