Ticket #24977: patch-Fix-Sonar-issues-in-WikimediaCommonsEntry.patch

File patch-Fix-Sonar-issues-in-WikimediaCommonsEntry.patch, 3.2 KB (added by DanProgs <Abenteurer.2901@…>, 3 days ago)
  • src/org/openstreetmap/josm/gui/layer/geoimage/WikimediaCommonsEntry.java

    ### Eclipse Workspace Patch 1.0
    #P josm
    diff --git src/org/openstreetmap/josm/gui/layer/geoimage/WikimediaCommonsEntry.java src/org/openstreetmap/josm/gui/layer/geoimage/WikimediaCommonsEntry.java
    index 8df45ae..8aa4e61 100644
     
    33
    44import java.net.MalformedURLException;
    55import java.net.URI;
    6 import java.net.URISyntaxException;
    76import java.net.URL;
    87import java.util.Objects;
    98
    109import org.openstreetmap.josm.data.coor.LatLon;
    11 import org.openstreetmap.josm.tools.JosmRuntimeException;
    1210import org.openstreetmap.josm.tools.Mediawiki;
    1311
    1412/**
    1513 * A geocoded image from <a href="https://commons.wikimedia.org/">Wikimedia Commons</a>
    1614 */
    1715class WikimediaCommonsEntry extends ImageEntry {
     16    private static final String COMMONS_BASE_URL = "https://upload.wikimedia.org/wikipedia/commons";
    1817    private final String title;
    1918
    2019    WikimediaCommonsEntry(String title, LatLon latLon) {
    21         this.title = title.replaceFirst("^File:", "").replace(" ", "_");
    22         setPos(latLon);
     20        Objects.requireNonNull(title, "Title must not be null");
     21        this.title = title.trim()
     22                .replaceFirst("^(?i)File:", "")
     23                .replace("\\s+", "_");
     24       
     25        if (latLon != null) {
     26            setPos(latLon);
     27        }
    2328    }
    2429
    2530    @Override
    2631    protected URL getImageUrl() throws MalformedURLException {
    27         return new URL(Mediawiki.getImageUrl("https://upload.wikimedia.org/wikipedia/commons", title));
     32        try {
     33            return getImageURI().toURL();
     34        } catch (IllegalArgumentException | MalformedURLException e) {
     35            throw new MalformedURLException("Invalid Commons URL for '" + title + "': " + e.getMessage());
     36        }
    2837    }
    2938
    3039    @Override
    3140    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         }
     41        return URI.create(Mediawiki.getImageUrl(COMMONS_BASE_URL, title));
    3842    }
    3943
    4044    @Override
    … …  
    4448
    4549    @Override
    4650    public String toString() {
    47         return "File:" + title;
     51        return getDisplayName();
    4852    }
    4953
    5054    @Override
    5155    public int hashCode() {
    52         return 31 * super.hashCode() + Objects.hash(title);
     56        return 31 * super.hashCode() + Objects.hash(title);
    5357    }
    5458
    5559    @Override
    5660    public boolean equals(Object obj) {
    57         if (this == obj)
    58             return true;
    59         if (!super.equals(obj) || getClass() != obj.getClass())
     61        if (this == obj) {
     62            return true;
     63        }
     64       
     65        if (obj == null || getClass() != obj.getClass()) {
    6066            return false;
    61         WikimediaCommonsEntry other = (WikimediaCommonsEntry) obj;
     67        }
     68       
     69        if (!super.equals(obj)) {
     70            return false;
     71        }
     72       
     73        final WikimediaCommonsEntry other = (WikimediaCommonsEntry) obj;
    6274        return Objects.equals(title, other.title);
    6375    }
    6476}