1 /* Copyright 2002-2005 Elliotte Rusty Harold
2 
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6 
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU Lesser General Public License for more details.
11 
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307  USA
16 
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@ibiblio.org. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */
21 
22 package nu.xom;
23 
24 import java.util.ArrayList;
25 import java.util.List;
26 
27 import org.xml.sax.SAXParseException;
28 
29 /**
30  * <p>
31  *  Signals a validity error in a document being parsed.
32  *  These are not thrown by default, unless you specifically
33  *  request that the builder validate.
34  * </p>
35 
36  * @author Elliotte Rusty Harold
37  * @version 1.1b3
38  *
39  */
40 public class ValidityException extends ParsingException {
41 
42 
43     private static final long serialVersionUID = 8950434465665278751L;
44 
45     private List saxExceptions = new ArrayList();
46     private transient Document document;
47 
48 
49     /**
50      * <p>
51      * Creates a new <code>ValidityException</code>
52      * with a detail message and an underlying root cause.
53      * </p>
54      *
55      * @param message a string indicating the specific problem
56      * @param cause the original cause of this exception
57      */
ValidityException(String message, Throwable cause)58     public ValidityException(String message, Throwable cause) {
59         super(message, cause);
60     }
61 
62 
63     /**
64      * <p>
65      * Creates a new <code>ValidityException</code>
66      * with a detail message and line and column numbers.
67      * </p>
68      *
69      * @param message a string indicating the specific problem
70      * @param lineNumber the approximate line number
71      *     where the problem occurs
72      * @param columnNumber the approximate column number
73      *     where the problem occurs
74      */
ValidityException( String message, int lineNumber, int columnNumber)75     public ValidityException(
76         String message,
77         int lineNumber,
78         int columnNumber) {
79         super(message, lineNumber, columnNumber);
80     }
81 
82 
83     /**
84      * <p>
85      * Creates a new <code>ValidityException</code>
86      * with a detail message, line and column numbers,
87      * and an underlying exception.
88      * </p>
89      *
90      * @param message a string indicating the specific problem
91      * @param lineNumber the approximate line number
92      *     where the problem occurs
93      * @param columnNumber the approximate column number
94      *     where the problem occurs
95      * @param cause the original cause of this exception
96      */
ValidityException( String message, int lineNumber, int columnNumber, Throwable cause)97     public ValidityException(
98         String message,
99         int lineNumber,
100         int columnNumber,
101         Throwable cause) {
102         super(message, lineNumber, columnNumber, cause);
103     }
104 
105 
106     /**
107      * <p>
108      * Creates a new <code>ValidityException</code>
109      * with a detail message, the URI of the document that contained
110      * the error, and approximate line and column numbers of the
111      * first validity error.
112      * </p>
113      *
114      * @param message a string indicating the specific problem
115      * @param uri URL of the document with a validity error
116      * @param lineNumber the approximate line number
117      *     where the problem occurs
118      * @param columnNumber the approximate column number
119      *     where the problem occurs
120      */
ValidityException(String message, String uri, int lineNumber, int columnNumber)121     public ValidityException(String message, String uri,
122       int lineNumber, int columnNumber) {
123         super(message, uri, lineNumber, columnNumber);
124     }
125 
126 
127     /**
128      * <p>
129      * Creates a new <code>ValidityException</code>
130      * with a detail message, URI of the document containing the
131      * validity error, line and column numbers of the error,
132      * and an underlying exception.
133      * </p>
134      *
135      * @param message a string indicating the specific problem
136      * @param uri URL of the document with a validity error
137      * @param lineNumber the approximate line number
138      *     where the problem occurs
139      * @param columnNumber the approximate column number
140      *     where the problem occurs
141      * @param cause the original cause of this exception
142      */
ValidityException( String message, String uri, int lineNumber, int columnNumber, Throwable cause)143     public ValidityException(
144         String message,
145         String uri,
146         int lineNumber,
147         int columnNumber,
148         Throwable cause) {
149         super(message, uri, lineNumber, columnNumber, cause);
150     }
151 
152 
153     /**
154      * <p>
155      * Creates a new <code>ValidityException</code>
156      * with a detail message.
157      * </p>
158      *
159      * @param message a string indicating the specific problem
160      */
ValidityException(String message)161     public ValidityException(String message) {
162         super(message);
163     }
164 
165 
166     /**
167      * <p>
168      * Returns a <code>Document</code> object for the document that
169      * caused this exception. This is useful if you want notification
170      * of validity errors, but nonetheless wish to further process
171      * the invalid document.
172      * </p>
173      *
174      * @return the invalid document
175      */
getDocument()176     public Document getDocument() {
177         return document;
178     }
179 
180 
setDocument(Document doc)181     void setDocument(Document doc) {
182         this.document = doc;
183     }
184 
185 
addError(SAXParseException ex)186     void addError(SAXParseException ex) {
187         saxExceptions.add(ex);
188     }
189 
190 
191     /**
192      * <p>
193      *   Returns the number of validity errors the parser detected
194      *   in the document. This is likely to not be consistent from one
195      *   parser to another.
196      * </p>
197      *
198      * @return the number of validity errors the parser detected
199      */
getErrorCount()200     public int getErrorCount() {
201         return saxExceptions.size();
202     }
203 
204 
205     /**
206      * <p>
207      *   Returns a message indicating a specific validity problem
208      *   in the input document as detected by the parser. Normally,
209      *   these will be in the order they appear in the document.
210      *   For instance, an error in the root element is likely
211      *   to appear before an error in a child element. However, this
212      *   depends on the underlying parser and is not guaranteed.
213      * </p>
214      *
215      * @param n the index of the validity error to report
216      *
217      * @return a string describing the n<i>th</i> validity error
218      *
219      * @throws IndexOutOfBoundsException if <code>n</code> is greater
220      *     than or equal to the number of errors detected
221      */
getValidityError(int n)222     public String getValidityError(int n) {
223         Exception ex = (Exception) saxExceptions.get(n);
224         return ex.getMessage();
225     }
226 
227 
228     /**
229      * <p>
230      *   Returns the line number of the <i>n</i>th validity
231      *   error. It returns -1 if this is not known. This number
232      *   may be helpful for debugging, but should not be relied on.
233      *   Different parsers may set it differently. For instance
234      *   a problem with an element might be reported using the
235      *   line number of the start-tag or the line number of the
236      *   end-tag.
237      * </p>
238      *
239      * @param n the index of the validity error to report
240      * @return the approximate line number where the n<i>th</i>
241      *     validity error was detected
242      *
243      * @throws IndexOutOfBoundsException if <code>n</code> is greater
244      *     than or equal to the number of errors detected
245      */
getLineNumber(int n)246     public int getLineNumber(int n) {
247         SAXParseException ex = (SAXParseException) saxExceptions.get(n);
248         return ex.getLineNumber();
249     }
250 
251 
252     /**
253      * <p>
254      *   Returns the column number of the <i>n</i>th validity
255      *   error. It returns -1 if this is not known. This number
256      *   may be helpful for debugging, but should not be relied on.
257      *   Different parsers may set it differently. For instance
258      *   a problem with an element might be reported using the
259      *   column of the <code>&lt;</code> or the <code>&gt;</code>
260      *   of the start-tag
261      * </p>
262      *
263      * @param n the index of the validity error to report
264      *
265      * @return the approximate column where the n<i>th</i>
266      *     validity error was detected
267      *
268      * @throws IndexOutOfBoundsException if <code>n</code> is greater
269      *     than or equal to the number of errors detected
270      */
getColumnNumber(int n)271     public int getColumnNumber(int n) {
272         SAXParseException ex = (SAXParseException) saxExceptions.get(n);
273         return ex.getColumnNumber();
274     }
275 
276 
277 }
278