KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > ValidityException


1 /* Copyright 2002-2004 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@metalab.unc.edu. 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 JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.xml.sax.SAXParseException JavaDoc;
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.0
38  *
39  */

40 public class ValidityException extends ParsingException {
41     
42     private List JavaDoc saxExceptions = new ArrayList JavaDoc();
43     private transient Document document;
44     
45
46     /**
47      * <p>
48      * Creates a new <code>ValidityException</code>
49      * with a detail message and an underlying root cause.
50      * </p>
51      *
52      * @param message a string indicating the specific problem
53      * @param cause the original cause of this exception
54      */

55     public ValidityException(String JavaDoc message, Throwable JavaDoc cause) {
56         super(message, cause);
57     }
58
59     
60     /**
61      * <p>
62      * Creates a new <code>ValidityException</code>
63      * with a detail message and line and column numbers.
64      * </p>
65      *
66      * @param message a string indicating the specific problem
67      * @param lineNumber the approximate line number
68      * where the problem occurs
69      * @param columnNumber the approximate column number
70      * where the problem occurs
71      */

72     public ValidityException(
73         String JavaDoc message,
74         int lineNumber,
75         int columnNumber) {
76         super(message, lineNumber, columnNumber);
77     }
78
79     
80     /**
81      * <p>
82      * Creates a new <code>ValidityException</code>
83      * with a detail message, line and column numbers,
84      * and an underlying exception.
85      * </p>
86      *
87      * @param message a string indicating the specific problem
88      * @param lineNumber the approximate line number
89      * where the problem occurs
90      * @param columnNumber the approximate column number
91      * where the problem occurs
92      * @param cause the original cause of this exception
93      */

94     public ValidityException(
95         String JavaDoc message,
96         int lineNumber,
97         int columnNumber,
98         Throwable JavaDoc cause) {
99         super(message, lineNumber, columnNumber, cause);
100     }
101
102     
103     /**
104      * <p>
105      * Creates a new <code>ValidityException</code>
106      * with a detail message, the URI of the document that contained
107      * the error, and approximate line and column numbers of the
108      * first validity error.
109      * </p>
110      *
111      * @param message a string indicating the specific problem
112      * @param lineNumber the approximate line number
113      * where the problem occurs
114      * @param columnNumber the approximate column number
115      * where the problem occurs
116      */

117     public ValidityException(String JavaDoc message, String JavaDoc uri,
118       int lineNumber, int columnNumber) {
119         super(message, uri, lineNumber, columnNumber);
120     }
121
122     
123     /**
124      * <p>
125      * Creates a new <code>ValidityException</code>
126      * with a detail message, URI of the document containing the
127      * validity error, line and column numbers of the error,
128      * and an underlying exception.
129      * </p>
130      *
131      * @param message a string indicating the specific problem
132      * @param lineNumber the approximate line number
133      * where the problem occurs
134      * @param columnNumber the approximate column number
135      * where the problem occurs
136      * @param cause the original cause of this exception
137      */

138     public ValidityException(
139         String JavaDoc message,
140         String JavaDoc uri,
141         int lineNumber,
142         int columnNumber,
143         Throwable JavaDoc cause) {
144         super(message, uri, lineNumber, columnNumber, cause);
145     }
146     
147     
148     /**
149      * <p>
150      * Creates a new <code>ValidityException</code>
151      * with a detail message.
152      * </p>
153      *
154      * @param message a string indicating the specific problem
155      */

156     public ValidityException(String JavaDoc message) {
157         super(message);
158     }
159
160     
161     /**
162      * <p>
163      * Returns a <code>Document</code> object for the document that
164      * caused this exception. This is useful if you want notification
165      * of validity errors, but nonetheless wish to further process
166      * the invalid document.
167      * </p>
168      *
169      * @return the invalid document
170      */

171     public Document getDocument() {
172         return document;
173     }
174     
175     
176     void setDocument(Document doc) {
177         this.document = doc;
178     }
179     
180     
181     void addError(SAXParseException JavaDoc ex) {
182         saxExceptions.add(ex);
183     }
184     
185     
186     /**
187      * <p>
188      * Returns the number of validity errors the parser detected
189      * in the document. This is likely to not be consistent from one
190      * parser to another.
191      * </p>
192      *
193      * @return the number of validity errors the parser detected
194      */

195     public int getErrorCount() {
196         return saxExceptions.size();
197     }
198     
199     
200     /**
201      * <p>
202      * Returns a message indicating a specific validity problem
203      * in the input document as detected by the parser. Normally,
204      * these will be in the order they appear in the document.
205      * For instance, an error in the root element is likely
206      * to appear before an error in a child element. However, this
207      * depends on the underlying parser and is not guaranteed.
208      * </p>
209      *
210      * @param n the index of the validity error to report
211      *
212      * @return a string describing the n<i>th</i> validity error
213      *
214      * @throws IndexOutOfBoundsException if <code>n</code> is greater
215      * than or equal to the number of errors detected
216      */

217     public String JavaDoc getValidityError(int n) {
218         Exception JavaDoc ex = (Exception JavaDoc) saxExceptions.get(n);
219         return ex.getMessage();
220     }
221
222     
223     /**
224      * <p>
225      * Returns the line number of the <i>n</i>th validity
226      * error. It returns -1 if this is not known. This number
227      * may be helpful for debugging, but should not be relied on.
228      * Different parsers may set it differently. For instance
229      * a problem with an element might be reported using the
230      * line number of the start-tag or the line number of the
231      * end-tag.
232      * </p>
233      *
234      * @param n the index of the validity error to report
235      * @return the approximate line number where the n<i>th</i>
236      * validity error was detected
237      *
238      * @throws IndexOutOfBoundsException if <code>n</code> is greater
239      * than or equal to the number of errors detected
240      */

241     public int getLineNumber(int n) {
242         SAXParseException JavaDoc ex = (SAXParseException JavaDoc) saxExceptions.get(n);
243         return ex.getLineNumber();
244     }
245
246     
247     /**
248      * <p>
249      * Returns the column number of the <i>n</i>th validity
250      * error. It returns -1 if this is not known. This number
251      * may be helpful for debugging, but should not be relied on.
252      * Different parsers may set it differently. For instance
253      * a problem with an element might be reported using the
254      * column of the <code>&lt;</code> or the <code>&gt;</code>
255      * of the start-tag
256      * </p>
257      *
258      * @param n the index of the validity error to report
259      *
260      * @return the approximate column where the n<i>th</i>
261      * validity error was detected
262      *
263      * @throws IndexOutOfBoundsException if <code>n</code> is greater
264      * than or equal to the number of errors detected
265      */

266     public int getColumnNumber(int n) {
267         SAXParseException JavaDoc ex = (SAXParseException JavaDoc) saxExceptions.get(n);
268         return ex.getColumnNumber();
269     }
270
271     
272 }
273
Popular Tags