001: /* Copyright 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: package nu.xom;
022:
023: /**
024: * <p>
025: * Indicates problems with XPath syntax or evaluation.
026: * </p>
027: *
028: * @author Elliotte Rusty Harold
029: * @version 1.1b3
030: *
031: */
032: public class XPathException extends RuntimeException {
033:
034: private static final long serialVersionUID = 6362087755031657439L;
035:
036: private String expression;
037: private Throwable cause;
038:
039: /**
040: * <p>
041: * Creates a new <code>XPathException</code>
042: * with a detail message.
043: * </p>
044: *
045: * @param message a string indicating the specific problem
046: */
047: public XPathException(String message) {
048: super (message);
049: }
050:
051: /**
052: * <p>
053: * Creates a new <code>IllegalNameException</code>
054: * with a detail message and an underlying root cause.
055: * </p>
056: *
057: * @param message a string indicating the specific problem
058: * @param cause the original cause of this exception
059: */
060: public XPathException(String message, Throwable cause) {
061: super (message);
062: this .initCause(cause);
063: }
064:
065: /**
066: * <p>
067: * Return the original cause that led to this exception,
068: * or null if there was no original exception.
069: * </p>
070: *
071: * @return the root cause of this exception
072: */
073: public Throwable getCause() {
074: return this .cause;
075: }
076:
077: // null is insufficient for detecting an uninitialized cause.
078: // The cause may be set to null which may not then be reset.
079: private boolean causeSet = false;
080:
081: /**
082: * <p>
083: * Sets the root cause of this exception. This may
084: * only be called once. Subsequent calls throw an
085: * <code>IllegalStateException</code>.
086: * </p>
087: *
088: * <p>
089: * This method is unnecessary in Java 1.4 where it could easily be
090: * inherited from the superclass. However, including it here
091: * allows this method to be used in Java 1.3 and earlier.
092: * </p>
093: *
094: * @param cause the root cause of this exception
095: *
096: * @return this <code>XMLException</code>
097: *
098: * @throws IllegalArgumentException if the cause is this exception
099: * (An exception cannot be its own cause.)
100: * @throws IllegalStateException if this method is called twice
101: */
102: public Throwable initCause(Throwable cause) {
103:
104: if (causeSet) {
105: throw new IllegalStateException("Can't overwrite cause");
106: } else if (cause == this ) {
107: throw new IllegalArgumentException(
108: "Self-causation not permitted");
109: } else
110: this .cause = cause;
111: causeSet = true;
112: return this ;
113:
114: }
115:
116: /**
117: * <p>
118: * Sets the specific XPath expression that caused this exception.
119: * </p>
120: *
121: * @param expression the XPath expression that caused the exception
122: */
123: void setXPath(String expression) {
124: this .expression = expression;
125: }
126:
127: /**
128: * <p>
129: * Returns the specific XPath expression being evaluated when this
130: * excepiton was thrown.
131: * </p>
132: *
133: * @return the XPath expression that caused the exception
134: */
135: public String getXPath() {
136: return this.expression;
137: }
138:
139: }
|