1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.concurrent.Future;
|
---|
8 | import java.util.regex.Matcher;
|
---|
9 | import java.util.regex.Pattern;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
12 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
13 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
14 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
15 | import org.openstreetmap.josm.gui.io.DownloadPrimitivesWithReferrersTask;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Specialized task for downloading OSM objects by ID.
|
---|
20 | * <p>
|
---|
21 | * It handles one URL pattern: openstreetmap website URL with {@code /(node|way|relation)/<id>} argument.
|
---|
22 | * @since 8240
|
---|
23 | */
|
---|
24 | public class DownloadOsmIdTask extends DownloadOsmTask {
|
---|
25 |
|
---|
26 | private static final String URL_ID_PATTERN = "https?://www\\.(osm|openstreetmap)\\.org/(node|way|relation)/(\\p{Digit}+).*";
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public String[] getPatterns() {
|
---|
30 | return new String[]{URL_ID_PATTERN};
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
|
---|
35 | final Matcher matcher = Pattern.compile(URL_ID_PATTERN).matcher(url);
|
---|
36 | if (matcher.matches()) {
|
---|
37 | final OsmPrimitiveType type = OsmPrimitiveType.from(matcher.group(2));
|
---|
38 | final long id = Long.parseLong(matcher.group(3));
|
---|
39 | final PrimitiveId primitiveId = new SimplePrimitiveId(id, type);
|
---|
40 | final DownloadPrimitivesWithReferrersTask downloadTask = new DownloadPrimitivesWithReferrersTask(
|
---|
41 | newLayer, Collections.singletonList(primitiveId), true, true, null, null);
|
---|
42 | return MainApplication.worker.submit(downloadTask);
|
---|
43 | } else {
|
---|
44 | throw new IllegalStateException("Failed to parse id from " + url);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public String getTitle() {
|
---|
50 | return tr("Download OSM object by ID");
|
---|
51 | }
|
---|
52 | }
|
---|