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.PartialStateHolder;
22  import javax.faces.component.UIComponent;
23  import javax.faces.context.FacesContext;
24  
25  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty;
26  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
27  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
28  
29  /**
30   * Creates a validator and associateds it with the nearest parent
31   * UIComponent.  When invoked, the validator ensures that values are
32   * valid doubles that lie within the minimum and maximum values specified.
33   * 
34   * Commonly associated with a h:inputText entity.
35   * 
36   * Unless otherwise specified, all attributes accept static values or EL expressions.
37   * 
38   * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
39   *
40   * @author Manfred Geiler (latest modification by $Author: struberg $)
41   * @author Thomas Spiegl
42   * @version $Revision: 1189793 $ $Date: 2011-10-27 14:51:09 +0000 (Thu, 27 Oct 2011) $
43   */
44  @JSFValidator(
45      name="f:validateDoubleRange",
46      bodyContent="empty",
47      tagClass="org.apache.myfaces.taglib.core.ValidateDoubleRangeTag")
48  @JSFJspProperty(
49      name="binding", 
50      returnType = "javax.faces.validator.DoubleRangeValidator",
51      longDesc = "A ValueExpression that evaluates to a DoubleRangeValidator.")
52  public class DoubleRangeValidator
53          implements Validator, PartialStateHolder
54  {
55      // FIELDS
56      public static final String VALIDATOR_ID       = "javax.faces.DoubleRange";
57      public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MAXIMUM";
58      public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MINIMUM";
59      public static final String TYPE_MESSAGE_ID    = "javax.faces.validator.DoubleRangeValidator.TYPE";
60      public static final String NOT_IN_RANGE_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.NOT_IN_RANGE";
61      
62      private Double _minimum = null;
63      private Double _maximum = null;
64      private boolean _transient = false;
65  
66      // CONSTRUCTORS
67      public DoubleRangeValidator()
68      {
69      }
70  
71      public DoubleRangeValidator(double maximum)
72      {
73          _maximum = new Double(maximum);
74      }
75  
76      public DoubleRangeValidator(double maximum,
77                                  double minimum)
78      {
79          _maximum = new Double(maximum);
80          _minimum = new Double(minimum);
81      }
82  
83      // VALIDATE
84      public void validate(FacesContext facesContext,
85                           UIComponent uiComponent,
86                           Object value)
87              throws ValidatorException
88      {
89          if (facesContext == null)
90          {
91              throw new NullPointerException("facesContext");
92          }
93          if (uiComponent == null)
94          {
95              throw new NullPointerException("uiComponent");
96          }
97  
98          if (value == null)
99          {
100             return;
101         }
102 
103         double dvalue = parseDoubleValue(facesContext, uiComponent,value);
104         if (_minimum != null && _maximum != null)
105         {
106             if (dvalue < _minimum.doubleValue() ||
107                 dvalue > _maximum.doubleValue())
108             {
109                 Object[] args = {_minimum, _maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
110                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, NOT_IN_RANGE_MESSAGE_ID,
111                                                                            args));
112             }
113         }
114         else if (_minimum != null)
115         {
116             if (dvalue < _minimum.doubleValue())
117             {
118                 Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)};
119                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
120             }
121         }
122         else if (_maximum != null)
123         {
124             if (dvalue > _maximum.doubleValue())
125             {
126                 Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
127                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
128             }
129         }
130     }
131 
132     private double parseDoubleValue(FacesContext facesContext, UIComponent uiComponent, Object value)
133         throws ValidatorException
134     {
135         if (value instanceof Number)
136         {
137             return ((Number)value).doubleValue();
138         }
139         
140         try
141         {
142             return Double.parseDouble(value.toString());
143         }
144         catch (NumberFormatException e)
145         {
146             Object[] args = {_MessageUtils.getLabel(facesContext, uiComponent)};
147             throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, TYPE_MESSAGE_ID, args));
148         }
149     }
150 
151 
152     // GETTER & SETTER
153     
154     /** 
155      * The largest value that should be considered valid.
156      * 
157      */
158     @JSFProperty(deferredValueType="java.lang.Double")
159     public double getMaximum()
160     {
161         return _maximum != null ? _maximum.doubleValue() : Double.MAX_VALUE;
162     }
163 
164     public void setMaximum(double maximum)
165     {
166         _maximum = new Double(maximum);
167         clearInitialState();
168     }
169 
170     /**
171      * The smallest value that should be considered valid.
172      *  
173      */
174     @JSFProperty(deferredValueType="java.lang.Double")
175     public double getMinimum()
176     {
177         return _minimum != null ? _minimum.doubleValue() : Double.MIN_VALUE;
178     }
179 
180     public void setMinimum(double minimum)
181     {
182         _minimum = new Double(minimum);
183         clearInitialState();
184     }
185 
186 
187     // RESTORE/SAVE STATE
188     public Object saveState(FacesContext context)
189     {
190         if (!initialStateMarked())
191         {
192             Object values[] = new Object[2];
193             values[0] = _maximum;
194             values[1] = _minimum;
195             return values;
196         }
197         return null;
198     }
199 
200     public void restoreState(FacesContext context,
201                              Object state)
202     {
203         if (state != null)
204         {
205             Object values[] = (Object[])state;
206             _maximum = (Double)values[0];
207             _minimum = (Double)values[1];
208         }
209     }
210 
211     public boolean isTransient()
212     {
213         return _transient;
214     }
215 
216     public void setTransient(boolean transientValue)
217     {
218         _transient = transientValue;
219     }
220 
221     // MISC
222     @Override
223     public boolean equals(Object o)
224     {
225         if (this == o)
226         {
227             return true;
228         }
229         if (!(o instanceof DoubleRangeValidator))
230         {
231             return false;
232         }
233 
234         DoubleRangeValidator doubleRangeValidator = (DoubleRangeValidator)o;
235 
236         if (_maximum != null ? !_maximum.equals(doubleRangeValidator._maximum) : doubleRangeValidator._maximum != null)
237         {
238             return false;
239         }
240         if (_minimum != null ? !_minimum.equals(doubleRangeValidator._minimum) : doubleRangeValidator._minimum != null)
241         {
242             return false;
243         }
244 
245         return true;
246     }
247 
248 
249 
250     private boolean _initialStateMarked = false;
251 
252     public void clearInitialState()
253     {
254         _initialStateMarked = false;
255     }
256 
257     public boolean initialStateMarked()
258     {
259         return _initialStateMarked;
260     }
261 
262     public void markInitialState()
263     {
264         _initialStateMarked = true;
265     }
266     
267     @JSFProperty(faceletsOnly=true)
268     private Boolean isDisabled()
269     {
270         return null;
271     }
272     
273     @JSFProperty(faceletsOnly=true)
274     private String getFor()
275     {
276         return null;
277     }
278 
279     @Override
280     public int hashCode()
281     {
282         int result = _minimum != null ? _minimum.hashCode() : 0;
283         result = 31 * result + (_maximum != null ? _maximum.hashCode() : 0);
284         return result;
285     }
286 }