001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.validation.validator;
018:
019: import java.util.Map;
020:
021: import org.apache.wicket.validation.IValidatable;
022:
023: /**
024: * Validator for dealing with string lengths. Usually this validator is used
025: * through the static factory methods, but it and its inner classes can also be
026: * subclassed directly.
027: *
028: * @author Jonathan Locke
029: * @author Johan Compagner
030: * @author Igor Vaynberg (ivaynberg)
031: */
032: public abstract class StringValidator extends AbstractValidator {
033:
034: /**
035: * Validator to check if the length of the string is exactly the specified
036: * length
037: */
038: public static class ExactLengthValidator extends StringValidator {
039: private static final long serialVersionUID = 1L;
040: private final int length;
041:
042: /**
043: * Construct.
044: *
045: * @param length
046: */
047: public ExactLengthValidator(int length) {
048: this .length = length;
049: }
050:
051: /**
052: * Gets length.
053: *
054: * @return length
055: */
056: public final int getLength() {
057: return length;
058: }
059:
060: protected void onValidate(IValidatable validatable) {
061: if (((String) validatable.getValue()).length() != length) {
062: error(validatable);
063: }
064: }
065:
066: /**
067: * @see org.apache.wicket.markup.html.form.validation.AbstractValidator#resourceKey(org.apache.wicket.markup.html.form.FormComponent)
068: */
069: protected String resourceKey() {
070: return "StringValidator.exact";
071: }
072:
073: protected Map variablesMap(IValidatable validatable) {
074: final Map map = super .variablesMap(validatable);
075: map.put("length", new Integer(((String) validatable
076: .getValue()).length()));
077: map.put("exact", new Integer(this .length));
078: return map;
079: }
080:
081: }
082:
083: /**
084: * Validator to check if the length of the string is within some range
085: */
086: public static class LengthBetweenValidator extends StringValidator {
087: private static final long serialVersionUID = 1L;
088: private final int maximum;
089: private final int minimum;
090:
091: /**
092: * Construct.
093: *
094: * @param minimum
095: * @param maximum
096: */
097: public LengthBetweenValidator(int minimum, int maximum) {
098: this .minimum = minimum;
099: this .maximum = maximum;
100:
101: }
102:
103: /**
104: * Gets maximum.
105: *
106: * @return maximum
107: */
108: public final int getMaximum() {
109: return maximum;
110: }
111:
112: /**
113: * Gets minimum.
114: *
115: * @return minimum
116: */
117: public final int getMinimum() {
118: return minimum;
119: }
120:
121: protected void onValidate(IValidatable validatable) {
122: final String value = (String) validatable.getValue();
123: if (value.length() < minimum || value.length() > maximum) {
124: error(validatable);
125: }
126:
127: }
128:
129: /**
130: * @see org.apache.wicket.markup.html.form.validation.AbstractValidator#resourceKey(org.apache.wicket.markup.html.form.FormComponent)
131: */
132: protected String resourceKey() {
133: return "StringValidator.range";
134: }
135:
136: /**
137: * @see org.apache.wicket.validation.validator.AbstractValidator#variablesMap(org.apache.wicket.validation.IValidatable)
138: */
139: protected Map variablesMap(IValidatable validatable) {
140: final Map map = super .variablesMap(validatable);
141: map.put("minimum", new Integer(minimum));
142: map.put("maximum", new Integer(maximum));
143: map.put("length", new Integer(((String) validatable
144: .getValue()).length()));
145: return map;
146: }
147:
148: }
149:
150: /**
151: * Validator to check if the length of the string meets a maximum
152: * requirement
153: */
154: public static class MaximumLengthValidator extends StringValidator {
155: private static final long serialVersionUID = 1L;
156: private final int maximum;
157:
158: /**
159: * Construct.
160: *
161: * @param maximum
162: */
163: public MaximumLengthValidator(int maximum) {
164: this .maximum = maximum;
165: }
166:
167: /**
168: * Gets maximum.
169: *
170: * @return maximum
171: */
172: public final int getMaximum() {
173: return maximum;
174: }
175:
176: protected void onValidate(IValidatable validatable) {
177: if (((String) validatable.getValue()).length() > maximum) {
178: error(validatable);
179: }
180: }
181:
182: /**
183: * @see org.apache.wicket.markup.html.form.validation.AbstractValidator#resourceKey(org.apache.wicket.markup.html.form.FormComponent)
184: */
185: protected String resourceKey() {
186: return "StringValidator.maximum";
187: }
188:
189: /**
190: * @see org.apache.wicket.validation.validator.AbstractValidator#variablesMap(org.apache.wicket.validation.IValidatable)
191: */
192: protected Map variablesMap(IValidatable validatable) {
193: final Map map = super .variablesMap(validatable);
194: map.put("maximum", new Integer(maximum));
195: map.put("length", new Integer(((String) validatable
196: .getValue()).length()));
197: return map;
198: }
199: }
200:
201: /**
202: * Validator to check if the length of the string meets a minumum
203: * requirement
204: */
205: public static class MinimumLengthValidator extends StringValidator {
206: private static final long serialVersionUID = 1L;
207: private final int minimum;
208:
209: /**
210: * Construct.
211: *
212: * @param minimum
213: */
214: public MinimumLengthValidator(int minimum) {
215: this .minimum = minimum;
216: }
217:
218: /**
219: * Gets minimum.
220: *
221: * @return minimum
222: */
223: public final int getMinimum() {
224: return minimum;
225: }
226:
227: protected void onValidate(IValidatable validatable) {
228: if (((String) validatable.getValue()).length() < minimum) {
229: error(validatable);
230: }
231: }
232:
233: /**
234: * @see org.apache.wicket.markup.html.form.validation.AbstractValidator#resourceKey(org.apache.wicket.markup.html.form.FormComponent)
235: */
236: protected String resourceKey() {
237: return "StringValidator.minimum";
238: }
239:
240: /**
241: * @see org.apache.wicket.validation.validator.AbstractValidator#variablesMap(org.apache.wicket.validation.IValidatable)
242: */
243: protected Map variablesMap(IValidatable validatable) {
244: final Map map = super .variablesMap(validatable);
245: map.put("minimum", new Integer(minimum));
246: map.put("length", new Integer(((String) validatable
247: .getValue()).length()));
248: return map;
249: }
250:
251: }
252:
253: /**
254: * Gets a String exact length validator to check if a string length is
255: * exactly the same as the given value
256: *
257: * If that is not the case then an error message will be generated with the
258: * key "StringValidator.exact" and the messages keys that can be used are:
259: * <ul>
260: * <li>${exact}: the maximum length</li>
261: * <li>${length}: the length of the user input</li>
262: * <li>${input}: the input the user did give</li>
263: * <li>${name}: the name of the component that failed</li>
264: * <li>${label}: the label of the component - either comes from
265: * FormComponent.labelModel or resource key [form-id].[form-component-id] in
266: * that order</li>
267: * </ul>
268: *
269: * @param length
270: * The required length of the string.
271: *
272: * @return The StringValidator
273: */
274: public static StringValidator exactLength(int length) {
275: return new ExactLengthValidator(length);
276: }
277:
278: /**
279: * Gets a String range validator to check if a string length is between min
280: * and max.
281: *
282: * If that is not the case then an error message will be generated with the
283: * key "StringValidator.range" and the messages keys that can be used are:
284: * <ul>
285: * <li>${minimum}: the minimum length</li>
286: * <li>${maximum}: the maximum length</li>
287: * <li>${length}: the length of the user input</li>
288: * <li>${input}: the input the user did give</li>
289: * <li>${name}: the name of the component that failed</li>
290: * <li>${label}: the label of the component - either comes from
291: * FormComponent.labelModel or resource key [form-id].[form-component-id] in
292: * that order</li>
293: * </ul>
294: *
295: * @param minimum
296: * The minimum length of the string.
297: * @param maximum
298: * The maximum length of the string.
299: *
300: * @return The StringValidator
301: */
302: public static StringValidator lengthBetween(int minimum, int maximum) {
303: return new LengthBetweenValidator(minimum, maximum);
304: }
305:
306: /**
307: * Gets a String maximum validator to check if a string length is smaller
308: * then the given maximum value.
309: *
310: * If that is not the case then an error message will be generated with the
311: * key "StringValidator.maximum" and the messages keys that can be used are:
312: * <ul>
313: * <li>${maximum}: the maximum length</li>
314: * <li>${length}: the length of the user input</li>
315: * <li>${input}: the input the user did give</li>
316: * <li>${name}: the name of the component that failed</li>
317: * <li>${label}: the label of the component - either comes from
318: * FormComponent.labelModel or resource key [form-id].[form-component-id] in
319: * that order</li>
320: * </ul>
321: *
322: * @param maximum
323: * The maximum length of the string.
324: *
325: * @return The StringValidator
326: */
327: public static StringValidator maximumLength(int maximum) {
328: return new MaximumLengthValidator(maximum);
329: }
330:
331: /**
332: * Gets a String minimum validator to check if a string length is greater
333: * then the given minimum value.
334: *
335: * If that is not the case then an error message will be generated with the
336: * key "StringValidator.minimum" and the messages keys that can be used are:
337: * <ul>
338: * <li>${minimum}: the minimum length</li>
339: * <li>${length}: the length of the user input</li>
340: * <li>${input}: the input the user did give</li>
341: * <li>${name}: the name of the component that failed</li>
342: * <li>${label}: the label of the component - either comes from
343: * FormComponent.labelModel or resource key [form-id].[form-component-id] in
344: * that order</li>
345: * </ul>
346: *
347: * @param minimum
348: * The minimum length of the string.
349: *
350: * @return The StringValidator
351: */
352: public static StringValidator minimumLength(int minimum) {
353: return new MinimumLengthValidator(minimum);
354: }
355: }
|