src/java.xml/share/classes/org/xml/sax/SAXException.java
changeset 48837 a262b919311a
parent 47216 71c04702a3d5
child 58247 3aef3bccfae3
equal deleted inserted replaced
48836:423bcbb288ff 48837:a262b919311a
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    27 // http://www.saxproject.org
    27 // http://www.saxproject.org
    28 // No warranty; no copyright -- use this as you will.
    28 // No warranty; no copyright -- use this as you will.
    29 // $Id: SAXException.java,v 1.3 2004/11/03 22:55:32 jsuttor Exp $
    29 // $Id: SAXException.java,v 1.3 2004/11/03 22:55:32 jsuttor Exp $
    30 
    30 
    31 package org.xml.sax;
    31 package org.xml.sax;
       
    32 
       
    33 import java.io.IOException;
       
    34 import java.io.InvalidClassException;
       
    35 import java.io.ObjectInputStream;
       
    36 import java.io.ObjectOutputStream;
       
    37 import java.io.ObjectStreamField;
    32 
    38 
    33 /**
    39 /**
    34  * Encapsulate a general SAX error or warning.
    40  * Encapsulate a general SAX error or warning.
    35  *
    41  *
    36  * <blockquote>
    42  * <blockquote>
    66      * Create a new SAXException.
    72      * Create a new SAXException.
    67      */
    73      */
    68     public SAXException ()
    74     public SAXException ()
    69     {
    75     {
    70         super();
    76         super();
    71         this.exception = null;
       
    72     }
    77     }
    73 
    78 
    74 
    79 
    75     /**
    80     /**
    76      * Create a new SAXException.
    81      * Create a new SAXException.
    77      *
    82      *
    78      * @param message The error or warning message.
    83      * @param message The error or warning message.
    79      */
    84      */
    80     public SAXException (String message) {
    85     public SAXException (String message) {
    81         super(message);
    86         super(message);
    82         this.exception = null;
       
    83     }
    87     }
    84 
    88 
    85 
    89 
    86     /**
    90     /**
    87      * Create a new SAXException wrapping an existing exception.
    91      * Create a new SAXException wrapping an existing exception.
    92      *
    96      *
    93      * @param e The exception to be wrapped in a SAXException.
    97      * @param e The exception to be wrapped in a SAXException.
    94      */
    98      */
    95     public SAXException (Exception e)
    99     public SAXException (Exception e)
    96     {
   100     {
    97         super();
   101         super(e);
    98         this.exception = e;
       
    99     }
   102     }
   100 
   103 
   101 
   104 
   102     /**
   105     /**
   103      * Create a new SAXException from an existing exception.
   106      * Create a new SAXException from an existing exception.
   108      * @param message The detail message.
   111      * @param message The detail message.
   109      * @param e The exception to be wrapped in a SAXException.
   112      * @param e The exception to be wrapped in a SAXException.
   110      */
   113      */
   111     public SAXException (String message, Exception e)
   114     public SAXException (String message, Exception e)
   112     {
   115     {
   113         super(message);
   116         super(message, e);
   114         this.exception = e;
       
   115     }
   117     }
   116 
   118 
   117 
   119 
   118     /**
   120     /**
   119      * Return a detail message for this exception.
   121      * Return a detail message for this exception.
   125      * @return The error or warning message.
   127      * @return The error or warning message.
   126      */
   128      */
   127     public String getMessage ()
   129     public String getMessage ()
   128     {
   130     {
   129         String message = super.getMessage();
   131         String message = super.getMessage();
   130 
   132         Throwable cause = super.getCause();
   131         if (message == null && exception != null) {
   133 
   132             return exception.getMessage();
   134         if (message == null && cause != null) {
       
   135             return cause.getMessage();
   133         } else {
   136         } else {
   134             return message;
   137             return message;
   135         }
   138         }
   136     }
   139     }
   137 
   140 
   138 
       
   139     /**
   141     /**
   140      * Return the embedded exception, if any.
   142      * Return the embedded exception, if any.
   141      *
   143      *
   142      * @return The embedded exception, or null if there is none.
   144      * @return The embedded exception, or null if there is none.
   143      */
   145      */
   144     public Exception getException ()
   146     public Exception getException ()
   145     {
   147     {
   146         return exception;
   148         return getExceptionInternal();
   147     }
   149     }
   148 
   150 
   149     /**
   151     /**
   150      * Return the cause of the exception
   152      * Return the cause of the exception
   151      *
   153      *
   152      * @return Return the cause of the exception
   154      * @return Return the cause of the exception
   153      */
   155      */
   154     public Throwable getCause() {
   156     public Throwable getCause() {
   155         return exception;
   157         return super.getCause();
   156     }
   158     }
   157 
   159 
   158     /**
   160     /**
   159      * Override toString to pick up any embedded exception.
   161      * Override toString to pick up any embedded exception.
   160      *
   162      *
   161      * @return A string representation of this exception.
   163      * @return A string representation of this exception.
   162      */
   164      */
   163     public String toString ()
   165     public String toString ()
   164     {
   166     {
       
   167         Throwable exception = super.getCause();
   165         if (exception != null) {
   168         if (exception != null) {
   166             return super.toString() + "\n" + exception.toString();
   169             return super.toString() + "\n" + exception.toString();
   167         } else {
   170         } else {
   168             return super.toString();
   171             return super.toString();
   169         }
   172         }
   173 
   176 
   174     //////////////////////////////////////////////////////////////////////
   177     //////////////////////////////////////////////////////////////////////
   175     // Internal state.
   178     // Internal state.
   176     //////////////////////////////////////////////////////////////////////
   179     //////////////////////////////////////////////////////////////////////
   177 
   180 
   178 
   181     private static final ObjectStreamField[] serialPersistentFields = {
   179     /**
   182         new ObjectStreamField( "exception", Exception.class )
   180      * @serial The embedded exception if tunnelling, or null.
   183     };
   181      */
   184 
   182     private Exception exception;
   185     /**
       
   186      * Writes "exception" field to the stream.
       
   187      *
       
   188      * @param out stream used for serialization.
       
   189      * @throws IOException thrown by <code>ObjectOutputStream</code>
       
   190      */
       
   191     private void writeObject(ObjectOutputStream out)
       
   192             throws IOException {
       
   193         ObjectOutputStream.PutField fields = out.putFields();
       
   194         fields.put("exception", getExceptionInternal());
       
   195         out.writeFields();
       
   196     }
       
   197 
       
   198     /**
       
   199      * Reads the "exception" field from the stream.
       
   200      * And initializes the "exception" if it wasn't
       
   201      * done before.
       
   202      *
       
   203      * @param in stream used for deserialization
       
   204      * @throws IOException            thrown by <code>ObjectInputStream</code>
       
   205      * @throws ClassNotFoundException thrown by <code>ObjectInputStream</code>
       
   206      */
       
   207     private void readObject(ObjectInputStream in)
       
   208             throws IOException, ClassNotFoundException {
       
   209         ObjectInputStream.GetField fields = in.readFields();
       
   210         Exception exception = (Exception) fields.get("exception", null);
       
   211         Throwable superCause = super.getCause();
       
   212 
       
   213         // if super.getCause() and 'exception' fields present then always use
       
   214         // getCause() value. Otherwise, use 'exception' to initialize cause
       
   215         if (superCause == null && exception != null) {
       
   216             try {
       
   217                 super.initCause(exception);
       
   218             } catch (IllegalStateException e) {
       
   219                 throw new InvalidClassException("Inconsistent state: two causes");
       
   220             }
       
   221         }
       
   222     }
       
   223 
       
   224     // Internal method to guard against overriding of public getException
       
   225     // method by SAXException subclasses
       
   226     private Exception getExceptionInternal() {
       
   227         Throwable cause = super.getCause();
       
   228         if (cause instanceof Exception) {
       
   229             return (Exception) cause;
       
   230         } else {
       
   231             return null;
       
   232         }
       
   233     }
   183 
   234 
   184     // Added serialVersionUID to preserve binary compatibility
   235     // Added serialVersionUID to preserve binary compatibility
   185     static final long serialVersionUID = 583241635256073760L;
   236     static final long serialVersionUID = 583241635256073760L;
   186 }
   237 }
   187 
   238