1 /*
   2  * Copyright (c) 2000, 2019, 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 org.xml.sax;
  27 
  28 /**
  29  * Encapsulate an XML parse error or warning.
  30  *
  31  * <p>This exception may include information for locating the error
  32  * in the original XML document, as if it came from a {@link Locator}
  33  * object.  Note that although the application
  34  * will receive a SAXParseException as the argument to the handlers
  35  * in the {@link org.xml.sax.ErrorHandler ErrorHandler} interface,
  36  * the application is not actually required to throw the exception;
  37  * instead, it can simply read the information in it and take a
  38  * different action.</p>
  39  *
  40  * <p>Since this exception is a subclass of {@link org.xml.sax.SAXException
  41  * SAXException}, it inherits the ability to wrap another exception.</p>
  42  *
  43  * @since 1.4, SAX 1.0
  44  * @author David Megginson
  45  * @version 2.0.1 (sax2r2)
  46  * @see org.xml.sax.SAXException
  47  * @see org.xml.sax.Locator
  48  * @see org.xml.sax.ErrorHandler
  49  */
  50 public class SAXParseException extends SAXException {
  51 
  52 
  53     //////////////////////////////////////////////////////////////////////
  54     // Constructors.
  55     //////////////////////////////////////////////////////////////////////
  56 
  57 
  58     /**
  59      * Create a new SAXParseException from a message and a Locator.
  60      *
  61      * <p>This constructor is especially useful when an application is
  62      * creating its own exception from within a {@link org.xml.sax.ContentHandler
  63      * ContentHandler} callback.</p>
  64      *
  65      * @param message The error or warning message.
  66      * @param locator The locator object for the error or warning (may be
  67      *        null).
  68      * @see org.xml.sax.Locator
  69      */
  70     public SAXParseException (String message, Locator locator) {
  71         super(message);
  72         if (locator != null) {
  73             init(locator.getPublicId(), locator.getSystemId(),
  74                  locator.getLineNumber(), locator.getColumnNumber());
  75         } else {
  76             init(null, null, -1, -1);
  77         }
  78     }
  79 
  80 
  81     /**
  82      * Wrap an existing exception in a SAXParseException.
  83      *
  84      * <p>This constructor is especially useful when an application is
  85      * creating its own exception from within a {@link org.xml.sax.ContentHandler
  86      * ContentHandler} callback, and needs to wrap an existing exception that is not a
  87      * subclass of {@link org.xml.sax.SAXException SAXException}.</p>
  88      *
  89      * @param message The error or warning message, or null to
  90      *                use the message from the embedded exception.
  91      * @param locator The locator object for the error or warning (may be
  92      *        null).
  93      * @param e Any exception.
  94      * @see org.xml.sax.Locator
  95      */
  96     public SAXParseException (String message, Locator locator,
  97                               Exception e) {
  98         super(message, e);
  99         if (locator != null) {
 100             init(locator.getPublicId(), locator.getSystemId(),
 101                  locator.getLineNumber(), locator.getColumnNumber());
 102         } else {
 103             init(null, null, -1, -1);
 104         }
 105     }
 106 
 107 
 108     /**
 109      * Create a new SAXParseException.
 110      *
 111      * <p>This constructor is most useful for parser writers.</p>
 112      *
 113      * <p>All parameters except the message are as if
 114      * they were provided by a {@link Locator}.  For example, if the
 115      * system identifier is a URL (including relative filename), the
 116      * caller must resolve it fully before creating the exception.</p>
 117      *
 118      *
 119      * @param message The error or warning message.
 120      * @param publicId The public identifier of the entity that generated
 121      *                 the error or warning.
 122      * @param systemId The system identifier of the entity that generated
 123      *                 the error or warning.
 124      * @param lineNumber The line number of the end of the text that
 125      *                   caused the error or warning.
 126      * @param columnNumber The column number of the end of the text that
 127      *                     cause the error or warning.
 128      */
 129     public SAXParseException (String message, String publicId, String systemId,
 130                               int lineNumber, int columnNumber)
 131     {
 132         super(message);
 133         init(publicId, systemId, lineNumber, columnNumber);
 134     }
 135 
 136 
 137     /**
 138      * Create a new SAXParseException with an embedded exception.
 139      *
 140      * <p>This constructor is most useful for parser writers who
 141      * need to wrap an exception that is not a subclass of
 142      * {@link org.xml.sax.SAXException SAXException}.</p>
 143      *
 144      * <p>All parameters except the message and exception are as if
 145      * they were provided by a {@link Locator}.  For example, if the
 146      * system identifier is a URL (including relative filename), the
 147      * caller must resolve it fully before creating the exception.</p>
 148      *
 149      * @param message The error or warning message, or null to use
 150      *                the message from the embedded exception.
 151      * @param publicId The public identifier of the entity that generated
 152      *                 the error or warning.
 153      * @param systemId The system identifier of the entity that generated
 154      *                 the error or warning.
 155      * @param lineNumber The line number of the end of the text that
 156      *                   caused the error or warning.
 157      * @param columnNumber The column number of the end of the text that
 158      *                     cause the error or warning.
 159      * @param e Another exception to embed in this one.
 160      */
 161     public SAXParseException (String message, String publicId, String systemId,
 162                               int lineNumber, int columnNumber, Exception e)
 163     {
 164         super(message, e);
 165         init(publicId, systemId, lineNumber, columnNumber);
 166     }
 167 
 168 
 169     /**
 170      * Internal initialization method.
 171      *
 172      * @param publicId The public identifier of the entity which generated the exception,
 173      *        or null.
 174      * @param systemId The system identifier of the entity which generated the exception,
 175      *        or null.
 176      * @param lineNumber The line number of the error, or -1.
 177      * @param columnNumber The column number of the error, or -1.
 178      */
 179     private void init (String publicId, String systemId,
 180                        int lineNumber, int columnNumber)
 181     {
 182         this.publicId = publicId;
 183         this.systemId = systemId;
 184         this.lineNumber = lineNumber;
 185         this.columnNumber = columnNumber;
 186     }
 187 
 188 
 189     /**
 190      * Get the public identifier of the entity where the exception occurred.
 191      *
 192      * @return A string containing the public identifier, or null
 193      *         if none is available.
 194      * @see org.xml.sax.Locator#getPublicId
 195      */
 196     public String getPublicId ()
 197     {
 198         return this.publicId;
 199     }
 200 
 201 
 202     /**
 203      * Get the system identifier of the entity where the exception occurred.
 204      *
 205      * <p>If the system identifier is a URL, it will have been resolved
 206      * fully.</p>
 207      *
 208      * @return A string containing the system identifier, or null
 209      *         if none is available.
 210      * @see org.xml.sax.Locator#getSystemId
 211      */
 212     public String getSystemId ()
 213     {
 214         return this.systemId;
 215     }
 216 
 217 
 218     /**
 219      * The line number of the end of the text where the exception occurred.
 220      *
 221      * <p>The first line is line 1.</p>
 222      *
 223      * @return An integer representing the line number, or -1
 224      *         if none is available.
 225      * @see org.xml.sax.Locator#getLineNumber
 226      */
 227     public int getLineNumber ()
 228     {
 229         return this.lineNumber;
 230     }
 231 
 232 
 233     /**
 234      * The column number of the end of the text where the exception occurred.
 235      *
 236      * <p>The first column in a line is position 1.</p>
 237      *
 238      * @return An integer representing the column number, or -1
 239      *         if none is available.
 240      * @see org.xml.sax.Locator#getColumnNumber
 241      */
 242     public int getColumnNumber ()
 243     {
 244         return this.columnNumber;
 245     }
 246 
 247     /**
 248      * Override toString to provide more detailed error message.
 249      *
 250      * @return A string representation of this exception.
 251      */
 252     public String toString() {
 253         StringBuilder buf = new StringBuilder(getClass().getName());
 254         String message = getLocalizedMessage();
 255         if (publicId!=null)    buf.append("publicId: ").append(publicId);
 256         if (systemId!=null)    buf.append("; systemId: ").append(systemId);
 257         if (lineNumber!=-1)    buf.append("; lineNumber: ").append(lineNumber);
 258         if (columnNumber!=-1)  buf.append("; columnNumber: ").append(columnNumber);
 259 
 260        //append the exception message at the end
 261         if (message!=null)     buf.append("; ").append(message);
 262         return buf.toString();
 263     }
 264 
 265     //////////////////////////////////////////////////////////////////////
 266     // Internal state.
 267     //////////////////////////////////////////////////////////////////////
 268 
 269 
 270     /**
 271      * @serial The public identifier, or null.
 272      * @see #getPublicId
 273      */
 274     private String publicId;
 275 
 276 
 277     /**
 278      * @serial The system identifier, or null.
 279      * @see #getSystemId
 280      */
 281     private String systemId;
 282 
 283 
 284     /**
 285      * @serial The line number, or -1.
 286      * @see #getLineNumber
 287      */
 288     private int lineNumber;
 289 
 290 
 291     /**
 292      * @serial The column number, or -1.
 293      * @see #getColumnNumber
 294      */
 295     private int columnNumber;
 296 
 297     // Added serialVersionUID to preserve binary compatibility
 298     static final long serialVersionUID = -5651165872476709336L;
 299 }
 300 
 301 // end of SAXParseException.java