1 /*
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 1999-2003 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * $Id$
44 */
45 package org.exolab.castor.xml;
46
47 import org.exolab.castor.xml.location.Location;
48
49 /**
50 * An Exception that can be used to signal XML validation errors.
51 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
52 * @version $Revision$ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
53 */
54 public class ValidationException extends XMLException {
55 /** SerialVersionUID */
56 private static final long serialVersionUID = 2220902174700444631L;
57
58 /**
59 * The location for this exception.
60 */
61 private Location _location = null;
62
63 /**
64 * The next Exception in the list, allowing the reporting of several
65 * validation exceptions.
66 */
67 private ValidationException _next = null;
68
69 /**
70 * Creates a new {@link ValidationException} with no message or nested exception.
71 */
72 public ValidationException() {
73 super();
74 }
75
76 /**
77 * Creates a new {@link ValidationException} with the given message.
78 * @param message the message for this exception
79 */
80 public ValidationException(final String message) {
81 super(message);
82 }
83
84 /**
85 * Creates a new {@link ValidationException} with the given message.
86 * @param message the message for this exception
87 * @param errorCode the errorCode for this exception
88 *
89 * @deprecated
90 */
91 public ValidationException(final String message, final int errorCode) {
92 super(message, errorCode);
93 }
94
95 /**
96 * Creates a new ValidationException with the given nested Exception.
97 * @param exception the nested Exception
98 */
99 public ValidationException(final Throwable exception) {
100 super(exception);
101 }
102
103 /**
104 * Creates a new {@link ValidationException} with the given message and nested
105 * exception.
106 *
107 * @param message the detail message for this exception
108 * @param exception the nested exception
109 */
110 public ValidationException(final String message, final Throwable exception) {
111 super(message, exception);
112 }
113
114 /**
115 * Creates a new {@link ValidationException} with the given message, nested
116 * exception, and error code.
117 *
118 * @param message the detail message for this exception
119 * @param exception the nested exception
120 * @param errorCode the error code for this exception
121 *
122 * @deprecated
123 */
124 public ValidationException(final String message, final Exception exception,
125 final int errorCode) {
126 super(message, exception, errorCode);
127 }
128
129 /**
130 * Returns the location of the Exception.
131 *
132 * @return the location of the Exception.
133 */
134 public Location getLocation() {
135 return _location;
136 }
137
138 /**
139 * Returns the next ValidationException in the list, or null if no
140 * additional validation exceptions exist.
141 *
142 * @return the next ValidationException in the list, or null if there are no
143 * additional Exceptions.
144 */
145 public ValidationException getNext() {
146 return _next;
147 }
148
149 /**
150 * Sets the location information for this ValidationException.
151 *
152 * @param location The location information for this validation Exception.
153 */
154 public void setLocation(final Location location) {
155 _location = location;
156 }
157
158 /**
159 * Removes the given ValidationException from the current list of
160 * ValidationException.
161 *
162 * @param exception the ValidationException to remove
163 * @return true if the given ValidationException was successfully removed.
164 */
165 protected boolean remove(final ValidationException exception) {
166 if (exception == null) {
167 return false;
168 }
169
170 ValidationException previous = this;
171 for (ValidationException current = _next; current != null;
172 previous = current, current = current._next) {
173 if (current == exception) {
174 previous._next = current._next;
175 current._next = null;
176 return true;
177 }
178 }
179 return false;
180 }
181
182 /**
183 * Adds the given ValidationException as the last exception in the list.
184 * This is equivalent to calling {@link #setNext} if no additional
185 * ValidationException(s) exist.
186 *
187 * @param exception the ValidationException to set as the last exception in
188 * the list.
189 */
190 protected void setLast(final ValidationException exception) {
191 if (exception == null) {
192 return;
193 }
194
195 ValidationException current = this;
196 while (current._next != null) {
197 current = current._next;
198 }
199 current._next = exception;
200 }
201
202 /**
203 * Sets the given ValidationException as the next Exception in the list.
204 * This method will overwrite any existing ValidationException that may
205 * already exist as the next Exception.
206 *
207 * @param exception the ValidationException to set as the next Exception in
208 * the list.
209 */
210 public void setNext(final ValidationException exception) {
211 _next = exception;
212 }
213
214 /**
215 * Returns the String representation of this ValidationException.
216 * @return the String representation of this ValidationException.
217 */
218 public String toString() {
219 final StringBuffer sb = new StringBuffer();
220 if (getNext() != null) {
221 int count = 1;
222 for (ValidationException vx = this; vx != null; vx = vx.getNext()) {
223 if (count > 1) {
224 sb.append("\n\n");
225 }
226 sb.append(count);
227 sb.append(". ");
228 dumpOneException(sb, vx);
229 ++count;
230 }
231 } else {
232 dumpOneException(sb, this);
233 }
234 return sb.toString();
235 }
236
237 /**
238 * Dump information for a single ValidationException.
239 * @param sb The StringBuffer to which we print information
240 * @param vx The ValidationException for which we print information.
241 */
242 private void dumpOneException(final StringBuffer sb, final ValidationException vx) {
243 sb.append("ValidationException: ");
244 String message = vx.getMessage();
245 if (message != null) {
246 sb.append(message);
247 }
248 Location location = vx.getLocation();
249 if (location != null) {
250 sb.append(";\n - location of error: ");
251 sb.append(location.toString());
252 }
253 Throwable t = vx.getCause();
254 if (t != null) {
255 sb.append("\n");
256 sb.append(t.getMessage());
257 }
258 }
259
260 }