KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) 2003 Iman L Crawford (icrawford@greatnation.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: ListValidator.java,v 1.9 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 validates all items even if OrigVal is an instance of
29  * <code>java.util.List</code>
30  *
31  * @author Iman L Crawford (icrawford@greatnation.com)
32  */

33 public class ListValidator extends AbstractFormValidator {
34
35     protected FormValidator fv = null;
36
37     /**
38      * Public constructor.
39      *
40      * @param ifv the validator we wish to make sure is not valid
41      */

42     public ListValidator(FormValidator ifv) {
43         this(ifv, null);
44     }
45
46     /**
47      * Public constructor.
48      *
49      * @param ifv the validator we wish to make sure is not valid
50      * @param ierrmsg the message associated with this error
51      */

52     public ListValidator(FormValidator ifv, String JavaDoc ierrmsg) {
53         setErrorMessage(ierrmsg);
54         fv = ifv;
55     }
56
57     /**
58      * Return the value that is being not'ed
59      *
60      * @return The form element to negate
61      */

62     public FormValidator getSubValidator() {
63         return fv;
64     }
65
66     /**
67      * Will check origVal's type and loop through all the values calling the
68      * subValidator.
69      *
70      * @param element the form element to be validated (null indicates
71      * we want to perform form level validation)
72      * @param formMap the map to which the element belongs (sometimes necessary
73      * to validate elements by comparing them with other elements)
74      * @param deferExceptions do we want to deferValidation exceptions
75      * and attempt to validate all elements so that we can process
76      * all the exceptions at once
77      * @throws ValidationException if the element is not valid
78      */

79     public void validate(FormElement element, FormMap formMap, boolean deferExceptions) throws ValidationException {
80         if (localLogger.isDebugEnabled()) localLogger.debug("Validating one or more items");
81 //csc_112103_1 if (element.getOrigVal() instanceof ArrayList) {
82
//csc_112103_1 ArrayList origList = (ArrayList)element.getOrigVal();
83
if (element.getOrigVal() instanceof List) { //csc_112103_1
84
List origList = (List) element.getOrigVal(); //csc_112103_1
85
for (int i=0; i<origList.size(); i++) {
86                 FormElement newElement = this.getNewElement(i, element);
87                 fv.validate((FormElement) newElement, formMap, deferExceptions);
88             }
89         } else if (fv!=null) {
90             fv.validate(element, formMap, deferExceptions);
91         }
92     }
93
94
95     /**
96      * Will return a duplicate of the element passed to it.
97      *
98      * @param index index of value to map from the original element.
99      * @param element the original form element to be validated
100      */

101     private FormElement getNewElement(int index, FormElement element) {
102         DefaultFormElement newElement = new DefaultFormElement();
103 //csc_112203_1 Object origVal = ((ArrayList)element.getOrigVal()).get(index);
104
//csc_112203_1 Object val = ((ArrayList)element.getVal()).get(index);
105
Object JavaDoc origVal = ((List) element.getOrigVal()).get(index); //csc_112203_1
106
Object JavaDoc val = ((List) element.getVal()).get(index); //csc_112203_1
107

108         newElement.setOrigVal(origVal);
109         newElement.setVal(val);
110         newElement.setKey(element.getKey());
111         newElement.setName(element.getName());
112         newElement.setType(element.getType());
113         newElement.setDefaultVal(element.getDefaultVal());
114         newElement.setAllowMultiples(false);
115         newElement.setValidator(fv);
116
117         try {
118             // don't need to parse if origVal is the expected type
119
Class JavaDoc typeclass = newElement.getType().getFormClass();
120             if (!typeclass.isInstance(origVal))
121               element.getType().parse(origVal.toString());
122         } catch (ParseException e) {
123             if (localLogger.isDebugEnabled()) localLogger.debug("ParseException:", e);
124             newElement.setParseException(e);
125         }
126
127
128         return (FormElement)newElement;
129     }
130
131 }
132
Popular Tags