commit f9b6849a844329b96bfed184cfc224141f9cd5a8
Author: Simon Legner <Simon.Legner@gmail.com>
Date: 2020-04-11 19:39:24 +0200
fix #19071 - Tag2Link: obtain rules via Apache Ivy and org.webjars.npm
diff --git a/build.xml b/build.xml
index 77a24027c..bf67bdfa1 100644
a
|
b
|
Build-Date: ${build.tstamp}
|
1055 | 1055 | <ivy:cachepath log="download-only" pathid="test.path" conf="test"/> |
1056 | 1056 | </target> |
1057 | 1057 | <target name="extract-libraries" depends="resolve" description="extract libraries to build dir"> |
| 1058 | <unzip dest="${build.dir}"> |
| 1059 | <fileset refid="runtime.fileset"/> |
| 1060 | <patternset> |
| 1061 | <include name="META-INF/resources/webjars/tag2link/*/index.json"/> |
| 1062 | </patternset> |
| 1063 | </unzip> |
1058 | 1064 | <unzip dest="${build.dir}"> |
1059 | 1065 | <fileset refid="runtime.fileset"/> |
1060 | 1066 | <patternset> |
diff --git a/ivy.xml b/ivy.xml
index 55f43c995..d99f56381 100644
a
|
b
|
|
30 | 30 | <dependency conf="api->default" org="org.tukaani" name="xz" rev="1.8"/> |
31 | 31 | <dependency conf="api->default" org="com.drewnoakes" name="metadata-extractor" rev="2.13.0"/> |
32 | 32 | <dependency conf="api->default" org="ch.poole" name="OpeningHoursParser" rev="0.21.1"/> |
| 33 | <dependency conf="api->default" org="org.webjars.npm" name="tag2link" rev="2020.4.6"/> |
33 | 34 | <!-- sources->sources --> |
34 | 35 | <dependency conf="sources->sources" org="org.openstreetmap.jmapviewer" name="jmapviewer" rev="2.13"/> |
35 | 36 | <dependency conf="sources->sources" org="javax.json" name="javax.json-api" rev="1.1.4"/> |
… |
… |
|
40 | 41 | <dependency conf="sources->sources" org="org.tukaani" name="xz" rev="1.8"/> |
41 | 42 | <dependency conf="sources->sources" org="com.drewnoakes" name="metadata-extractor" rev="2.13.0"/> |
42 | 43 | <dependency conf="sources->sources" org="ch.poole" name="OpeningHoursParser" rev="0.21.1"/> |
| 44 | <dependency conf="sources->sources" org="org.webjars.npm" name="tag2link" rev="2020.4.6"/> |
43 | 45 | <!-- commonslang->default --> |
44 | 46 | <dependency conf="commonslang->default" org="org.apache.commons" name="commons-lang3" rev="3.10"/> |
45 | 47 | <!-- jacocoant->default --> |
diff --git a/resources/data/tag2link.sophox.sparql b/resources/data/tag2link.sophox.sparql
deleted file mode 100644
index 045ec234b..000000000
+
|
-
|
|
1 | | SELECT (CONCAT("Key:", ?permanent_key_ID) as ?OSM_key) ?formatter_URL WHERE { |
2 | | ?item osmdt:P2 osmd:Q7. |
3 | | ?item osmdt:P16 ?permanent_key_ID. |
4 | | ?item osmdt:P8 ?formatter_URL. |
5 | | } |
diff --git a/resources/data/tag2link.wikidata.sparql b/resources/data/tag2link.wikidata.sparql
deleted file mode 100644
index 3a67b5af7..000000000
+
|
-
|
|
1 | | SELECT ?OSM_key ?formatter_URL WHERE { |
2 | | { ?item wdt:P1282 ?OSM_key. } |
3 | | FILTER(STRSTARTS(?OSM_key, 'Key:')) . |
4 | | |
5 | | { |
6 | | { ?item wdt:P1630 ?formatter_URL. } |
7 | | UNION |
8 | | { ?item wdt:P3303 ?formatter_URL. } |
9 | | } |
10 | | } |
diff --git a/src/org/openstreetmap/josm/tools/Tag2Link.java b/src/org/openstreetmap/josm/tools/Tag2Link.java
index c778461f4..2fcd89d05 100644
a
|
b
|
|
4 | 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
5 | 5 | import static org.openstreetmap.josm.tools.I18n.trn; |
6 | 6 | |
7 | | import java.io.BufferedReader; |
8 | 7 | import java.io.IOException; |
| 8 | import java.io.InputStream; |
9 | 9 | import java.net.MalformedURLException; |
10 | 10 | import java.net.URL; |
11 | | import java.nio.charset.StandardCharsets; |
12 | 11 | import java.util.Collections; |
13 | 12 | import java.util.Map; |
14 | 13 | import java.util.function.UnaryOperator; |
… |
… |
|
23 | 22 | import javax.json.JsonValue; |
24 | 23 | |
25 | 24 | import org.openstreetmap.josm.data.osm.OsmUtils; |
26 | | import org.openstreetmap.josm.io.CachedFile; |
27 | | import org.openstreetmap.josm.spi.preferences.Config; |
28 | 25 | |
29 | 26 | /** |
30 | 27 | * Extracts web links from OSM tags. |
… |
… |
private Tag2Link() {
|
79 | 76 | public static void initialize() { |
80 | 77 | try { |
81 | 78 | 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(); |
84 | 80 | } catch (Exception e) { |
85 | 81 | Logging.error("Failed to initialize tag2link rules"); |
86 | 82 | Logging.error(e); |
… |
… |
public static void initialize() {
|
88 | 84 | } |
89 | 85 | |
90 | 86 | /** |
91 | | * Fetches rules from Wikidata using a SPARQL query. |
| 87 | * Initializes the tag2link rules from the resources. |
92 | 88 | * |
93 | | * @param query the SPARQL query |
94 | | * @param server the query server |
95 | 89 | * @throws IOException in case of I/O error |
96 | 90 | */ |
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.4.6/index.json"; |
104 | 93 | 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(); |
109 | 97 | } |
110 | 98 | |
111 | 99 | 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"); |
114 | 102 | if (key.startsWith("Key:")) { |
115 | 103 | wikidataRules.put(key.substring("Key:".length()), url); |
116 | 104 | } |
… |
… |
private static void fetchRulesViaSPARQL(final String query, final String server)
|
119 | 107 | Stream.of("image", "url", "website", "wikidata", "wikimedia_commons") |
120 | 108 | .forEach(wikidataRules::remove); |
121 | 109 | |
122 | | final int size = wikidataRules.size() - initialSize; |
| 110 | final int size = wikidataRules.size(); |
123 | 111 | Logging.info(trn( |
124 | 112 | "Obtained {0} Tag2Link rule from {1}", |
125 | 113 | "Obtained {0} Tag2Link rules from {1}", |
126 | | size, size, server)); |
| 114 | size, size, resource)); |
127 | 115 | } |
128 | 116 | |
129 | 117 | /** |
diff --git a/test/unit/org/openstreetmap/josm/tools/Tag2LinkTest.java b/test/unit/org/openstreetmap/josm/tools/Tag2LinkTest.java
index 1e1bc16fd..4a2a8928b 100644
a
|
b
|
void checkLinks(String... expected) {
|
23 | 23 | Assert.assertEquals(Arrays.asList(expected), links); |
24 | 24 | } |
25 | 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); |
| 33 | } |
| 34 | |
26 | 35 | /** |
27 | 36 | * Unit test of function {@link Tag2Link#getLinksForTag}. |
28 | 37 | */ |
diff --git a/test/unit/org/openstreetmap/josm/tools/Tag2LinkTestIT.java b/test/unit/org/openstreetmap/josm/tools/Tag2LinkTestIT.java
deleted file mode 100644
index 0dc69c7cb..000000000
+
|
-
|
|
1 | | // License: GPL. For details, see LICENSE file. |
2 | | package org.openstreetmap.josm.tools; |
3 | | |
4 | | import org.junit.Assert; |
5 | | import org.junit.ClassRule; |
6 | | import org.junit.Test; |
7 | | import org.openstreetmap.josm.testutils.JOSMTestRules; |
8 | | |
9 | | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
10 | | |
11 | | /** |
12 | | * Integration tests for the {@link Tag2Link} |
13 | | */ |
14 | | public class Tag2LinkTestIT { |
15 | | |
16 | | /** |
17 | | * Setup rule |
18 | | */ |
19 | | @ClassRule |
20 | | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
21 | | public static JOSMTestRules test = new JOSMTestRules().timeout(20_000); |
22 | | |
23 | | /** |
24 | | * Integration test of function {@link org.openstreetmap.josm.tools.Tag2Link#initialize()}. |
25 | | */ |
26 | | @Test |
27 | | public void testInitialize() { |
28 | | Tag2Link.initialize(); |
29 | | Assert.assertTrue("obtails at least 40 rules", Tag2Link.wikidataRules.size() > 40); |
30 | | } |
31 | | } |