1 /* 2 * $Header$ 3 * $Revision$ 4 * $Date$ 5 * 6 * ==================================================================== 7 * 8 * Copyright 2000-2002 bob mcwhirter & James Strachan. 9 * All rights reserved. 10 * 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions are 14 * met: 15 * 16 * * Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * * Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * * Neither the name of the Jaxen Project nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 30 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 31 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39 * ==================================================================== 40 * This software consists of voluntary contributions made by many 41 * individuals on behalf of the Jaxen Project and was originally 42 * created by bob mcwhirter <bob@werken.com> and 43 * James Strachan <jstrachan@apache.org>. For more information on the 44 * Jaxen Project, please see <http://www.jaxen.org/>. 45 * 46 * $Id$ 47 */ 48 49 package org.jaxen.saxpath; 50 51 import java.io.PrintStream; 52 import java.io.PrintWriter; 53 54 /** Base of all SAXPath exceptions. 55 * 56 * @author bob mcwhirter (bob@werken.com) 57 */ 58 public class SAXPathException extends Exception 59 { 60 61 /** 62 * 63 */ 64 private static final long serialVersionUID = 4826444568928720706L; 65 66 private static double javaVersion = 1.4; 67 68 static { 69 try { 70 String versionString = System.getProperty("java.version"); 71 versionString = versionString.substring(0, 3); 72 javaVersion = Double.valueOf(versionString).doubleValue(); 73 } 74 catch (Exception ex) { 75 // The version string format changed so presumably it's 76 // 1.4 or later. 77 } 78 } 79 80 /** Create a new SAXPathException with a given message. 81 * 82 * @param message the error message 83 */ 84 public SAXPathException(String message) 85 { 86 super( message ); 87 } 88 89 /** Create a new SAXPathException based on another exception 90 * 91 * @param cause the error source 92 */ 93 public SAXPathException(Throwable cause) 94 { 95 super ( cause.getMessage() ); 96 initCause(cause); 97 } 98 99 /** 100 * Create a new SAXPathException with the specified detail message 101 * and root cause. 102 * 103 * @param message the detail message 104 * @param cause the cause of this exception 105 */ 106 public SAXPathException(String message, Throwable cause) { 107 super( message ); 108 initCause(cause); 109 } 110 111 112 private Throwable cause; 113 private boolean causeSet = false; 114 115 /** 116 * Returns the exception that caused this exception. 117 * This is necessary to implement Java 1.4 chained exception 118 * functionality in a Java 1.3-compatible way. 119 * 120 * @return the exception that caused this exception 121 */ 122 public Throwable getCause() { 123 return cause; 124 } 125 126 127 /** 128 * Sets the exception that caused this exception. 129 * This is necessary to implement Java 1.4 chained exception 130 * functionality in a Java 1.3-compatible way. 131 * 132 * @param cause the exception wrapped in this runtime exception 133 * 134 * @return this exception 135 */ 136 public Throwable initCause(Throwable cause) { 137 if (causeSet) throw new IllegalStateException("Cause cannot be reset"); 138 if (cause == this) throw new IllegalArgumentException("Exception cannot be its own cause"); 139 causeSet = true; 140 this.cause = cause; 141 return this; 142 } 143 144 /** Print this exception's stack trace, followed by the 145 * source exception's trace, if any. 146 * 147 * @param s the stream on which to print the stack trace 148 */ 149 public void printStackTrace ( PrintStream s ) 150 { 151 super.printStackTrace ( s ); 152 if (javaVersion < 1.4 && getCause() != null) { 153 s.print( "Caused by: " ); 154 getCause().printStackTrace( s ); 155 } 156 } 157 158 /** Print this exception's stack trace, followed by the 159 * source exception's stack trace, if any. 160 * 161 * @param s the writer on which to print the stack trace 162 */ 163 public void printStackTrace ( PrintWriter s ) 164 { 165 super.printStackTrace( s ); 166 if (javaVersion < 1.4 && getCause() != null) { 167 s.print( "Caused by: " ); 168 getCause().printStackTrace( s ); 169 } 170 } 171 172 }