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 strings with a length that lies within the minimum and maximum
33   * 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  @JSFValidator(
42      name="f:validateLength",
43      bodyContent="empty",
44      tagClass="org.apache.myfaces.taglib.core.ValidateLengthTag")
45  @JSFJspProperty(
46      name="binding", 
47      returnType = "javax.faces.validator.LengthValidator",
48      longDesc = "A ValueExpression that evaluates to a LengthValidator.")
49  public class LengthValidator
50          implements Validator, PartialStateHolder
51  {
52      // FIELDS
53      public static final String     MAXIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MAXIMUM";
54      public static final String     MINIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MINIMUM";
55      public static final String     VALIDATOR_ID        = "javax.faces.Length";
56  
57      private Integer _minimum = null;
58      private Integer _maximum = null;
59      private boolean _transient = false;
60  
61      // CONSTRUCTORS
62      public LengthValidator()
63      {
64      }
65  
66      public LengthValidator(int maximum)
67      {
68          _maximum = Integer.valueOf(maximum);
69      }
70  
71      public LengthValidator(int maximum,
72                             int minimum)
73      {
74          _maximum = Integer.valueOf(maximum);
75          _minimum = Integer.valueOf(minimum);
76      }
77  
78      // VALIDATE
79      public void validate(FacesContext facesContext,
80                           UIComponent uiComponent,
81                           Object value)
82              throws ValidatorException
83      {
84          if (facesContext == null)
85          {
86              throw new NullPointerException("facesContext");
87          }
88          if (uiComponent == null)
89          {
90              throw new NullPointerException("uiComponent");
91          }
92  
93          if (value == null)
94          {
95              return;
96          }
97  
98          int length = value instanceof String ?
99              ((String)value).length() : value.toString().length();
100 
101         if (_minimum != null)
102         {
103             if (length < _minimum.intValue())
104             {
105                 Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)};
106                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
107             }
108         }
109 
110         if (_maximum != null)
111         {
112             if (length > _maximum.intValue())
113             {
114                 Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
115                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
116             }
117         }
118     }
119 
120     // SETTER & GETTER
121     
122     /** 
123      * The largest value that should be considered valid.
124      * 
125      */
126     @JSFProperty(deferredValueType="java.lang.Integer")
127     public int getMaximum()
128     {
129         return _maximum != null ? _maximum.intValue() : 0;
130     }
131 
132     public void setMaximum(int maximum)
133     {
134         _maximum = Integer.valueOf(maximum);
135         clearInitialState();
136     }
137 
138     /**
139      * The smallest value that should be considered valid.
140      *  
141      */
142     @JSFProperty(deferredValueType="java.lang.Integer")
143     public int getMinimum()
144     {
145         return _minimum != null ? _minimum.intValue() : 0;
146     }
147 
148     public void setMinimum(int minimum)
149     {
150         _minimum = Integer.valueOf(minimum);
151         clearInitialState();
152     }
153 
154     public boolean isTransient()
155     {
156         return _transient;
157     }
158 
159     public void setTransient(boolean transientValue)
160     {
161         _transient = transientValue;
162     }
163 
164     // RESTORE & SAVE STATE
165     public Object saveState(FacesContext context)
166     {
167         if (context == null)
168         {
169             throw new NullPointerException("context");
170         }
171 
172         if (!initialStateMarked())
173         {
174             Object values[] = new Object[2];
175             values[0] = _maximum;
176             values[1] = _minimum;
177             return values;
178         }
179         return null;
180     }
181 
182     public void restoreState(FacesContext context,
183                              Object state)
184     {
185         if (context == null)
186         {
187             throw new NullPointerException("context");
188         }
189 
190         if (state != null)
191         {
192             Object values[] = (Object[])state;
193             _maximum = (Integer)values[0];
194             _minimum = (Integer)values[1];
195         }
196     }
197 
198     // MISC
199     @Override
200     public boolean equals(Object o)
201     {
202         if (this == o)
203         {
204             return true;
205         }
206         if (!(o instanceof LengthValidator))
207         {
208             return false;
209         }
210 
211         LengthValidator lengthValidator = (LengthValidator)o;
212 
213         if (_maximum != null ? !_maximum.equals(lengthValidator._maximum) : lengthValidator._maximum != null)
214         {
215             return false;
216         }
217         if (_minimum != null ? !_minimum.equals(lengthValidator._minimum) : lengthValidator._minimum != null)
218         {
219             return false;
220         }
221 
222         return true;
223     }
224 
225     @Override
226     public int hashCode()
227     {
228         int result = _minimum != null ? _minimum.hashCode() : 0;
229         result = 31 * result + (_maximum != null ? _maximum.hashCode() : 0);
230         return result;
231     }
232 
233     private boolean _initialStateMarked = false;
234 
235     public void clearInitialState()
236     {
237         _initialStateMarked = false;
238     }
239 
240     public boolean initialStateMarked()
241     {
242         return _initialStateMarked;
243     }
244 
245     public void markInitialState()
246     {
247         _initialStateMarked = true;
248     }
249     
250     @JSFProperty(faceletsOnly=true)
251     @SuppressWarnings("unused")
252     private Boolean isDisabled()
253     {
254         return null;
255     }
256     
257     @JSFProperty(faceletsOnly=true)
258     @SuppressWarnings("unused")
259     private String getFor()
260     {
261         return null;
262     }
263 }