001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import org.xml.sax.Locator; 007import org.xml.sax.SAXException; 008 009/** 010 * An exception thrown during XML parsing, with known line and column. 011 * @since 6906 012 */ 013public class XmlParsingException extends SAXException { 014 private int columnNumber; 015 private int lineNumber; 016 017 /** 018 * Constructs a new {@code XmlParsingException}. 019 * @param e The cause 020 */ 021 public XmlParsingException(Exception e) { 022 super(e); 023 } 024 025 /** 026 * Constructs a new {@code XmlParsingException}. 027 * @param message The error message 028 * @param e The cause 029 */ 030 public XmlParsingException(String message, Exception e) { 031 super(message, e); 032 } 033 034 /** 035 * Constructs a new {@code XmlParsingException}. 036 * @param message The error message 037 */ 038 public XmlParsingException(String message) { 039 super(message); 040 } 041 042 /** 043 * Sets the location (line/column) where the exception occurred. 044 * @param locator object giving the location (line/column) where the exception occurred 045 * @return {@code this} 046 */ 047 public XmlParsingException rememberLocation(Locator locator) { 048 if (locator != null) { 049 this.columnNumber = locator.getColumnNumber(); 050 this.lineNumber = locator.getLineNumber(); 051 } 052 return this; 053 } 054 055 @Override 056 public String getMessage() { 057 String msg = super.getMessage(); 058 if (lineNumber == 0 && columnNumber == 0) 059 return msg; 060 if (msg == null) { 061 msg = getClass().getName(); 062 } 063 return msg + ' ' + tr("(at line {0}, column {1})", lineNumber, columnNumber); 064 } 065 066 /** 067 * Returns the column number where the exception occurred. 068 * @return the column number where the exception occurred 069 */ 070 public int getColumnNumber() { 071 return columnNumber; 072 } 073 074 /** 075 * Returns the line number where the exception occurred. 076 * @return the line number where the exception occurred 077 */ 078 public int getLineNumber() { 079 return lineNumber; 080 } 081}