Changeset 15701 in josm
- Timestamp:
- 2020-01-13T22:30:27+01:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/util/Tag2Link.java
r15679 r15701 9 9 import java.net.MalformedURLException; 10 10 import java.net.URL; 11 import java.nio.charset.StandardCharsets; 11 12 import java.util.regex.Matcher; 12 13 import java.util.regex.Pattern; … … 15 16 import javax.json.Json; 16 17 import javax.json.JsonArray; 18 import javax.json.JsonReader; 17 19 import javax.json.JsonValue; 18 20 19 import com.drew.lang.Charsets;20 21 import org.openstreetmap.josm.data.osm.OsmUtils; 21 22 import org.openstreetmap.josm.io.CachedFile; … … 44 45 * Maps OSM keys to formatter URLs from Wikidata and OSM Sophox where {@code "$1"} has to be replaced by a value. 45 46 */ 46 protectedstatic MultiMap<String, String> wikidataRules = new MultiMap<>();47 static MultiMap<String, String> wikidataRules = new MultiMap<>(); 47 48 48 49 private Tag2Link() { … … 50 51 } 51 52 53 /** 54 * Represents an operation that accepts a link. 55 */ 52 56 @FunctionalInterface 53 57 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 */ 54 63 void acceptLink(String name, String url); 55 64 } … … 78 87 private static void fetchRulesViaSPARQL(final String query, final String server) throws IOException { 79 88 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 } 83 93 84 94 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"); 87 99 } 88 100 … … 106 118 107 119 static void getLinksForTag(String key, String value, LinkConsumer linkConsumer) { 108 Matcher keyMatcher;109 Matcher valueMatcher;110 120 111 121 // Search … … 130 140 131 141 // 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()) { 134 145 final String lang = Utils.firstNotEmptyString("en", keyMatcher.group("lang"), valueMatcher.group("lang")); 135 146 linkConsumer.acceptLink(tr("View Wikipedia article"), "https://" + lang + ".wikipedia.org/wiki/" + valueMatcher.group("article"));
Note:
See TracChangeset
for help on using the changeset viewer.