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