001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import javax.xml.stream.Location;
007import javax.xml.stream.XMLStreamException;
008
009/**
010 * Exception for unexpected processing errors during XML stream parsing.
011 * It uses proper JOSM i18n system to translate error message, including file location.
012 * @since 10235
013 */
014public class XmlStreamParsingException extends XMLStreamException {
015
016    /**
017     * Constructs a new {@code XmlStreamParsingException}.
018     * @param msg error message
019     * @param location file location
020     */
021    public XmlStreamParsingException(String msg, Location location) {
022        super(msg); /* cannot use super(msg, location) because it messes with the message preventing localization */
023        this.location = location;
024    }
025
026    /**
027     * Constructs a new {@code XmlStreamParsingException}.
028     * @param msg error message
029     * @param location file location
030     * @param th Throwable cause
031     */
032    public XmlStreamParsingException(String msg, Location location, Throwable th) {
033        super(msg, th);
034        this.location = location;
035    }
036
037    @Override
038    public String getMessage() {
039        String msg = super.getMessage();
040        if (msg == null) {
041            msg = getClass().getName();
042        }
043        if (getLocation() == null)
044            return msg;
045        msg += ' ' + tr("(at line {0}, column {1})", getLocation().getLineNumber(), getLocation().getColumnNumber());
046        int offset = getLocation().getCharacterOffset();
047        if (offset > -1) {
048            msg += ". "+ tr("{0} bytes have been read", offset);
049        }
050        return msg;
051    }
052}