001: /* Copyright 2002-2005 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package nu.xom;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: import org.xml.sax.SAXParseException;
028:
029: /**
030: * <p>
031: * Signals a validity error in a document being parsed.
032: * These are not thrown by default, unless you specifically
033: * request that the builder validate.
034: * </p>
035:
036: * @author Elliotte Rusty Harold
037: * @version 1.1b3
038: *
039: */
040: public class ValidityException extends ParsingException {
041:
042: private static final long serialVersionUID = 8950434465665278751L;
043:
044: private List saxExceptions = new ArrayList();
045: private transient Document document;
046:
047: /**
048: * <p>
049: * Creates a new <code>ValidityException</code>
050: * with a detail message and an underlying root cause.
051: * </p>
052: *
053: * @param message a string indicating the specific problem
054: * @param cause the original cause of this exception
055: */
056: public ValidityException(String message, Throwable cause) {
057: super (message, cause);
058: }
059:
060: /**
061: * <p>
062: * Creates a new <code>ValidityException</code>
063: * with a detail message and line and column numbers.
064: * </p>
065: *
066: * @param message a string indicating the specific problem
067: * @param lineNumber the approximate line number
068: * where the problem occurs
069: * @param columnNumber the approximate column number
070: * where the problem occurs
071: */
072: public ValidityException(String message, int lineNumber,
073: int columnNumber) {
074: super (message, lineNumber, columnNumber);
075: }
076:
077: /**
078: * <p>
079: * Creates a new <code>ValidityException</code>
080: * with a detail message, line and column numbers,
081: * and an underlying exception.
082: * </p>
083: *
084: * @param message a string indicating the specific problem
085: * @param lineNumber the approximate line number
086: * where the problem occurs
087: * @param columnNumber the approximate column number
088: * where the problem occurs
089: * @param cause the original cause of this exception
090: */
091: public ValidityException(String message, int lineNumber,
092: int columnNumber, Throwable cause) {
093: super (message, lineNumber, columnNumber, cause);
094: }
095:
096: /**
097: * <p>
098: * Creates a new <code>ValidityException</code>
099: * with a detail message, the URI of the document that contained
100: * the error, and approximate line and column numbers of the
101: * first validity error.
102: * </p>
103: *
104: * @param message a string indicating the specific problem
105: * @param lineNumber the approximate line number
106: * where the problem occurs
107: * @param columnNumber the approximate column number
108: * where the problem occurs
109: */
110: public ValidityException(String message, String uri,
111: int lineNumber, int columnNumber) {
112: super (message, uri, lineNumber, columnNumber);
113: }
114:
115: /**
116: * <p>
117: * Creates a new <code>ValidityException</code>
118: * with a detail message, URI of the document containing the
119: * validity error, line and column numbers of the error,
120: * and an underlying exception.
121: * </p>
122: *
123: * @param message a string indicating the specific problem
124: * @param lineNumber the approximate line number
125: * where the problem occurs
126: * @param columnNumber the approximate column number
127: * where the problem occurs
128: * @param cause the original cause of this exception
129: */
130: public ValidityException(String message, String uri,
131: int lineNumber, int columnNumber, Throwable cause) {
132: super (message, uri, lineNumber, columnNumber, cause);
133: }
134:
135: /**
136: * <p>
137: * Creates a new <code>ValidityException</code>
138: * with a detail message.
139: * </p>
140: *
141: * @param message a string indicating the specific problem
142: */
143: public ValidityException(String message) {
144: super (message);
145: }
146:
147: /**
148: * <p>
149: * Returns a <code>Document</code> object for the document that
150: * caused this exception. This is useful if you want notification
151: * of validity errors, but nonetheless wish to further process
152: * the invalid document.
153: * </p>
154: *
155: * @return the invalid document
156: */
157: public Document getDocument() {
158: return document;
159: }
160:
161: void setDocument(Document doc) {
162: this .document = doc;
163: }
164:
165: void addError(SAXParseException ex) {
166: saxExceptions.add(ex);
167: }
168:
169: /**
170: * <p>
171: * Returns the number of validity errors the parser detected
172: * in the document. This is likely to not be consistent from one
173: * parser to another.
174: * </p>
175: *
176: * @return the number of validity errors the parser detected
177: */
178: public int getErrorCount() {
179: return saxExceptions.size();
180: }
181:
182: /**
183: * <p>
184: * Returns a message indicating a specific validity problem
185: * in the input document as detected by the parser. Normally,
186: * these will be in the order they appear in the document.
187: * For instance, an error in the root element is likely
188: * to appear before an error in a child element. However, this
189: * depends on the underlying parser and is not guaranteed.
190: * </p>
191: *
192: * @param n the index of the validity error to report
193: *
194: * @return a string describing the n<i>th</i> validity error
195: *
196: * @throws IndexOutOfBoundsException if <code>n</code> is greater
197: * than or equal to the number of errors detected
198: */
199: public String getValidityError(int n) {
200: Exception ex = (Exception) saxExceptions.get(n);
201: return ex.getMessage();
202: }
203:
204: /**
205: * <p>
206: * Returns the line number of the <i>n</i>th validity
207: * error. It returns -1 if this is not known. This number
208: * may be helpful for debugging, but should not be relied on.
209: * Different parsers may set it differently. For instance
210: * a problem with an element might be reported using the
211: * line number of the start-tag or the line number of the
212: * end-tag.
213: * </p>
214: *
215: * @param n the index of the validity error to report
216: * @return the approximate line number where the n<i>th</i>
217: * validity error was detected
218: *
219: * @throws IndexOutOfBoundsException if <code>n</code> is greater
220: * than or equal to the number of errors detected
221: */
222: public int getLineNumber(int n) {
223: SAXParseException ex = (SAXParseException) saxExceptions.get(n);
224: return ex.getLineNumber();
225: }
226:
227: /**
228: * <p>
229: * Returns the column number of the <i>n</i>th validity
230: * error. It returns -1 if this is not known. This number
231: * may be helpful for debugging, but should not be relied on.
232: * Different parsers may set it differently. For instance
233: * a problem with an element might be reported using the
234: * column of the <code><</code> or the <code>></code>
235: * of the start-tag
236: * </p>
237: *
238: * @param n the index of the validity error to report
239: *
240: * @return the approximate column 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: */
246: public int getColumnNumber(int n) {
247: SAXParseException ex = (SAXParseException) saxExceptions.get(n);
248: return ex.getColumnNumber();
249: }
250:
251: }
|