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 strings with a length that lies within the minimum and maximum
34   * values specified.
35   * 
36   * Commonly associated with a h:inputText entity.
37   * 
38   * Unless otherwise specified, all attributes accept static values or EL expressions.
39   * 
40   * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
41   *
42   * @author Manfred Geiler (latest modification by $Author: bommel $)
43   * @author Thomas Spiegl
44   * @version $Revision: 1187700 $ $Date: 2011-10-22 12:19:37 +0000 (Sat, 22 Oct 2011) $
45   */
46  @JSFValidator(
47      name="f:validateLength",
48      bodyContent="empty",
49      tagClass="org.apache.myfaces.taglib.core.ValidateLengthTag")
50  @JSFJspProperty(
51      name="binding", 
52      returnType = "javax.faces.validator.LengthValidator",
53      longDesc = "A ValueExpression that evaluates to a LengthValidator.")
54  public class LengthValidator
55          implements Validator, PartialStateHolder
56  {
57      // FIELDS
58      public static final String     MAXIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MAXIMUM";
59      public static final String     MINIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MINIMUM";
60      public static final String     VALIDATOR_ID        = "javax.faces.Length";
61  
62      private Integer _minimum = null;
63      private Integer _maximum = null;
64      private boolean _transient = false;
65  
66      // CONSTRUCTORS
67      public LengthValidator()
68      {
69      }
70  
71      public LengthValidator(int maximum)
72      {
73          _maximum = Integer.valueOf(maximum);
74      }
75  
76      public LengthValidator(int maximum,
77                             int minimum)
78      {
79          _maximum = Integer.valueOf(maximum);
80          _minimum = Integer.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          int length = value instanceof String ?
98              ((String)value).length() : value.toString().length();
99  
100         if (_minimum != null)
101         {
102             if (length < _minimum.intValue())
103             {
104                 Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)};
105                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
106             }
107         }
108 
109         if (_maximum != null)
110         {
111             if (length > _maximum.intValue())
112             {
113                 Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
114                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
115             }
116         }
117     }
118 
119     // SETTER & GETTER
120     
121     /** 
122      * The largest value that should be considered valid.
123      * 
124      */
125     @JSFProperty(deferredValueType="java.lang.Integer")
126     public int getMaximum()
127     {
128         return _maximum != null ? _maximum.intValue() : 0;
129     }
130 
131     public void setMaximum(int maximum)
132     {
133         _maximum = Integer.valueOf(maximum);
134         clearInitialState();
135     }
136 
137     /**
138      * The smallest value that should be considered valid.
139      *  
140      */
141     @JSFProperty(deferredValueType="java.lang.Integer")
142     public int getMinimum()
143     {
144         return _minimum != null ? _minimum.intValue() : 0;
145     }
146 
147     public void setMinimum(int minimum)
148     {
149         _minimum = Integer.valueOf(minimum);
150         clearInitialState();
151     }
152 
153     public boolean isTransient()
154     {
155         return _transient;
156     }
157 
158     public void setTransient(boolean transientValue)
159     {
160         _transient = transientValue;
161     }
162 
163     // RESTORE & SAVE STATE
164     public Object saveState(FacesContext context)
165     {
166         if (!initialStateMarked())
167         {
168             Object values[] = new Object[2];
169             values[0] = _maximum;
170             values[1] = _minimum;
171             return values;
172         }
173         return null;
174     }
175 
176     public void restoreState(FacesContext context,
177                              Object state)
178     {
179         if (state != null)
180         {
181             Object values[] = (Object[])state;
182             _maximum = (Integer)values[0];
183             _minimum = (Integer)values[1];
184         }
185     }
186 
187     // MISC
188     @Override
189     public boolean equals(Object o)
190     {
191         if (this == o) return true;
192         if (!(o instanceof LengthValidator)) return false;
193 
194         final LengthValidator lengthValidator = (LengthValidator)o;
195 
196         if (_maximum != null ? !_maximum.equals(lengthValidator._maximum) : lengthValidator._maximum != null) return false;
197         if (_minimum != null ? !_minimum.equals(lengthValidator._minimum) : lengthValidator._minimum != null) return false;
198 
199         return true;
200     }
201 
202     private boolean _initialStateMarked = false;
203 
204     public void clearInitialState()
205     {
206         _initialStateMarked = false;
207     }
208 
209     public boolean initialStateMarked()
210     {
211         return _initialStateMarked;
212     }
213 
214     public void markInitialState()
215     {
216         _initialStateMarked = true;
217     }
218     
219     @JSFProperty(faceletsOnly=true)
220     private Boolean isDisabled()
221     {
222         return null;
223     }
224     
225     @JSFProperty(faceletsOnly=true)
226     private String getFor()
227     {
228         return null;
229     }
230 }