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