Ignore:
Timestamp:
2020-01-13T22:30:27+01:00 (4 years ago)
Author:
simon04
Message:

see #13901 - fix SonarQube issues

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/util/Tag2Link.java

    r15679 r15701  
    99import java.net.MalformedURLException;
    1010import java.net.URL;
     11import java.nio.charset.StandardCharsets;
    1112import java.util.regex.Matcher;
    1213import java.util.regex.Pattern;
     
    1516import javax.json.Json;
    1617import javax.json.JsonArray;
     18import javax.json.JsonReader;
    1719import javax.json.JsonValue;
    1820
    19 import com.drew.lang.Charsets;
    2021import org.openstreetmap.josm.data.osm.OsmUtils;
    2122import org.openstreetmap.josm.io.CachedFile;
     
    4445     * Maps OSM keys to formatter URLs from Wikidata and OSM Sophox where {@code "$1"} has to be replaced by a value.
    4546     */
    46     protected static MultiMap<String, String> wikidataRules = new MultiMap<>();
     47    static MultiMap<String, String> wikidataRules = new MultiMap<>();
    4748
    4849    private Tag2Link() {
     
    5051    }
    5152
     53    /**
     54     * Represents an operation that accepts a link.
     55     */
    5256    @FunctionalInterface
    5357    interface LinkConsumer {
     58        /**
     59         * Performs the operation on the given arguments.
     60         * @param name the name/label of the link
     61         * @param url the URL of the link
     62         */
    5463        void acceptLink(String name, String url);
    5564    }
     
    7887    private static void fetchRulesViaSPARQL(final String query, final String server) throws IOException {
    7988        final int initialSize = wikidataRules.size();
    80         final String sparql = new String(new CachedFile(query).getByteContent(), Charsets.UTF_8);
    81         final CachedFile sparqlFile = new CachedFile(server + "?query=" + Utils.encodeUrl(sparql))
    82                 .setHttpAccept("application/json");
     89        final String sparql;
     90        try (CachedFile cachedFile = new CachedFile(query)) {
     91            sparql = new String(cachedFile.getByteContent(), StandardCharsets.UTF_8);
     92        }
    8393
    8494        final JsonArray rules;
    85         try (BufferedReader reader = sparqlFile.getContentReader()) {
    86             rules = Json.createReader(reader).read().asJsonObject().getJsonObject("results").getJsonArray("bindings");
     95        try (CachedFile cachedFile = new CachedFile(server + "?query=" + Utils.encodeUrl(sparql));
     96             BufferedReader reader = cachedFile.setHttpAccept("application/json").getContentReader();
     97             JsonReader jsonReader = Json.createReader(reader)) {
     98            rules = jsonReader.read().asJsonObject().getJsonObject("results").getJsonArray("bindings");
    8799        }
    88100
     
    106118
    107119    static void getLinksForTag(String key, String value, LinkConsumer linkConsumer) {
    108         Matcher keyMatcher;
    109         Matcher valueMatcher;
    110120
    111121        // Search
     
    130140
    131141        // Wikimedia
    132         if ((keyMatcher = Pattern.compile("wikipedia(:(?<lang>\\p{Lower}{2,}))?").matcher(key)).matches()
    133                 && (valueMatcher = Pattern.compile("((?<lang>\\p{Lower}{2,}):)?(?<article>.*)").matcher(value)).matches()) {
     142        final Matcher keyMatcher = Pattern.compile("wikipedia(:(?<lang>\\p{Lower}{2,}))?").matcher(key);
     143        final Matcher valueMatcher = Pattern.compile("((?<lang>\\p{Lower}{2,}):)?(?<article>.*)").matcher(value);
     144        if (keyMatcher.matches() && valueMatcher.matches()) {
    134145            final String lang = Utils.firstNotEmptyString("en", keyMatcher.group("lang"), valueMatcher.group("lang"));
    135146            linkConsumer.acceptLink(tr("View Wikipedia article"), "https://" + lang + ".wikipedia.org/wiki/" + valueMatcher.group("article"));
Note: See TracChangeset for help on using the changeset viewer.