Changeset 18475 in josm for trunk/src/org
- Timestamp:
- 2022-06-08T19:09:15+02:00 (2 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Tag2Link.java
r18208 r18475 11 11 import java.util.Arrays; 12 12 import java.util.Collections; 13 import java.util. HashMap;13 import java.util.EnumMap; 14 14 import java.util.List; 15 15 import java.util.Map; 16 16 import java.util.Objects; 17 17 import java.util.Optional; 18 import java.util.Set; 18 19 import java.util.function.Supplier; 19 20 import java.util.function.UnaryOperator; … … 68 69 static final CachingProperty<List<String>> PREF_SEARCH_ENGINES = new ListProperty("tag2link.search", 69 70 Arrays.asList("https://duckduckgo.com/?q=$1", "https://www.google.com/search?q=$1")).cached(); 71 private static final Pattern PATTERN_DOLLAR_ONE = Pattern.compile("$1", Pattern.LITERAL); 70 72 71 73 private Tag2Link() { … … 148 150 } 149 151 150 final HashMap<OsmPrimitiveType, Optional<ImageResource>> memoize = new HashMap<>();152 final Map<OsmPrimitiveType, Optional<ImageResource>> memoize = new EnumMap<>(OsmPrimitiveType.class); 151 153 final Supplier<ImageResource> imageResource = () -> memoize 152 154 .computeIfAbsent(OsmPrimitiveType.NODE, type -> OsmPrimitiveImageProvider.getResource(key, value, type)) … … 209 211 } 210 212 211 wikidataRules.getValues(key).forEach(urlFormatter -> { 213 final Set<String> formatterUrls = wikidataRules.getValues(key); 214 if (!formatterUrls.isEmpty()) { 212 215 final String formattedValue = valueFormatter.getOrDefault(key, x -> x).apply(value); 213 final String url = urlFormatter.replace("$1", formattedValue); 214 linkConsumer.acceptLink(getLinkName(url, key), url, imageResource.get()); 215 }); 216 217 final String urlKey = formatterUrls.stream().map(urlFormatter -> PATTERN_DOLLAR_ONE.matcher(urlFormatter) 218 .replaceAll(Matcher.quoteReplacement("(.*)"))).map(PatternUtils::compile) 219 .map(pattern -> pattern.matcher(value)).filter(Matcher::matches) 220 .map(matcher -> matcher.group(1)).findFirst().orElse(formattedValue); 221 222 formatterUrls.forEach(urlFormatter -> { 223 // Check if the current value matches the formatter pattern -- some keys can take a full url or a key for 224 // the formatter. Example: https://wiki.openstreetmap.org/wiki/Key:contact:facebook 225 final String url = PATTERN_DOLLAR_ONE.matcher(urlFormatter).replaceAll(urlKey); 226 linkConsumer.acceptLink(getLinkName(url, key), url, imageResource.get()); 227 }); 228 } 216 229 } 217 230
Note:
See TracChangeset
for help on using the changeset viewer.