source: josm/trunk/src/org/openstreetmap/josm/gui/layer/geoimage/WikimediaCommonsEntry.java

Last change on this file was 18427, checked in by taylor.smock, 2 years ago

Fix an NPE that occurred when attempting to copy an image URL

This occurred when the user
1) Downloaded images from Wikimedia Commons
2) Attempted to open the image in an external program

This patch also fixes an issue where image paths, when copied with the
Copy image path action and were to remote resources, could have a /
removed from the protocol (https://example.org -> https:/example.org).

File size: 1.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.geoimage;
3
4import java.net.MalformedURLException;
5import java.net.URI;
6import java.net.URISyntaxException;
7import java.net.URL;
8import java.util.Objects;
9
10import org.openstreetmap.josm.data.coor.LatLon;
11import org.openstreetmap.josm.tools.JosmRuntimeException;
12import org.openstreetmap.josm.tools.Mediawiki;
13
14/**
15 * A geocoded image from <a href="https://commons.wikimedia.org/">Wikimedia Commons</a>
16 */
17class WikimediaCommonsEntry extends ImageEntry {
18 private final String title;
19
20 WikimediaCommonsEntry(String title, LatLon latLon) {
21 this.title = title.replaceFirst("^File:", "").replace(" ", "_");
22 setPos(latLon);
23 }
24
25 @Override
26 protected URL getImageUrl() throws MalformedURLException {
27 return new URL(Mediawiki.getImageUrl("https://upload.wikimedia.org/wikipedia/commons", title));
28 }
29
30 @Override
31 public URI getImageURI() {
32 try {
33 return new URI(Mediawiki.getImageUrl("https://upload.wikimedia.org/wikipedia/commons", this.title));
34 } catch (URISyntaxException e) {
35 // This should never happen.
36 throw new JosmRuntimeException(this.toString(), e);
37 }
38 }
39
40 @Override
41 public String getDisplayName() {
42 return "File:" + title;
43 }
44
45 @Override
46 public String toString() {
47 return "File:" + title;
48 }
49
50 @Override
51 public int hashCode() {
52 return 31 * super.hashCode() + Objects.hash(title);
53 }
54
55 @Override
56 public boolean equals(Object obj) {
57 if (this == obj)
58 return true;
59 if (!super.equals(obj) || getClass() != obj.getClass())
60 return false;
61 WikimediaCommonsEntry other = (WikimediaCommonsEntry) obj;
62 return Objects.equals(title, other.title);
63 }
64}
Note: See TracBrowser for help on using the repository browser.