// License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.actions.downloadtasks; import static org.openstreetmap.josm.tools.I18n.tr; import java.io.IOException; import java.net.URL; import java.util.concurrent.Future; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.openstreetmap.josm.Main; import org.openstreetmap.josm.data.Bounds; import org.openstreetmap.josm.data.Bounds.ParseMethod; import org.openstreetmap.josm.data.gpx.GpxData; import org.openstreetmap.josm.gui.PleaseWaitRunnable; import org.openstreetmap.josm.gui.layer.GpxLayer; import org.openstreetmap.josm.gui.layer.Layer; import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; import org.openstreetmap.josm.gui.progress.ProgressMonitor; import org.openstreetmap.josm.gui.progress.ProgressTaskId; import org.openstreetmap.josm.gui.progress.ProgressTaskIds; import org.openstreetmap.josm.io.BoundingBoxDownloader; import org.openstreetmap.josm.io.GpxImporter; import org.openstreetmap.josm.io.GpxImporter.GpxImporterData; import org.openstreetmap.josm.io.OsmServerLocationReader; import org.openstreetmap.josm.io.OsmServerReader; import org.openstreetmap.josm.io.OsmTransferException; import org.openstreetmap.josm.tools.CheckParameterUtil; import org.xml.sax.SAXException; /** * Task allowing to download GPS data. */ public class DownloadGpsTask extends AbstractDownloadTask { private DownloadTask downloadTask; private static final String PATTERN_TRACE_ID = "https?://.*(osm|openstreetmap).org/trace/\\p{Digit}+/data"; private static final String PATTERN_TRACKPOINTS_BBOX = "https?://.*/api/0.6/trackpoints\\?bbox=.*,.*,.*,.*"; private static final String PATTERN_EXTERNAL_GPX_SCRIPT = "https?://.*exportgpx.*"; private static final String PATTERN_EXTERNAL_GPX_FILE = "https?://.*/(.*\\.gpx)"; protected String newLayerName = null; @Override public String[] getPatterns() { return new String[] {PATTERN_EXTERNAL_GPX_FILE, PATTERN_EXTERNAL_GPX_SCRIPT, PATTERN_TRACE_ID, PATTERN_TRACKPOINTS_BBOX}; } @Override public String getTitle() { return tr("Download GPS"); } @Override public Future> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) { downloadTask = new DownloadTask(newLayer, new BoundingBoxDownloader(downloadArea), progressMonitor); // We need submit instead of execute so we can wait for it to finish and get the error // message if necessary. If no one calls getErrorMessage() it just behaves like execute. return Main.worker.submit(downloadTask); } @Override public Future> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) { CheckParameterUtil.ensureParameterNotNull(url, "url"); if (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_EXTERNAL_GPX_SCRIPT) || url.matches(PATTERN_EXTERNAL_GPX_FILE)) { downloadTask = new DownloadTask(newLayer, new OsmServerLocationReader(url), progressMonitor); // Extract .gpx filename from URL to set the new layer name Matcher matcher = Pattern.compile(PATTERN_EXTERNAL_GPX_FILE).matcher(url); newLayerName = matcher.matches() ? matcher.group(1) : null; // We need submit instead of execute so we can wait for it to finish and get the error // message if necessary. If no one calls getErrorMessage() it just behaves like execute. return Main.worker.submit(downloadTask); } else if (url.matches(PATTERN_TRACKPOINTS_BBOX)) { String[] table = url.split("\\?|=|&"); for (int i = 0; i