1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Optional;
|
---|
7 |
|
---|
8 | import javax.xml.stream.Location;
|
---|
9 | import javax.xml.stream.XMLStreamException;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Exception for unexpected processing errors during XML stream parsing.
|
---|
13 | * It uses proper JOSM i18n system to translate error message, including file location.
|
---|
14 | * @since 10235
|
---|
15 | */
|
---|
16 | public class XmlStreamParsingException extends XMLStreamException {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Constructs a new {@code XmlStreamParsingException}.
|
---|
20 | * @param msg error message
|
---|
21 | * @param location file location
|
---|
22 | */
|
---|
23 | public XmlStreamParsingException(String msg, Location location) {
|
---|
24 | super(msg); /* cannot use super(msg, location) because it messes with the message preventing localization */
|
---|
25 | this.location = location;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new {@code XmlStreamParsingException}.
|
---|
30 | * @param msg error message
|
---|
31 | * @param location file location
|
---|
32 | * @param th Throwable cause
|
---|
33 | */
|
---|
34 | public XmlStreamParsingException(String msg, Location location, Throwable th) {
|
---|
35 | super(msg, th);
|
---|
36 | this.location = location;
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public String getMessage() {
|
---|
41 | String msg = Optional.ofNullable(super.getMessage()).orElseGet(() -> getClass().getName());
|
---|
42 | if (getLocation() == null)
|
---|
43 | return msg;
|
---|
44 | StringBuilder sb = new StringBuilder(msg).append(' ')
|
---|
45 | .append(tr("(at line {0}, column {1})", getLocation().getLineNumber(), getLocation().getColumnNumber()));
|
---|
46 | int offset = getLocation().getCharacterOffset();
|
---|
47 | if (offset > -1) {
|
---|
48 | sb.append(". ").append(tr("{0} bytes have been read", offset));
|
---|
49 | }
|
---|
50 | return sb.toString();
|
---|
51 | }
|
---|
52 | }
|
---|