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