1 /*
   2  * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.xpath;
  27 
  28 import java.io.PrintWriter;
  29 import java.io.IOException;
  30 import java.io.ObjectInputStream;
  31 import java.io.ObjectOutputStream;
  32 import java.io.ObjectStreamField;
  33 
  34 /**
  35  * <code>XPathException</code> represents a generic XPath exception.</p>
  36  *
  37  * @author  <a href="Norman.Walsh@Sun.com">Norman Walsh</a>
  38  * @author <a href="mailto:Jeff.Suttor@Sun.COM">Jeff Suttor</a>
  39  * @since 1.5
  40  */
  41 public class XPathException extends Exception {
  42     
  43     private static final ObjectStreamField[] serialPersistentFields = {
  44         new ObjectStreamField( "cause", Throwable.class )
  45     };
  46     
  47     /**
  48      * <p>Stream Unique Identifier.</p>
  49      */
  50     private static final long serialVersionUID = -1837080260374986980L;
  51 
  52     /**
  53      * <p>Constructs a new <code>XPathException</code>
  54      * with the specified detail <code>message</code>.</p>
  55      *
  56      * <p>The <code>cause</code> is not initialized.</p>
  57      *
  58      * <p>If <code>message</code> is <code>null</code>,
  59      * then a <code>NullPointerException</code> is thrown.</p>
  60      *
  61      * @param message The detail message.
  62      *
  63      * @throws NullPointerException When <code>message</code> is
  64      *   <code>null</code>.
  65      */
  66     public XPathException(String message) {
  67         super(message);
  68         if ( message == null ) {
  69             throw new NullPointerException ( "message can't be null");
  70         }
  71     }
  72     
  73     /**
  74      * <p>Constructs a new <code>XPathException</code>
  75      * with the specified <code>cause</code>.</p>
  76      *
  77      * <p>If <code>cause</code> is <code>null</code>,
  78      * then a <code>NullPointerException</code> is thrown.</p>
  79      *
  80      * @param cause The cause.
  81      *
  82      * @throws NullPointerException if <code>cause</code> is <code>null</code>.
  83      */
  84     public XPathException(Throwable cause) {
  85         super(cause);
  86         if ( cause == null ) {
  87             throw new NullPointerException ( "cause can't be null");
  88         }
  89     }
  90 
  91     /**
  92      * <p>Get the cause of this XPathException.</p>
  93      *
  94      * @return Cause of this XPathException.
  95      */
  96     public Throwable getCause() {
  97         return super.getCause();
  98     }
  99     
 100     /**
 101      * Writes "cause" field to the stream. 
 102      * The cause is got from the parent class.
 103      * 
 104      * @param out stream used for serialization.
 105      * @throws IOException thrown by <code>ObjectOutputStream</code>
 106      * 
 107      */
 108     private void writeObject(ObjectOutputStream out) 
 109             throws IOException 
 110     {
 111         ObjectOutputStream.PutField fields = out.putFields();
 112         fields.put("cause", (Throwable) super.getCause());
 113         out.writeFields();
 114     }
 115     
 116     /**
 117      * Reads the "cause" field from the stream.
 118      * And initializes the "cause" if it wasn't 
 119      * done before.
 120      * 
 121      * @param in stream used for deserialization
 122      * @throws IOException thrown by <code>ObjectInputStream</code>
 123      * @throws ClassNotFoundException  thrown by <code>ObjectInputStream</code>
 124      */
 125     private void readObject(ObjectInputStream in) 
 126             throws IOException, ClassNotFoundException 
 127     {
 128         ObjectInputStream.GetField fields = in.readFields();
 129         Throwable scause = (Throwable) fields.get("cause", null);
 130         if (super.getCause() == null && scause != null)
 131             super.initCause(scause);
 132     }    
 133 
 134     /**
 135      * <p>Print stack trace to specified <code>PrintStream</code>.</p>
 136      *
 137      * @param s Print stack trace to this <code>PrintStream</code>.
 138      */
 139     public void printStackTrace(java.io.PrintStream s) {
 140         if (getCause() != null) {
 141             getCause().printStackTrace(s);
 142           s.println("--------------- linked to ------------------");
 143         }
 144 
 145         super.printStackTrace(s);
 146     }
 147 
 148     /**
 149      * <p>Print stack trace to <code>System.err</code>.</p>
 150      */
 151     public void printStackTrace() {
 152         printStackTrace(System.err);
 153     }
 154 
 155     /**
 156      * <p>Print stack trace to specified <code>PrintWriter</code>.</p>
 157      *
 158      * @param s Print stack trace to this <code>PrintWriter</code>.
 159      */
 160     public void printStackTrace(PrintWriter s) {
 161 
 162         if (getCause() != null) {
 163             getCause().printStackTrace(s);
 164           s.println("--------------- linked to ------------------");
 165         }
 166 
 167         super.printStackTrace(s);
 168     }
 169 }