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  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: lu4242 $)
42   * @author Thomas Spiegl
43   * @version $Revision: 693358 $ $Date: 2008-09-08 22:54:29 -0500 (Mon, 08 Sep 2008) $
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, StateHolder
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 = new Integer(maximum);
73      }
74  
75      public LengthValidator(int maximum,
76                             int minimum)
77      {
78          _maximum = new Integer(maximum);
79          _minimum = new Integer(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) throw new NullPointerException("facesContext");
89          if (uiComponent == null) throw new NullPointerException("uiComponent");
90  
91          if (value == null)
92          {
93              return;
94          }
95  
96          int length = value instanceof String ?
97              ((String)value).length() : value.toString().length();
98  
99          if (_minimum != null)
100         {
101             if (length < _minimum.intValue())
102             {
103                 Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)};
104                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
105             }
106         }
107 
108         if (_maximum != null)
109         {
110             if (length > _maximum.intValue())
111             {
112                 Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
113                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
114             }
115         }
116     }
117 
118     // SETTER & GETTER
119     
120     /** 
121      * The largest value that should be considered valid.
122      * 
123      */
124     @JSFProperty
125     public int getMaximum()
126     {
127         return _maximum != null ? _maximum.intValue() : 0;
128     }
129 
130     public void setMaximum(int maximum)
131     {
132         _maximum = new Integer(maximum);
133     }
134 
135     /**
136      * The smallest value that should be considered valid.
137      *  
138      */
139     @JSFProperty
140     public int getMinimum()
141     {
142         return _minimum != null ? _minimum.intValue() : 0;
143     }
144 
145     public void setMinimum(int minimum)
146     {
147         _minimum = new Integer(minimum);
148     }
149 
150     public boolean isTransient()
151     {
152         return _transient;
153     }
154 
155     public void setTransient(boolean transientValue)
156     {
157         _transient = transientValue;
158     }
159 
160     // RESTORE & SAVE STATE
161     public Object saveState(FacesContext context)
162     {
163         Object values[] = new Object[2];
164         values[0] = _maximum;
165         values[1] = _minimum;
166         return values;
167     }
168 
169     public void restoreState(FacesContext context,
170                              Object state)
171     {
172         Object values[] = (Object[])state;
173         _maximum = (Integer)values[0];
174         _minimum = (Integer)values[1];
175     }
176 
177     // MISC
178     public boolean equals(Object o)
179     {
180         if (this == o) return true;
181         if (!(o instanceof LengthValidator)) return false;
182 
183         final LengthValidator lengthValidator = (LengthValidator)o;
184 
185         if (_maximum != null ? !_maximum.equals(lengthValidator._maximum) : lengthValidator._maximum != null) return false;
186         if (_minimum != null ? !_minimum.equals(lengthValidator._minimum) : lengthValidator._minimum != null) return false;
187 
188         return true;
189     }
190 
191 }