1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
160 log.warn(fieldName + " with value: " + value +
161 " is of the wrong type for checking on length");
162
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 }