View Javadoc

1   /*
2    * $Id: MinimumFieldLengthValidator.java,v 1.7 2004/04/25 10:02:36 eelco12 Exp $
3    * $Revision: 1.7 $
4    * $Date: 2004/04/25 10:02:36 $
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 minimum 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 minLength is 4, "hello" will pass, but "hi" 
45   * will fail, and number 5 will pass, but 2 will fail.
46   * 
47   * @author Eelco Hillenius
48   */
49  public final class MinimumFieldLengthValidator extends AbstractFieldValidator
50  {
51  	/*** special value that indicates there's no min value to check on */
52  	public final static int NO_MINIMUM = -1;
53  
54  	private int minLength = NO_MINIMUM;
55  	
56  	private static Log log = LogFactory.getLog(MinimumFieldLengthValidator.class);
57  	
58  	private String errorMessageKey = "invalid.field.input.size";
59  
60  	/***
61  	 * Construct with key invalid.field.input.size for error messages.
62  	 */
63  	public MinimumFieldLengthValidator()
64  	{
65  
66  	}
67  
68  	/***
69  	 * Construct with message prefix for error message keys.
70  	 * @param errorMessageKey message key of error
71  	 */
72  	public MinimumFieldLengthValidator(String errorMessageKey)
73  	{
74  		setErrorMessageKey(errorMessageKey);
75  	}
76  	
77  	/***
78  	 * Construct with message key for error message keys and set
79  	 * checking on minimum length with given length of fields only.
80  	 * @param errorMessageKey key for error messages
81  	 * @param minLength minimum length allowed for values; use -1 for no minimum
82  	 */
83  	public MinimumFieldLengthValidator(String errorMessageKey, int minLength)
84  	{
85  		setErrorMessageKey(errorMessageKey);
86  		setMinLength(minLength);
87  	}
88  	
89  	/***
90  	 * Construct with activation rule.
91  	 * @param rule activation rule
92  	 */
93  	public MinimumFieldLengthValidator(ValidationActivationRule rule)
94  	{
95  		setValidationRule(rule);
96  	}
97  
98  	/***
99  	 * Construct with errorMessageKey and activation rule.
100 	 * @param errorMessageKey
101 	 * @param rule activation rule
102 	 */
103 	public MinimumFieldLengthValidator(
104 		String errorMessageKey,
105 		ValidationActivationRule rule)
106 	{
107 		setErrorMessageKey(errorMessageKey);
108 		setValidationRule(rule);
109 	}
110 
111 	/***
112 	 * Construct with min length.
113 	 * @param minLength minimum length allowed for values; use -1 for no minimum
114 	 */
115 	public MinimumFieldLengthValidator(int minLength)
116 	{
117 		setMinLength(minLength);
118 	}
119 
120 	/***
121 	 * Checks whether the provided value is greater than the minimum.
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 minLength 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 minLength property.
126 	 * @return boolean true if the length of value is equal to or less than the
127 	 * 	minLength 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 minExceeded = false;
137 		if(value != null)
138 		{
139 			if(value instanceof String)
140 			{
141 				String toCheck = (String)value;
142 				int length = toCheck.length();
143 				if(minLength != NO_MINIMUM)
144 				{
145 					minExceeded = (length < minLength);
146 				}
147 			}
148 			else if(value instanceof Number)
149 			{
150 				Number toCheck = (Number)value;
151 				int length = toCheck.intValue();
152 				if(minLength != NO_MINIMUM)
153 				{
154 					minExceeded = (length < minLength);
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"); // give a warning though
162 			}
163 		}
164 		
165 		if(minExceeded)
166 		{
167 			setErrorMessage(formBeanContext, fieldName, getErrorMessageKey(), 
168 				new Object[]{getFieldName(formBeanContext, fieldName), 
169 				value, new Integer(minLength)});
170 		}
171 		
172 		return (!minExceeded);
173 	}
174 
175 	/***
176 	 * @return int minimum length that is checked on
177 	 */
178 	public int getMinLength()
179 	{
180 		return minLength;
181 	}
182 
183 	/***
184 	 * @param i minimum length that is checked on 
185 	 */
186 	public void setMinLength(int i)
187 	{
188 		minLength = i;
189 	}
190 	
191 	/***
192 	 * Get key of error message.
193 	 * @return String key of error message
194 	 */
195 	public String getErrorMessageKey()
196 	{
197 		return errorMessageKey;
198 	}
199 
200 	/***
201 	 * Set key of error message.
202 	 * @param string key of error message
203 	 */
204 	public void setErrorMessageKey(String string)
205 	{
206 		errorMessageKey = string;
207 	}
208 
209 }