Changeset 16434 in josm for trunk/src/org
- Timestamp:
- 2020-05-17T00:02:18+02:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Tag2Link.java
r16172 r16434 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; … … 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 /** … … 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"); … … 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 query94 * @param server the query server95 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.5.16/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().get JsonObject("OSM_key").getString("value");113 final String url = rule.asJsonObject().get JsonObject("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); … … 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
Note:
See TracChangeset
for help on using the changeset viewer.