001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions.downloadtasks;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.concurrent.Future;
007import java.util.regex.Matcher;
008import java.util.regex.Pattern;
009
010import org.openstreetmap.josm.gui.progress.ProgressMonitor;
011
012/**
013 * Specialized task for downloading OSM notes by ID.
014 * <p>
015 * It handles one URL pattern: openstreetmap website URL with {@code /node/<id>} argument.
016 * @since 8195
017 */
018public class DownloadNotesUrlIdTask extends DownloadNotesTask {
019
020    private static final String URL_ID_PATTERN = "https?://www\\.(osm|openstreetmap)\\.org/note/(\\p{Digit}+).*";
021
022    @Override
023    public Future<?> loadUrl(DownloadParams settings, String url, ProgressMonitor progressMonitor) {
024        final Matcher matcher = Pattern.compile(URL_ID_PATTERN).matcher(url);
025        if (matcher.matches()) {
026            return download(Long.parseLong(matcher.group(2)), null);
027        } else {
028            throw new IllegalStateException("Failed to parse note id from " + url);
029        }
030    }
031
032    @Override
033    public String[] getPatterns() {
034        return new String[]{URL_ID_PATTERN};
035    }
036
037    @Override
038    public String getTitle() {
039        return tr("Download OSM Note by ID");
040    }
041}