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