View Javadoc

1   /*
2    * $Id: MaximumFieldLengthValidator.java,v 1.7 2004/04/25 10:02:37 eelco12 Exp $
3    * $Revision: 1.7 $
4    * $Date: 2004/04/25 10:02:37 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package nl.openedge.baritus.validation.impl;
32  
33  import nl.openedge.baritus.FormBeanContext;
34  import nl.openedge.baritus.validation.AbstractFieldValidator;
35  import nl.openedge.baritus.validation.ValidationActivationRule;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  import org.infohazard.maverick.flow.ControllerContext;
40  
41  /***
42   * This validator checks on maximum length. If the type of the value is a String,
43   * the string length is checked. If the type of the value is a Number, the actual
44   * number is used. E.g. if property maxLength is 4, "hello" will fail, but "hi" 
45   * will pass, and number 5 will fail, but 2 will pass.
46   *  
47   * @author Eelco Hillenius
48   */
49  public final class MaximumFieldLengthValidator extends AbstractFieldValidator
50  {
51  	/*** special value that indicates there's no maximum value to check on */
52  	public final static int NO_MAXIMUM = -1;
53  
54  	private int maxLength = NO_MAXIMUM;
55  	
56  	private static Log log = LogFactory.getLog(MaximumFieldLengthValidator.class);
57  	
58  	private String errorMessageKey = "invalid.field.input.size";
59  
60  	/***
61  	 * construct with 'invalid.field.input.size' as message prefix.
62  	 */
63  	public MaximumFieldLengthValidator()
64  	{
65  
66  	}
67  
68  	/***
69  	 * Construct with errorMessageKey for error message keys.
70  	 * @param errorMessageKey
71  	 */
72  	public MaximumFieldLengthValidator(String errorMessageKey)
73  	{
74  		setErrorMessageKey(errorMessageKey);
75  	}
76  	
77  	/***
78  	 * Construct with message prefix for error message keys and set
79  	 * checking on maximum length with given length of fields only.
80  	 * @param errorMessageKey message key
81  	 * @param maxLength maximum length allowed for values; use -1 for no maximum
82  	 */
83  	public MaximumFieldLengthValidator(String errorMessageKey, int maxLength)
84  	{
85  		setErrorMessageKey(errorMessageKey);
86  		setMaxLength(maxLength);
87  	}
88  	
89  	/***
90  	 * Construct with activation rule.
91  	 * @param rule activation rule
92  	 */
93  	public MaximumFieldLengthValidator(ValidationActivationRule rule)
94  	{
95  		setValidationRule(rule);
96  	}
97  
98  	/***
99  	 * Construct with errorMessageKey and activation rule.
100 	 * @param errorMessageKey error message key
101 	 * @param rule activation rule
102 	 */
103 	public MaximumFieldLengthValidator(
104 		String errorMessageKey,
105 		ValidationActivationRule rule)
106 	{
107 		setErrorMessageKey(errorMessageKey);
108 		setValidationRule(rule);
109 	}
110 
111 	/***
112 	 * Construct with maxLength.
113 	 * @param maxLength maximum length allowed for values; use -1 for no maximum
114 	 */
115 	public MaximumFieldLengthValidator(int maxLength)
116 	{
117 		setMaxLength(maxLength);
118 	}
119 
120 	/***
121 	 * Checks whether the provided value is less than the maximum.
122 	 * In case the value is an instance of string: checks whether the length of the string
123 	 * is equal to or smaller than the maximumLength property.
124 	 * In case the value is an instance of number: checks whether the length of the integer
125 	 * value is equal to or smaller than the maximumLength property.
126 	 * @return boolean true if the length of value is equal to or less than the
127 	 * 	maxLength property, false otherwise
128 	 * @see nl.openedge.baritus.validation.FieldValidator#isValid(org.infohazard.maverick.flow.ControllerContext, nl.openedge.baritus.FormBeanContext, java.lang.String, java.lang.Object)
129 	 */
130 	public boolean isValid(
131 		ControllerContext cctx,
132 		FormBeanContext formBeanContext,
133 		String fieldName,
134 		Object value)
135 	{
136 		boolean maxExceeded = false;
137 		if(value != null)
138 		{
139 			if(value instanceof String)
140 			{
141 				String toCheck = (String)value;
142 				int length = toCheck.length();
143 				if(maxLength != NO_MAXIMUM)
144 				{
145 					maxExceeded = (length > maxLength);
146 				}
147 			}
148 			else if(value instanceof Number)
149 			{
150 				Number toCheck = (Number)value;
151 				int length = toCheck.intValue();
152 				if(maxLength != NO_MAXIMUM)
153 				{
154 					maxExceeded = (length > maxLength);
155 				}
156 			}
157 			else
158 			{
159 				// just ignore; wrong type to check on
160 				log.warn(fieldName + " with value: " + value + 
161 					" is of the wrong type for checking on length"); 
162 				// give a warning though
163 			}
164 		}
165 		
166 		if(maxExceeded)
167 		{
168 			setErrorMessage(formBeanContext, fieldName, getErrorMessageKey(), 
169 				new Object[]{getFieldName(formBeanContext, fieldName), 
170 				value, new Integer(maxLength)});
171 		}
172 		
173 		return (!maxExceeded);
174 	}
175 
176 	/***
177 	 * Get maximum length that is checked on.
178 	 * @return int maximum length that is checked on
179 	 */
180 	public int getMaxLength()
181 	{
182 		return maxLength;
183 	}
184 
185 	/***
186 	 * Set maximum length that is checked on .
187 	 * @param i maximum length that is checked on 
188 	 */
189 	public void setMaxLength(int i)
190 	{
191 		maxLength = i;
192 	}
193 
194 	/***
195 	 * Get key of error message.
196 	 * @return String key of error message
197 	 */
198 	public String getErrorMessageKey()
199 	{
200 		return errorMessageKey;
201 	}
202 
203 	/***
204 	 * Set key of error message.
205 	 * @param string key of error message
206 	 */
207 	public void setErrorMessageKey(String string)
208 	{
209 		errorMessageKey = string;
210 	}
211 
212 }