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