KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > ValidationException


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: ValidationException.java,v 1.10 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.enhydra.barracuda.plankton.exceptions.NestableException;
26
27 /**
28  * This class defines the validation exception. Code that catches
29  * these exceptions should check for subExceptions, as this class
30  * can be used to group collections of ValidationExceptions.
31  */

32 public class ValidationException extends NestableException {
33
34     protected List subExceptions = null;
35     protected Object JavaDoc source = null;
36     protected static final String JavaDoc sep = System.getProperty("line.separator");
37     
38     /**
39      * The noargs public contructor for ValidationException
40      */

41     public ValidationException () {
42         this("Generic Validation Exception");
43     }
44
45     /**
46      * The public contructor for ValidationException
47      *
48      * @param s a String describing the exception
49      */

50     public ValidationException (String JavaDoc s) {
51         this(null, s);
52     }
53
54     /**
55      * The public contructor for ValidationException
56      *
57      * @param source the object which caused this error (usually a
58      * FormElement)
59      */

60     public ValidationException (Object JavaDoc source) {
61         this(source, null);
62     }
63
64     /**
65      * The public contructor for ValidationException
66      *
67      * @param source the object which caused this error (usually a
68      * FormElement)
69      * @param s a String describing the exception
70      */

71     public ValidationException (Object JavaDoc source, String JavaDoc s) {
72         this(source, s, null);
73     }
74
75     /**
76      * The public contructor for ValidationException
77      *
78      * @param isource the object which caused this error (usually a
79      * FormElement)
80      * @param s a String describing the exception
81      * @param ibaseException the original exception to wrap within this exception
82      */

83     public ValidationException (Object JavaDoc isource, String JavaDoc s, Exception JavaDoc ibaseException) {
84         super(s, ibaseException);
85         source = isource;
86     }
87
88     /**
89      * Get the form element which caused this error
90      *
91      * @return the form element which caused this error
92      */

93     public Object JavaDoc getSource() {
94         return source;
95     }
96     
97     /**
98      * See if this particular ValidationException has
99      * sub-exceptions
100      *
101      * @return true if this particular ValidationException has
102      * sub-exceptions
103      */

104     public boolean hasSubExceptions() {
105         return (subExceptions!=null && subExceptions.size()>0);
106     }
107     
108     /**
109      * Add a sub exception
110      *
111      * @param ve a sub-exception to be added
112      */

113     public void addSubException(ValidationException ve) {
114         if (subExceptions==null) subExceptions = new ArrayList();
115         subExceptions.add(ve);
116     }
117
118     /**
119      * Get a copy of the list of sub-exceptions. May be null
120      * if there are no sub-exceptions.
121      *
122      * @return a copy of the list of sub-exceptions
123      */

124     public List getSubExceptions() {
125         return (subExceptions==null ? null : new ArrayList(subExceptions));
126     }
127     
128     public String JavaDoc toString() {
129         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(200);
130         sb.append(this.getClass().getName()+"@"+Integer.toHexString(this.hashCode()));
131         sb.append(", Msg:"+getMessage());
132         if (source!=null && source instanceof ValidationException) sb.append(", Src: ValidationException[@"+Integer.toHexString(source.hashCode())+"]");
133         else sb.append(", Src:"+source);
134         if (subExceptions!=null) {
135             sb.append(sep+"Sub-exceptions:");
136             Iterator it = subExceptions.iterator();
137             while (it.hasNext()) {
138                 sb.append(sep+" "+it.next());
139             }
140         }
141         return sb.toString();
142     }
143
144     /**
145      * Collapse all sub-exceptions into a single list of
146      * Validation exceptions
147      */

148     public List getExceptionList() {
149         List list = new ArrayList();
150         list.addAll(getExceptionList(this));
151         return list;
152     }
153     
154     protected List getExceptionList(ValidationException ve) {
155         List list = new ArrayList();
156         if (!ve.hasSubExceptions()) {
157             list.add(ve);
158         } else {
159             Iterator it = ve.getSubExceptions().iterator();
160             while (it.hasNext()) {
161                 list.addAll(getExceptionList((ValidationException) it.next()));
162             }
163         }
164         return list;
165     }
166
167 }
168
Popular Tags