001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.io.importexport;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.File;
007import java.io.IOException;
008import java.io.InputStream;
009
010import org.openstreetmap.josm.actions.ExtensionFileFilter;
011import org.openstreetmap.josm.gui.io.importexport.GpxImporter.GpxImporterData;
012import org.openstreetmap.josm.io.nmea.NmeaReader;
013
014/**
015 * File importer allowing to import NMEA-0183 files (*.nmea/nme/nma/log/txt files).
016 * @since 1637
017 */
018public class NMEAImporter extends GpxLikeImporter<NmeaReader> {
019
020    /**
021     * The NMEA file filter (*.nmea *.nme *.nma *.log *.txt files).
022     */
023    public static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
024            "nmea,nme,nma,log,txt", "nmea", tr("NMEA-0183 Files"), false);
025
026    /**
027     * Constructs a new {@code NMEAImporter}.
028     */
029    public NMEAImporter() {
030        super(FILE_FILTER, NmeaReader.class);
031    }
032
033    @Override
034    protected void appendInfoboxContent(StringBuilder msg, boolean success, NmeaReader r) {
035        msg.append(tr("Malformed sentences: {0}", r.getParserMalformed())).append("<br>")
036           .append(tr("Checksum errors: {0}", r.getParserChecksumErrors())).append("<br>");
037        if (!success) {
038            msg.append(tr("Unknown sentences: {0}", r.getParserUnknown())).append("<br>");
039        }
040        msg.append(tr("Zero coordinates: {0}", r.getParserZeroCoordinates()));
041    }
042
043    /**
044     * Replies the new GPX and marker layers corresponding to the specified NMEA file.
045     * @param is input stream to NMEA 0183 data
046     * @param associatedFile NMEA file
047     * @param gpxLayerName The GPX layer name
048     * @return the new GPX and marker layers corresponding to the specified NMEA file
049     * @throws IOException if an I/O error occurs
050     */
051    public static GpxImporterData loadLayers(InputStream is, final File associatedFile,
052                                             final String gpxLayerName) throws IOException {
053        final NmeaReader r = buildAndParse(is, NmeaReader.class);
054        final boolean parsedProperly = r.getNumberOfCoordinates() > 0;
055        r.getGpxData().storageFile = associatedFile;
056        return GpxImporter.loadLayers(r.getGpxData(), parsedProperly, gpxLayerName);
057    }
058}