Path: blob/master/src/java.xml/share/classes/org/xml/sax/SAXParseException.java
40948 views
/*1* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package org.xml.sax;2627/**28* Encapsulate an XML parse error or warning.29*30* <p>This exception may include information for locating the error31* in the original XML document, as if it came from a {@link Locator}32* object. Note that although the application33* will receive a SAXParseException as the argument to the handlers34* in the {@link org.xml.sax.ErrorHandler ErrorHandler} interface,35* the application is not actually required to throw the exception;36* instead, it can simply read the information in it and take a37* different action.</p>38*39* <p>Since this exception is a subclass of {@link org.xml.sax.SAXException40* SAXException}, it inherits the ability to wrap another exception.</p>41*42* @since 1.4, SAX 1.043* @author David Megginson44* @version 2.0.1 (sax2r2)45* @see org.xml.sax.SAXException46* @see org.xml.sax.Locator47* @see org.xml.sax.ErrorHandler48*/49public class SAXParseException extends SAXException {505152//////////////////////////////////////////////////////////////////////53// Constructors.54//////////////////////////////////////////////////////////////////////555657/**58* Create a new SAXParseException from a message and a Locator.59*60* <p>This constructor is especially useful when an application is61* creating its own exception from within a {@link org.xml.sax.ContentHandler62* ContentHandler} callback.</p>63*64* @param message The error or warning message.65* @param locator The locator object for the error or warning (may be66* null).67* @see org.xml.sax.Locator68*/69public SAXParseException (String message, Locator locator) {70super(message);71if (locator != null) {72init(locator.getPublicId(), locator.getSystemId(),73locator.getLineNumber(), locator.getColumnNumber());74} else {75init(null, null, -1, -1);76}77}787980/**81* Wrap an existing exception in a SAXParseException.82*83* <p>This constructor is especially useful when an application is84* creating its own exception from within a {@link org.xml.sax.ContentHandler85* ContentHandler} callback, and needs to wrap an existing exception that is not a86* subclass of {@link org.xml.sax.SAXException SAXException}.</p>87*88* @param message The error or warning message, or null to89* use the message from the embedded exception.90* @param locator The locator object for the error or warning (may be91* null).92* @param e Any exception.93* @see org.xml.sax.Locator94*/95public SAXParseException (String message, Locator locator,96Exception e) {97super(message, e);98if (locator != null) {99init(locator.getPublicId(), locator.getSystemId(),100locator.getLineNumber(), locator.getColumnNumber());101} else {102init(null, null, -1, -1);103}104}105106107/**108* Create a new SAXParseException.109*110* <p>This constructor is most useful for parser writers.</p>111*112* <p>All parameters except the message are as if113* they were provided by a {@link Locator}. For example, if the114* system identifier is a URL (including relative filename), the115* caller must resolve it fully before creating the exception.</p>116*117*118* @param message The error or warning message.119* @param publicId The public identifier of the entity that generated120* the error or warning.121* @param systemId The system identifier of the entity that generated122* the error or warning.123* @param lineNumber The line number of the end of the text that124* caused the error or warning.125* @param columnNumber The column number of the end of the text that126* cause the error or warning.127*/128public SAXParseException (String message, String publicId, String systemId,129int lineNumber, int columnNumber)130{131super(message);132init(publicId, systemId, lineNumber, columnNumber);133}134135136/**137* Create a new SAXParseException with an embedded exception.138*139* <p>This constructor is most useful for parser writers who140* need to wrap an exception that is not a subclass of141* {@link org.xml.sax.SAXException SAXException}.</p>142*143* <p>All parameters except the message and exception are as if144* they were provided by a {@link Locator}. For example, if the145* system identifier is a URL (including relative filename), the146* caller must resolve it fully before creating the exception.</p>147*148* @param message The error or warning message, or null to use149* the message from the embedded exception.150* @param publicId The public identifier of the entity that generated151* the error or warning.152* @param systemId The system identifier of the entity that generated153* the error or warning.154* @param lineNumber The line number of the end of the text that155* caused the error or warning.156* @param columnNumber The column number of the end of the text that157* cause the error or warning.158* @param e Another exception to embed in this one.159*/160public SAXParseException (String message, String publicId, String systemId,161int lineNumber, int columnNumber, Exception e)162{163super(message, e);164init(publicId, systemId, lineNumber, columnNumber);165}166167168/**169* Internal initialization method.170*171* @param publicId The public identifier of the entity which generated the exception,172* or null.173* @param systemId The system identifier of the entity which generated the exception,174* or null.175* @param lineNumber The line number of the error, or -1.176* @param columnNumber The column number of the error, or -1.177*/178private void init (String publicId, String systemId,179int lineNumber, int columnNumber)180{181this.publicId = publicId;182this.systemId = systemId;183this.lineNumber = lineNumber;184this.columnNumber = columnNumber;185}186187188/**189* Get the public identifier of the entity where the exception occurred.190*191* @return A string containing the public identifier, or null192* if none is available.193* @see org.xml.sax.Locator#getPublicId194*/195public String getPublicId ()196{197return this.publicId;198}199200201/**202* Get the system identifier of the entity where the exception occurred.203*204* <p>If the system identifier is a URL, it will have been resolved205* fully.</p>206*207* @return A string containing the system identifier, or null208* if none is available.209* @see org.xml.sax.Locator#getSystemId210*/211public String getSystemId ()212{213return this.systemId;214}215216217/**218* The line number of the end of the text where the exception occurred.219*220* <p>The first line is line 1.</p>221*222* @return An integer representing the line number, or -1223* if none is available.224* @see org.xml.sax.Locator#getLineNumber225*/226public int getLineNumber ()227{228return this.lineNumber;229}230231232/**233* The column number of the end of the text where the exception occurred.234*235* <p>The first column in a line is position 1.</p>236*237* @return An integer representing the column number, or -1238* if none is available.239* @see org.xml.sax.Locator#getColumnNumber240*/241public int getColumnNumber ()242{243return this.columnNumber;244}245246/**247* Override toString to provide more detailed error message.248*249* @return A string representation of this exception.250*/251public String toString() {252StringBuilder buf = new StringBuilder(getClass().getName());253String message = getLocalizedMessage();254if (publicId!=null) buf.append("publicId: ").append(publicId);255if (systemId!=null) buf.append("; systemId: ").append(systemId);256if (lineNumber!=-1) buf.append("; lineNumber: ").append(lineNumber);257if (columnNumber!=-1) buf.append("; columnNumber: ").append(columnNumber);258259//append the exception message at the end260if (message!=null) buf.append("; ").append(message);261return buf.toString();262}263264//////////////////////////////////////////////////////////////////////265// Internal state.266//////////////////////////////////////////////////////////////////////267268269/**270* @serial The public identifier, or null.271* @see #getPublicId272*/273private String publicId;274275276/**277* @serial The system identifier, or null.278* @see #getSystemId279*/280private String systemId;281282283/**284* @serial The line number, or -1.285* @see #getLineNumber286*/287private int lineNumber;288289290/**291* @serial The column number, or -1.292* @see #getColumnNumber293*/294private int columnNumber;295296// Added serialVersionUID to preserve binary compatibility297static final long serialVersionUID = -5651165872476709336L;298}299300// end of SAXParseException.java301302303