[7937] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import org.xml.sax.Locator;
|
---|
| 7 | import org.xml.sax.SAXException;
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * An exception thrown during XML parsing, with known line and column.
|
---|
| 11 | * @since 6906
|
---|
| 12 | */
|
---|
| 13 | public class XmlParsingException extends SAXException {
|
---|
| 14 | private int columnNumber;
|
---|
| 15 | private int lineNumber;
|
---|
| 16 |
|
---|
| 17 | /**
|
---|
| 18 | * Constructs a new {@code XmlParsingException}.
|
---|
| 19 | * @param e The cause
|
---|
| 20 | */
|
---|
| 21 | public XmlParsingException(Exception e) {
|
---|
| 22 | super(e);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Constructs a new {@code XmlParsingException}.
|
---|
| 27 | * @param message The error message
|
---|
| 28 | * @param e The cause
|
---|
| 29 | */
|
---|
| 30 | public XmlParsingException(String message, Exception e) {
|
---|
| 31 | super(message, e);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Constructs a new {@code XmlParsingException}.
|
---|
| 36 | * @param message The error message
|
---|
| 37 | */
|
---|
| 38 | public XmlParsingException(String message) {
|
---|
| 39 | super(message);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * Sets the location (line/column) where the exception occured.
|
---|
| 44 | * @param locator object giving the location (line/column) where the exception occured
|
---|
| 45 | * @return {@code this}
|
---|
| 46 | */
|
---|
| 47 | public XmlParsingException rememberLocation(Locator locator) {
|
---|
| 48 | if (locator != null) {
|
---|
| 49 | this.columnNumber = locator.getColumnNumber();
|
---|
| 50 | this.lineNumber = locator.getLineNumber();
|
---|
| 51 | }
|
---|
| 52 | return this;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | @Override
|
---|
| 56 | public String getMessage() {
|
---|
| 57 | String msg = super.getMessage();
|
---|
| 58 | if (lineNumber == 0 && columnNumber == 0)
|
---|
| 59 | return msg;
|
---|
| 60 | if (msg == null) {
|
---|
| 61 | msg = getClass().getName();
|
---|
| 62 | }
|
---|
[8846] | 63 | return msg + ' ' + tr("(at line {0}, column {1})", lineNumber, columnNumber);
|
---|
[7937] | 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * Returns the column number where the exception occured.
|
---|
| 68 | * @return the column number where the exception occured
|
---|
| 69 | */
|
---|
| 70 | public int getColumnNumber() {
|
---|
| 71 | return columnNumber;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * Returns the line number where the exception occured.
|
---|
| 76 | * @return the line number where the exception occured
|
---|
| 77 | */
|
---|
| 78 | public int getLineNumber() {
|
---|
| 79 | return lineNumber;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|