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