KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > validators > MinLengthValidator


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: MinLengthValidator.java,v 1.14 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms.validators;
21
22 import java.util.*;
23
24 import org.enhydra.barracuda.core.forms.*;
25 import org.enhydra.barracuda.plankton.*;
26
27 /**
28  * This validator ensures that the length of a value is >= a minimum length
29  */

30 public class MinLengthValidator extends DefaultFormValidator {
31
32     protected int min = 0;
33
34     /**
35      * Public no-args constructor.
36      */

37     public MinLengthValidator() {
38         this(0, null);
39     }
40
41     /**
42      * Public constructor.
43      *
44      * @param imin the min length
45      */

46     public MinLengthValidator(int imin) {
47         this(imin, null);
48     }
49
50     /**
51      * Public constructor.
52      *
53      * @param imin the min length
54      * @param ierrmsg the message associated with this error
55      */

56     public MinLengthValidator(int imin, String JavaDoc ierrmsg) {
57         super(ierrmsg);
58         min = imin;
59     }
60
61     //bw_102501.1 - added
62
/**
63      * Get the minimum allowable length
64      *
65      * @return The minimum allowable length of this field.
66      */

67     public int getMinLength() {
68         return min;
69     }
70
71     /**
72      * Validate a FormElement to make sure the length of element is not
73      * less than a Min length. Validation is not supported for FormType.BOOLEAN
74      * and FormType.DATE
75      *
76      * @param val the value to compare the element value to
77      * @param element the form element that contains the val
78      * to validate elements by comparing them with other elements)
79      * @param deferExceptions do we want to deferValidation exceptions
80      * and attempt to validate all elements so that we can process
81      * all the exceptions at once
82      * @throws ValidationException if the element is not valid
83      */

84     public void validateFormElement(Object JavaDoc val, FormElement element, boolean deferExceptions) throws ValidationException {
85
86         // ilc_022602.1_start
87
// use make no value judgements on null, use NotNullValidator
88
if (this.isNull(val, element))
89           return;
90         // ilc_022602.1_end
91

92         //if element is null, or form type=BOOLEAN or DATE, err because this should
93
//not be considered a valid option (note that these exceptions are always
94
//immediate since they typically represent a programming error)
95
if (element==null) throw new ValidationException(val, "Object val:"+val+" is associated with a null FormElement");
96         if (element.getType().equals(FormType.BOOLEAN)) throw new ValidationException(val, "Unsupported validation: "+val+" is of FormType.BOOLEAN and cannot be validated by this validator");
97         if (element.getType().equals(FormType.DATE)) throw new ValidationException(val, "Unsupported validation: "+val+" is of FormType.DATE and cannot be validated by this validator");
98
99         //validate the length >= min
100
String JavaDoc s = (null!=val) ? val.toString(): "" ;
101
102         if (localLogger.isInfoEnabled()) localLogger.info("validating to see if length of val {"+s+"} >= "+min);
103         if (s.length()<min) {
104             throw this.generateException(element, deferExceptions, "Length of val {"+val+"} fails to meet minimum length of "+min);
105         }
106     }
107
108 }
109
Popular Tags