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
32   * are valid longs 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: bommel $)
41   * @author Thomas Spiegl
42   * @version $Revision: 1187700 $ $Date: 2011-10-22 12:19:37 +0000 (Sat, 22 Oct 2011) $
43   */
44  @JSFValidator(
45      name="f:validateLongRange",
46      bodyContent="empty",
47      tagClass="org.apache.myfaces.taglib.core.ValidateLongRangeTag")
48  @JSFJspProperty(
49      name="binding", 
50      returnType = "javax.faces.validator.LongRangeValidator",
51      longDesc = "A ValueExpression that evaluates to a LongRangeValidator.")
52  public class LongRangeValidator
53          implements Validator, PartialStateHolder
54  {
55      // FIELDS
56      public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.MAXIMUM";
57      public static final String MINIMUM_MESSAGE_ID =    "javax.faces.validator.LongRangeValidator.MINIMUM";
58      public static final String TYPE_MESSAGE_ID       = "javax.faces.validator.LongRangeValidator.TYPE";
59      public static final String VALIDATOR_ID       = "javax.faces.LongRange";
60      public static final String NOT_IN_RANGE_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.NOT_IN_RANGE";
61      
62      private Long _minimum = null;
63      private Long _maximum = null;
64      private boolean _transient = false;
65  
66      // CONSTRUCTORS
67      public LongRangeValidator()
68      {
69      }
70  
71      public LongRangeValidator(long maximum)
72      {
73          _maximum = Long.valueOf(maximum);
74      }
75  
76      public LongRangeValidator(long maximum,
77                                long minimum)
78      {
79          _maximum = Long.valueOf(maximum);
80          _minimum = Long.valueOf(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) throw new NullPointerException("facesContext");
90          if (uiComponent == null) throw new NullPointerException("uiComponent");
91  
92          if (value == null)
93          {
94              return;
95          }
96  
97          double dvalue = parseLongValue(facesContext, uiComponent,value);
98          if (_minimum != null && _maximum != null)
99          {
100             if (dvalue < _minimum.longValue() ||
101                 dvalue > _maximum.longValue())
102             {
103                 Object[] args = {_minimum, _maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
104                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, NOT_IN_RANGE_MESSAGE_ID, args));
105             }
106         }
107         else if (_minimum != null)
108         {
109             if (dvalue < _minimum.longValue())
110             {
111                 Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)};
112                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
113             }
114         }
115         else if (_maximum != null)
116         {
117             if (dvalue > _maximum.longValue())
118             {
119                 Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
120                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
121             }
122         }
123     }
124 
125     private long parseLongValue(FacesContext facesContext, UIComponent uiComponent, Object value)
126         throws ValidatorException
127     {
128         if (value instanceof Number)
129         {
130             return ((Number)value).longValue();
131         }
132 
133         try
134         {
135             return Long.parseLong(value.toString());
136         }
137         catch (NumberFormatException e)
138         {
139             Object[] args = {_MessageUtils.getLabel(facesContext, uiComponent)};
140             throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, TYPE_MESSAGE_ID, args));
141         }
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.Long")
153     public long getMaximum()
154     {
155         return _maximum != null ? _maximum.longValue() : Long.MAX_VALUE;
156     }
157 
158     public void setMaximum(long maximum)
159     {
160         _maximum = Long.valueOf(maximum);
161         clearInitialState();
162     }
163 
164     /**
165      * The smallest value that should be considered valid.
166      *  
167      */
168     @JSFProperty(deferredValueType="java.lang.Long")
169     public long getMinimum()
170     {
171         return _minimum != null ? _minimum.longValue() : Long.MIN_VALUE;
172     }
173 
174     public void setMinimum(long minimum)
175     {
176         _minimum = new Long(minimum);
177         clearInitialState();
178     }
179 
180     public boolean isTransient()
181     {
182         return _transient;
183     }
184 
185     public void setTransient(boolean transientValue)
186     {
187         _transient = transientValue;
188     }
189 
190     // RESTORE & SAVE STATE
191     public Object saveState(FacesContext context)
192     {
193         if (!initialStateMarked())
194         {
195             Object values[] = new Object[2];
196             values[0] = _maximum;
197             values[1] = _minimum;
198             return values;
199         }
200         return null;
201     }
202 
203     public void restoreState(FacesContext context,
204                              Object state)
205     {
206         if (state != null)
207         {
208             Object values[] = (Object[])state;
209             _maximum = (Long)values[0];
210             _minimum = (Long)values[1];
211         }
212     }
213 
214     // MISC
215     @Override
216     public boolean equals(Object o)
217     {
218         if (this == o) return true;
219         if (!(o instanceof LongRangeValidator)) return false;
220 
221         final LongRangeValidator longRangeValidator = (LongRangeValidator)o;
222 
223         if (_maximum != null ? !_maximum.equals(longRangeValidator._maximum) : longRangeValidator._maximum != null) return false;
224         if (_minimum != null ? !_minimum.equals(longRangeValidator._minimum) : longRangeValidator._minimum != null) return false;
225 
226         return true;
227     }
228     
229     private boolean _initialStateMarked = false;
230 
231     public void clearInitialState()
232     {
233         _initialStateMarked = false;
234     }
235 
236     public boolean initialStateMarked()
237     {
238         return _initialStateMarked;
239     }
240 
241     public void markInitialState()
242     {
243         _initialStateMarked = true;
244     }
245     
246     @JSFProperty(faceletsOnly=true)
247     private Boolean isDisabled()
248     {
249         return null;
250     }
251     
252     @JSFProperty(faceletsOnly=true)
253     private String getFor()
254     {
255         return null;
256     }
257 }