1 /* Copyright 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 package nu.xom;
22 
23 /**
24  * <p>
25  * Indicates problems with XPath syntax or evaluation.
26  * </p>
27  *
28  * @author Elliotte Rusty Harold
29  * @version 1.1b3
30  *
31  */
32 public class XPathException extends RuntimeException {
33 
34 
35     private static final long serialVersionUID = 6362087755031657439L;
36 
37     private String expression;
38     private Throwable cause;
39 
40 
41     /**
42      * <p>
43      * Creates a new <code>XPathException</code>
44      * with a detail message.
45      * </p>
46      *
47      * @param message a string indicating the specific problem
48      */
XPathException(String message)49     public XPathException(String message) {
50         super(message);
51     }
52 
53 
54     /**
55      * <p>
56      * Creates a new <code>IllegalNameException</code>
57      * with a detail message and an underlying root cause.
58      * </p>
59      *
60      * @param message a string indicating the specific problem
61      * @param cause the original cause of this exception
62      */
XPathException(String message, Throwable cause)63     public XPathException(String message, Throwable cause) {
64         super(message);
65         this.initCause(cause);
66     }
67 
68 
69     /**
70      * <p>
71      *  Return the original cause that led to this exception,
72      *  or null if there was no original exception.
73      * </p>
74      *
75      * @return the root cause of this exception
76      */
getCause()77     public Throwable getCause() {
78         return this.cause;
79     }
80 
81 
82     // null is insufficient for detecting an uninitialized cause.
83     // The cause may be set to null which may not then be reset.
84     private boolean causeSet = false;
85 
86 
87     /**
88      * <p>
89      * Sets the root cause of this exception. This may
90      * only be called once. Subsequent calls throw an
91      * <code>IllegalStateException</code>.
92      * </p>
93      *
94      * <p>
95      * This method is unnecessary in Java 1.4 where it could easily be
96      * inherited from the superclass. However, including it here
97      * allows this  method to be used in Java 1.3 and earlier.
98      * </p>
99      *
100      * @param cause the root cause of this exception
101      *
102      * @return this <code>XMLException</code>
103      *
104      * @throws IllegalArgumentException if the cause is this exception
105      *   (An exception cannot be its own cause.)
106      * @throws IllegalStateException if this method is called twice
107      */
initCause(Throwable cause)108     public Throwable initCause(Throwable cause) {
109 
110         if (causeSet) {
111             throw new IllegalStateException("Can't overwrite cause");
112         }
113         else if (cause == this) {
114             throw new IllegalArgumentException("Self-causation not permitted");
115         }
116         else this.cause = cause;
117         causeSet = true;
118         return this;
119 
120     }
121 
122 
123     /**
124      * <p>
125      * Sets the specific XPath expression that caused this exception.
126      * </p>
127      *
128      * @param expression the XPath expression that caused the exception
129      */
setXPath(String expression)130     void setXPath(String expression) {
131         this.expression = expression;
132     }
133 
134 
135     /**
136      * <p>
137      * Returns the specific XPath expression being evaluated when this
138      * excepiton was thrown.
139      * </p>
140      *
141      * @return the XPath expression that caused the exception
142      */
getXPath()143     public String getXPath() {
144         return this.expression;
145     }
146 
147 
148 }
149