Changeset 16434 in josm


Ignore:
Timestamp:
2020-05-17T00:02:18+02:00 (3 weeks ago)
Author:
simon04
Message:

fix #19071 - Tag2Link: obtain rules via Apache Ivy and org.webjars.npm

Location:
trunk
Files:
3 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/ivy.xml

    r16403 r16434  
    3131        <dependency conf="api->default" org="ch.poole" name="OpeningHoursParser" rev="0.21.4"/>
    3232        <dependency conf="api->default" org="oauth.signpost" name="signpost-core" rev="2.0.0"/>
     33        <dependency conf="api->default" org="org.webjars.npm" name="tag2link" rev="2020.5.16"/>
    3334        <!-- sources->sources -->
    3435        <dependency conf="sources->sources" org="org.openstreetmap.jmapviewer" name="jmapviewer" rev="2.13"/>
     
    4243        <dependency conf="sources->sources" org="ch.poole" name="OpeningHoursParser" rev="0.21.4"/>
    4344        <dependency conf="sources->sources" org="oauth.signpost" name="signpost-core" rev="2.0.0"/>
     45        <dependency conf="sources->sources" org="org.webjars.npm" name="tag2link" rev="2020.5.16"/>
    4446        <!-- commonslang->default -->
    4547        <dependency conf="commonslang->default" org="org.apache.commons" name="commons-lang3" rev="3.10"/>
  • trunk/src/org/openstreetmap/josm/tools/Tag2Link.java

    r16172 r16434  
    55import static org.openstreetmap.josm.tools.I18n.trn;
    66
    7 import java.io.BufferedReader;
    87import java.io.IOException;
     8import java.io.InputStream;
    99import java.net.MalformedURLException;
    1010import java.net.URL;
    11 import java.nio.charset.StandardCharsets;
    1211import java.util.Collections;
    1312import java.util.Map;
     
    2423
    2524import org.openstreetmap.josm.data.osm.OsmUtils;
    26 import org.openstreetmap.josm.io.CachedFile;
    27 import org.openstreetmap.josm.spi.preferences.Config;
    2825
    2926/**
     
    8077        try {
    8178            wikidataRules.clear();
    82             fetchRulesViaSPARQL("resource://data/tag2link.wikidata.sparql", Config.getUrls().getJOSMWebsite() + "/remote/wikidata-sparql");
    83             fetchRulesViaSPARQL("resource://data/tag2link.sophox.sparql", Config.getUrls().getJOSMWebsite() + "/remote/sophox-sparql");
     79            initializeFromResources();
    8480        } catch (Exception e) {
    8581            Logging.error("Failed to initialize tag2link rules");
     
    8985
    9086    /**
    91      * Fetches rules from Wikidata using a SPARQL query.
     87     * Initializes the tag2link rules from the resources.
    9288     *
    93      * @param query the SPARQL query
    94      * @param server the query server
    9589     * @throws IOException in case of I/O error
    9690     */
    97     private static void fetchRulesViaSPARQL(final String query, final String server) throws IOException {
    98         final int initialSize = wikidataRules.size();
    99         final String sparql;
    100         try (CachedFile cachedFile = new CachedFile(query)) {
    101             sparql = new String(cachedFile.getByteContent(), StandardCharsets.UTF_8);
    102         }
    103 
     91    private static void initializeFromResources() throws IOException {
     92        final String resource = "META-INF/resources/webjars/tag2link/2020.5.16/index.json";
    10493        final JsonArray rules;
    105         try (CachedFile cachedFile = new CachedFile(server + "?query=" + Utils.encodeUrl(sparql));
    106              BufferedReader reader = cachedFile.setHttpAccept("application/json").getContentReader();
    107              JsonReader jsonReader = Json.createReader(reader)) {
    108             rules = jsonReader.read().asJsonObject().getJsonObject("results").getJsonArray("bindings");
     94        try (InputStream inputStream = Tag2Link.class.getClassLoader().getResourceAsStream(resource);
     95             JsonReader jsonReader = Json.createReader(inputStream)) {
     96            rules = jsonReader.readArray();
    10997        }
    11098
    11199        for (JsonValue rule : rules) {
    112             final String key = rule.asJsonObject().getJsonObject("OSM_key").getString("value");
    113             final String url = rule.asJsonObject().getJsonObject("formatter_URL").getString("value");
     100            final String key = rule.asJsonObject().getString("key");
     101            final String url = rule.asJsonObject().getString("url");
    114102            if (key.startsWith("Key:")) {
    115103                wikidataRules.put(key.substring("Key:".length()), url);
     
    120108                .forEach(wikidataRules::remove);
    121109
    122         final int size = wikidataRules.size() - initialSize;
     110        final int size = wikidataRules.size();
    123111        Logging.info(trn(
    124112                "Obtained {0} Tag2Link rule from {1}",
    125113                "Obtained {0} Tag2Link rules from {1}",
    126                 size, size, server));
     114                size, size, resource));
    127115    }
    128116
  • trunk/test/unit/org/openstreetmap/josm/tools/Tag2LinkTest.java

    r15794 r16434  
    2222    void checkLinks(String... expected) {
    2323        Assert.assertEquals(Arrays.asList(expected), links);
     24    }
     25
     26    /**
     27     * Unit test of function {@link Tag2Link#initialize()}.
     28     */
     29    @Test
     30    public void testInitialize() {
     31        Tag2Link.initialize();
     32        Assert.assertTrue("obtains at least 40 rules", Tag2Link.wikidataRules.size() > 40);
    2433    }
    2534
Note: See TracChangeset for help on using the changeset viewer.