KickJava   Java API By Example, From Geeks To Geeks.

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


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: MaxLengthValidator.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 maximum length
29  */

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

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

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

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

67     public int getMaxLength() {
68         return max;
69     }
70
71     /**
72      * Validate a FormElement to make sure the length of element does not
73      * exceed a Max 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         // a null value will always be less than the maximum length
86
// ilc_022502_start
87
// use isNull method
88
// if (val==null) return;
89
if (this.isNull(val, element))
90           return;
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 <= max
100
String JavaDoc s = val.toString();
101         if (localLogger.isInfoEnabled()) localLogger.info("validating to see if length of val {"+s+"} <= "+max);
102         if (s.length()>max) {
103             throw this.generateException(element, deferExceptions, "Length of val {"+val+"} exceeds maximum length of "+max);
104         }
105     }
106
107 }
108
Popular Tags