1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.markerlayer;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.net.MalformedURLException;
|
---|
6 | import java.net.URL;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.Optional;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.gpx.Extensions;
|
---|
14 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
15 | import org.openstreetmap.josm.data.gpx.GpxLink;
|
---|
16 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
17 | import org.openstreetmap.josm.tools.Utils;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Marker specifying the default behaviour.
|
---|
21 | * @since 11892 (extracted from Marker)
|
---|
22 | */
|
---|
23 | public final class DefaultMarkerProducers implements MarkerProducers {
|
---|
24 |
|
---|
25 | @Override
|
---|
26 | public Collection<Marker> createMarkers(WayPoint wpt, File relativePath, MarkerLayer parentLayer, double time,
|
---|
27 | double offset) {
|
---|
28 | String uri = null;
|
---|
29 | // cheapest way to check whether "link" object exists and is a non-empty collection of GpxLink objects...
|
---|
30 | Collection<GpxLink> links = wpt.<GpxLink>getCollection(GpxConstants.META_LINKS);
|
---|
31 | if (links != null) {
|
---|
32 | for (GpxLink oneLink : links) {
|
---|
33 | uri = oneLink.uri;
|
---|
34 | break;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | URL url = uriToUrl(uri, relativePath);
|
---|
39 | String urlStr = url == null ? "" : url.toString();
|
---|
40 | String symbolName = Optional.ofNullable(wpt.getString("symbol"))
|
---|
41 | .orElseGet(() -> wpt.getString(GpxConstants.PT_SYM));
|
---|
42 | // text marker is returned in every case, see #10208
|
---|
43 | final Marker marker = new Marker(wpt.getCoor(), wpt, symbolName, parentLayer, time, offset);
|
---|
44 | if (url == null) {
|
---|
45 | return Collections.singleton(marker);
|
---|
46 | } else if (Utils.hasExtension(urlStr, "wav", "mp3", "aac", "aif", "aiff")) {
|
---|
47 | final AudioMarker audioMarker = new AudioMarker(wpt.getCoor(), wpt, url, parentLayer, time, offset);
|
---|
48 | Extensions exts = (Extensions) wpt.get(GpxConstants.META_EXTENSIONS);
|
---|
49 | if (exts != null && exts.containsKey("offset")) {
|
---|
50 | try {
|
---|
51 | audioMarker.syncOffset = Double.parseDouble(exts.get("sync-offset"));
|
---|
52 | } catch (NumberFormatException nfe) {
|
---|
53 | Main.warn(nfe);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | return Arrays.asList(marker, audioMarker);
|
---|
57 | } else if (Utils.hasExtension(urlStr, "png", "jpg", "jpeg", "gif")) {
|
---|
58 | return Arrays.asList(marker, new ImageMarker(wpt.getCoor(), url, parentLayer, time, offset));
|
---|
59 | } else {
|
---|
60 | return Arrays.asList(marker, new WebMarker(wpt.getCoor(), url, parentLayer, time, offset));
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | private static URL uriToUrl(String uri, File relativePath) {
|
---|
65 | URL url = null;
|
---|
66 | if (uri != null) {
|
---|
67 | try {
|
---|
68 | url = new URL(uri);
|
---|
69 | } catch (MalformedURLException e) {
|
---|
70 | // Try a relative file:// url, if the link is not in an URL-compatible form
|
---|
71 | if (relativePath != null) {
|
---|
72 | url = Utils.fileToURL(new File(relativePath.getParentFile(), uri));
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | return url;
|
---|
77 | }
|
---|
78 | }
|
---|