001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.IOException;
007import java.io.InputStream;
008import java.io.Reader;
009
010import javax.xml.parsers.ParserConfigurationException;
011
012import org.openstreetmap.josm.data.gpx.GpxConstants;
013import org.openstreetmap.josm.data.gpx.GpxData;
014import org.openstreetmap.josm.tools.Logging;
015import org.openstreetmap.josm.tools.Utils;
016import org.openstreetmap.josm.tools.XmlUtils;
017import org.xml.sax.InputSource;
018import org.xml.sax.SAXException;
019import org.xml.sax.SAXParseException;
020
021/**
022 * Read a gpx file.
023 * <p>
024 * Bounds are read, even if we calculate them, see {@link GpxData#recalculateBounds}.<br>
025 * Both GPX version 1.0 and 1.1 are supported.
026 *
027 * @author imi, ramack
028 */
029public class GpxReader implements GpxConstants, IGpxReader {
030
031    /** The resulting gpx data */
032    private GpxData gpxData;
033    private final InputSource inputSource;
034
035    /**
036     * Constructs a new {@code GpxReader}, which can later parse the input stream
037     * and store the result in trackData and markerData
038     *
039     * @param source the source input stream
040     * @throws IOException if an IO error occurs, e.g. the input stream is closed.
041     */
042    public GpxReader(InputStream source) throws IOException {
043        Reader utf8stream = UTFInputStreamReader.create(source);
044        Reader filtered = new InvalidXmlCharacterFilter(utf8stream); // NOPMD
045        this.inputSource = new InputSource(filtered);
046    }
047
048    /**
049     * Parse the GPX data.
050     *
051     * @param tryToFinish true, if the reader should return at least part of the GPX
052     * data in case of an error.
053     * @return true if file was properly parsed, false if there was error during
054     * parsing but some data were parsed anyway
055     * @throws SAXException if any SAX parsing error occurs
056     * @throws IOException if any I/O error occurs
057     */
058    @Override
059    public boolean parse(boolean tryToFinish) throws SAXException, IOException {
060        GpxParser parser = new GpxParser();
061        try {
062            XmlUtils.parseSafeSAX(inputSource, parser);
063            return true;
064        } catch (SAXException e) {
065            if (tryToFinish) {
066                parser.tryToFinish();
067                String message = e.getLocalizedMessage();
068                if (e instanceof SAXParseException) {
069                    boolean dot = message.lastIndexOf('.') == message.length() - 1;
070                    if (dot)
071                        message = message.substring(0, message.length() - 1);
072                    SAXParseException spe = (SAXParseException) e;
073                    message += ' ' + tr("(at line {0}, column {1})", spe.getLineNumber(), spe.getColumnNumber());
074                    if (dot)
075                        message += '.';
076                }
077                if (!Utils.isStripEmpty(parser.getData().creator)) {
078                    message += "\n" + tr("The file was created by \"{0}\".", parser.getData().creator);
079                }
080                SAXException ex = new SAXException(message, e);
081                if (parser.getData().isEmpty())
082                    throw ex;
083                Logging.warn(ex);
084                return false;
085            } else
086                throw e;
087        } catch (ParserConfigurationException e) {
088            Logging.error(e); // broken SAXException chaining
089            throw new SAXException(e);
090        } finally {
091            if (parser.getData() != null) {
092                this.gpxData = parser.getData();
093            }
094        }
095    }
096
097    @Override
098    public GpxData getGpxData() {
099        return gpxData;
100    }
101}