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.Main;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
13 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
14 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
15 | import org.openstreetmap.josm.gui.io.DownloadPrimitivesWithReferrersTask;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 |
|
---|
18 | public class DownloadOsmIdTask extends DownloadOsmTask {
|
---|
19 |
|
---|
20 | private static final String URL_ID_PATTERN = "https?://www\\.(osm|openstreetmap)\\.org/(node|way|relation)/(\\p{Digit}+).*";
|
---|
21 |
|
---|
22 | @Override
|
---|
23 | public String[] getPatterns() {
|
---|
24 | return new String[]{URL_ID_PATTERN};
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
|
---|
29 | final Matcher matcher = Pattern.compile(URL_ID_PATTERN).matcher(url);
|
---|
30 | if (matcher.matches()) {
|
---|
31 | final OsmPrimitiveType type = OsmPrimitiveType.from(matcher.group(2));
|
---|
32 | final long id = Long.parseLong(matcher.group(3));
|
---|
33 | final PrimitiveId primitiveId = new SimplePrimitiveId(id, type);
|
---|
34 | final DownloadPrimitivesWithReferrersTask downloadTask = new DownloadPrimitivesWithReferrersTask(
|
---|
35 | newLayer, Collections.singletonList(primitiveId), true, true, null, null);
|
---|
36 | return Main.worker.submit(downloadTask);
|
---|
37 | } else {
|
---|
38 | throw new IllegalStateException("Failed to parse id from " + url);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public String getTitle() {
|
---|
44 | return tr("Download OSM object by ID");
|
---|
45 | }
|
---|
46 | }
|
---|