KickJava   Java API By Example, From Geeks To Geeks.

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


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: CardinalityValidator.java,v 1.5 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 import org.apache.log4j.*;
27
28 /**
29  * This validator ensures that a value is a valid date
30  *
31  * @author Diez B. Roggisch <diez.roggisch@artnology.com>
32  * @version %I%, %G%
33  * @since 1.0
34  */

35 public class CardinalityValidator extends DefaultFormValidator {
36
37     int _min, _max;
38
39     public static final FormValidator ONE = new CardinalityValidator(1, 1);
40     public static final FormValidator ONE_TO_MANY = new CardinalityValidator(1, Integer.MAX_VALUE);
41     public static final FormValidator ZERO_TO_MANY = new CardinalityValidator(0, Integer.MAX_VALUE);
42     public static final FormValidator ZERO_TO_ONE = new CardinalityValidator(0, 1);
43
44     /**
45      * Public constructor.
46      *
47      */

48     public CardinalityValidator(int min, int max) {
49         super();
50         _min = min;
51         _max = max;
52     }
53
54     /**
55      * Public constructor.
56      *
57      */

58     public CardinalityValidator(String JavaDoc ierrmsg, int min, int max) {
59         super(ierrmsg);
60         _min = min;
61         _max = max;
62     }
63
64     /**
65      * Validate a FormElement to make sure the number of values is in the specified range
66      */

67
68     public void validateFormElement(Object JavaDoc val, FormElement element, boolean deferExceptions) throws ValidationException {
69 //csc_112202.2 Object [] values = element.getMultipleValues();
70
Object JavaDoc [] values = element.getVals(); //csc_112202.2
71

72         if (values.length < _min || values.length > _max) {
73             if (super.getErrorMessage()!=null) {
74                 throw this.generateException(element, deferExceptions, super.getErrorMessage());
75             } else {
76                 throw this.generateException(element, deferExceptions, values.toString()
77                                              + " with length " + values.length
78                                              + " is not in the valid range [" + _min + ":" + _max + "]");
79             }
80
81         }
82     }
83
84 }
85
Popular Tags