|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| XIncludeException.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 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@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.xinclude; | |
| 23 | ||
| 24 | /** | |
| 25 | * <p> | |
| 26 | * The generic superclass for all checked exceptions that may be thrown | |
| 27 | * as a result of a violation of XInclude's rules. | |
| 28 | * </p> | |
| 29 | * | |
| 30 | * @author Elliotte Rusty Harold | |
| 31 | * @version 1.1b3 | |
| 32 | */ | |
| 33 | public class XIncludeException extends Exception { | |
| 34 | ||
| 35 | ||
| 36 | private static final long serialVersionUID = 969926723618314277L; | |
| 37 | ||
| 38 | private String uri; | |
| 39 | ||
| 40 | /** | |
| 41 | * <p> | |
| 42 | * Constructs an <code>XIncludeException</code> with the specified | |
| 43 | * detail message. | |
| 44 | * </p> | |
| 45 | * | |
| 46 | * @param message a string indicating the specific problem | |
| 47 | */ | |
| 48 | 21 | public XIncludeException(String message) { |
| 49 | 21 | super(message); |
| 50 | } | |
| 51 | ||
| 52 | ||
| 53 | /** | |
| 54 | * <p> | |
| 55 | * Constructs an <code>XIncludeException</code> with the specified | |
| 56 | * detail message and initial cause. The error message string | |
| 57 | * <code>message</code> can later be retrieved by the | |
| 58 | * <code>{@link java.lang.Throwable#getMessage}</code> | |
| 59 | * method of class <code>java.lang.Throwable</code>. | |
| 60 | * </p> | |
| 61 | * | |
| 62 | * @param message a string indicating the specific problem | |
| 63 | * @param cause the initial cause of the exception | |
| 64 | */ | |
| 65 | 1 | public XIncludeException(String message, Throwable cause) { |
| 66 | 1 | super(message); |
| 67 | 1 | initCause(cause); |
| 68 | } | |
| 69 | ||
| 70 | ||
| 71 | /** | |
| 72 | * <p> | |
| 73 | * Creates a new <code>XIncludeException</code> with a detail | |
| 74 | * message, line and column numbers, and the URI of the document | |
| 75 | * that caused the exception. | |
| 76 | * </p> | |
| 77 | * | |
| 78 | * @param message a string indicating the specific problem | |
| 79 | * @param uri the URI of the document that caused this exception | |
| 80 | */ | |
| 81 | 53 | public XIncludeException(String message, String uri) { |
| 82 | 53 | super(message); |
| 83 | 53 | this.uri = uri; |
| 84 | } | |
| 85 | ||
| 86 | ||
| 87 | /** | |
| 88 | * <p> | |
| 89 | * Returns the URI of the document that caused this exception. | |
| 90 | * If the URI is not known, null is returned. | |
| 91 | * </p> | |
| 92 | * | |
| 93 | * @return URI of the document where the exception occurred | |
| 94 | */ | |
| 95 | 14 | public String getURI() { |
| 96 | 14 | return this.uri; |
| 97 | } | |
| 98 | ||
| 99 | ||
| 100 | private Throwable cause; | |
| 101 | ||
| 102 | ||
| 103 | /** | |
| 104 | * <p> | |
| 105 | * When an <code>IOException</code>, | |
| 106 | * <code>MalformedURLException</code>, or other generic | |
| 107 | * exception is thrown while processing an XML document | |
| 108 | * for XIncludes, it is customarily replaced | |
| 109 | * by some form of <code>XIncludeException</code>. | |
| 110 | * This method allows you to retrieve the original exception. | |
| 111 | * It returns null if no such exception caused this | |
| 112 | * <code>XIncludeException</code>. | |
| 113 | *</p> | |
| 114 | * | |
| 115 | * @return the underlying exception which | |
| 116 | * caused this XIncludeException to be thrown | |
| 117 | */ | |
| 118 | 10 | public Throwable getCause() { |
| 119 | 10 | return this.cause; |
| 120 | } | |
| 121 | ||
| 122 | ||
| 123 | // null is insufficient for detecting an uninitialized cause. | |
| 124 | // The cause may be set to null which may not then be reset. | |
| 125 | private boolean causeSet = false; | |
| 126 | ||
| 127 | ||
| 128 | /** | |
| 129 | * <p> | |
| 130 | * When an <code>IOException</code>, | |
| 131 | * <code>MalformedURLException</code>, or other generic exception | |
| 132 | * is thrown while processing an XML document | |
| 133 | * for XIncludes, it is customarily replaced | |
| 134 | * by some form of <code>XIncludeException</code>. | |
| 135 | * This method allows you to store the original exception. | |
| 136 | * </p> | |
| 137 | * | |
| 138 | * @param cause the root cause of this exception | |
| 139 | * | |
| 140 | * @return this <code>XIncludeException</code> | |
| 141 | * | |
| 142 | * @throws IllegalArgumentException if the cause is this exception | |
| 143 | * (An exception cannot be its own cause.) | |
| 144 | * @throws IllegalStateException if this method is called twice | |
| 145 | */ | |
| 146 | 24 | public Throwable initCause(Throwable cause) { |
| 147 | 24 | if (causeSet) { |
| 148 | 4 | throw new IllegalStateException("Can't overwrite cause"); |
| 149 | } | |
| 150 | 20 | else if (cause == this) { |
| 151 | 1 | throw new IllegalArgumentException("Self-causation not permitted"); |
| 152 | } | |
| 153 | 19 | else this.cause = cause; |
| 154 | 19 | causeSet = true; |
| 155 | 19 | return this; |
| 156 | } | |
| 157 | ||
| 158 | ||
| 159 | } |
|
||||||||||