Changeset 35148 in osm
- Timestamp:
- 2019-09-27T16:12:13+02:00 (5 years ago)
- Location:
- applications/editors/josm/plugins/tag2link
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/tag2link/resources/tag2link_sources.xml
r35146 r35148 201 201 <link name="View INSEE explanation of %name% code" href="http://recherche-naf.insee.fr/SIRENET_ClassesNaf/%v%.htm" /> 202 202 </rule> 203 <!-- lat/lon of selected object works, but this website does not. Kept as an example 203 204 <rule> 204 205 <condition k="ref:FR:NAF" v="[A-Z0-9-]{5}" /> 205 <link name="View a map of %name% items" href="https://sidjy.github.io/ape.html?ape=%v%" /> 206 </rule> 207 <!-- TODO map lat/lon of selected object 208 <rule> 209 <condition k="ref:FR:NAF" v="[A-Z0-9-]{5}" /> 210 <link name="View a map of %name% items" href="https://sidjy.github.io/ape.html?ape=%v%#%zoom%/%lat%/%lon%" /> 206 <link name="View a map of %name% items" href="https://sidjy.github.io/ape.html?ape=%v%#15/%lat%/%lon%" /> 211 207 </rule> --> 212 208 </src> -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/Tag2LinkRuleChecker.java
r35147 r35148 27 27 import java.util.regex.Pattern; 28 28 29 import org.openstreetmap.josm.data.coor.LatLon; 29 30 import org.openstreetmap.josm.data.osm.IPrimitive; 30 31 import org.openstreetmap.josm.data.osm.Tag; … … 73 74 } 74 75 75 private static String replaceParams(String s, EvalResult eval) {76 private static String replaceParams(String s, Collection<MatchingTag> matchingTags, LatLon latLon) { 76 77 String result = s; 77 78 Matcher m = PLACEHOLDERS.matcher(s); … … 80 81 81 82 // Search for a standard value 82 String val = findValue(arg, eval.matchingTags);83 String val = findValue(arg, matchingTags); 83 84 84 85 // No standard value found: test lang() function 85 86 if (val == null) { 86 Matcher lm = LANG.matcher(arg); 87 if (lm.matches()) { 88 String josmLang = Config.getPref().get("language"); 89 String jvmLang = (josmLang.isEmpty() ? Locale.getDefault().getLanguage() : josmLang).split("_")[0]; 90 if (lm.groupCount() == 0) { 91 val = jvmLang; 92 } else { 93 for (int i = 1; i<=lm.groupCount() && val == null; i++) { 94 if (jvmLang.equals(lm.group(i))) { 95 val = jvmLang; 96 } 97 } 98 } 99 } 100 } 101 87 val = replaceLang(arg); 88 } 89 90 // No standard value found: test lat/lon 91 if (val == null && latLon != null) { 92 val = replaceLatLon(arg, latLon); 93 } 94 102 95 // Find a default value if set after ":" 103 96 if (val == null && arg.contains(":")) { 104 97 String[] vars = arg.split(":"); 105 98 for (int i = 0; val == null && i < vars.length-1; i++) { 106 val = findValue(vars[i], eval.matchingTags);99 val = findValue(vars[i], matchingTags); 107 100 } 108 101 if (val == null) { … … 135 128 } 136 129 137 private static void replaceMapParams(Map<String, String> map, EvalResult eval) { 130 private static String replaceLang(String arg) { 131 Matcher lm = LANG.matcher(arg); 132 if (lm.matches()) { 133 String josmLang = Config.getPref().get("language"); 134 String jvmLang = (josmLang.isEmpty() ? Locale.getDefault().getLanguage() : josmLang).split("_")[0]; 135 if (lm.groupCount() == 0) { 136 return jvmLang; 137 } else { 138 for (int i = 1; i <= lm.groupCount(); i++) { 139 if (jvmLang.equals(lm.group(i))) { 140 return jvmLang; 141 } 142 } 143 } 144 } 145 return null; 146 } 147 148 private static String replaceLatLon(String arg, LatLon ll) { 149 switch (arg) { 150 case "lat": return Double.toString(ll.lat()); 151 case "lon": return Double.toString(ll.lon()); 152 default: return null; 153 } 154 } 155 156 private static void replaceMapParams(Map<String, String> map, Collection<MatchingTag> matchingTags, LatLon latLon) { 138 157 for (Entry<String, String> e : map.entrySet()) { 139 158 String key = e.getKey(); 140 159 String value = e.getValue(); 141 String key2 = replaceParams(key, eval);142 String value2 = replaceParams(value, eval);160 String key2 = replaceParams(key, matchingTags, latLon); 161 String value2 = replaceParams(value, matchingTags, latLon); 143 162 if (key.equals(key2) && value.equals(value2)) { 144 163 // Nothing to do … … 154 173 } 155 174 156 private static Collection<Link> processEval(EvalResult eval, Rule rule, Source source) { 175 private static Collection<Link> processEval(EvalResult eval, Rule rule, Source source, LatLon latLon) { 157 176 Collection<Link> result = new ArrayList<>(); 158 177 if (eval.matches()) { … … 161 180 Link copy = (Link) link.clone(); 162 181 copy.name = copy.name.replaceAll("%name%", source.name); 163 copy.url = replaceParams(copy.url, eval); 182 copy.url = replaceParams(copy.url, eval.matchingTags, latLon); 164 183 if (copy instanceof LinkPost) { 165 184 LinkPost lp = (LinkPost) copy; 166 replaceMapParams(lp.headers, eval); 167 replaceMapParams(lp.params, eval); 185 replaceMapParams(lp.headers, eval.matchingTags, latLon); 186 replaceMapParams(lp.params, eval.matchingTags, latLon); 168 187 } 169 188 result.add(copy); … … 176 195 } 177 196 178 private static <T> Collection<Link> doGetLinks(BiFunction<Rule, T, EvalResult> evaluator, T obj) { 197 private static <T> Collection<Link> doGetLinks(BiFunction<Rule, T, EvalResult> evaluator, T obj, LatLon latLon) { 179 198 Collection<Link> result = new ArrayList<>(); 180 199 for (Source source : sources) { 181 200 for (Rule rule : source.rules) { 182 result.addAll(processEval(evaluator.apply(rule, obj), rule, source)); 201 result.addAll(processEval(evaluator.apply(rule, obj), rule, source, latLon)); 183 202 } 184 203 } … … 192 211 */ 193 212 public static Collection<Link> getLinks(IPrimitive p) { 194 return doGetLinks(Rule::evaluates, p); 213 return doGetLinks(Rule::evaluates, p, p.getBBox().getCenter()); 195 214 } 196 215 … … 198 217 * Replies the links relevant to the given OSM tag. 199 218 * @param tag The OSM tag 219 * @param tags The latlon center, or null 200 220 * @return the links relevant to the {@code tag}. 201 221 */ 202 public static Collection<Link> getLinks(Tag tag) { 203 return doGetLinks(Rule::evaluates, tag); 222 public static Collection<Link> getLinks(Tag tag, LatLon latLon) { 223 return doGetLinks(Rule::evaluates, tag, latLon); 204 224 } 205 225 … … 207 227 * Replies the links relevant to the given OSM tags. 208 228 * @param tags The OSM tags 229 * @param tags The latlon center, or null 209 230 * @return the links relevant to the {@code tags}. 210 231 */ 211 public static Collection<Link> getLinks(Tags tags) { 212 return doGetLinks(Rule::evaluates, tags); 232 public static Collection<Link> getLinks(Tags tags, LatLon latLon) { 233 return doGetLinks(Rule::evaluates, tags, latLon); 213 234 } 214 235 } -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/Rule.java
r35134 r35148 32 32 public final Collection<Condition> conditions = new ArrayList<>(); 33 33 public final Collection<Link> links = new ArrayList<>(); 34 34 35 35 public static class MatchingTag { 36 36 public String key; … … 46 46 } 47 47 public void addParams(Matcher m, String paramName) { 48 for (int i = 1; i <=m.groupCount(); i++) {48 for (int i = 1; i <= m.groupCount(); i++) { 49 49 params.put(prefix+paramName+"."+i, m.group(i)); 50 50 } … … 62 62 return "MatchingTag [" + (key != null ? "key=" + key + ", " : "") 63 63 + (value != null ? "value=" + value + ", " : "") 64 + (params != null ?"params=" + params + ", ": "")65 + (prefix != null ? "prefix=" + prefix : "") + "]";64 + "params=" + params + ", " 65 + (prefix != null ? "prefix=" + prefix : "") + ']'; 66 66 } 67 67 } 68 68 69 69 public static class EvalResult { 70 70 private final int conditionsNumber; … … 79 79 public String toString() { 80 80 return "EvalResult [conditionsNumber=" + conditionsNumber 81 + ", matchingTags=" + matchingTags + "]";81 + ", matchingTags=" + matchingTags + ']'; 82 82 } 83 83 } -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/PropertyPopupListener.java
r35134 r35148 16 16 package org.openstreetmap.josm.plugins.tag2link.listeners; 17 17 18 import java.util.Optional; 19 18 20 import javax.swing.JPopupMenu; 19 21 import javax.swing.event.PopupMenuEvent; 20 22 23 import org.openstreetmap.josm.data.osm.BBox; 24 import org.openstreetmap.josm.data.osm.OsmPrimitive; 21 25 import org.openstreetmap.josm.data.osm.Tags; 26 import org.openstreetmap.josm.gui.MainApplication; 22 27 import org.openstreetmap.josm.gui.MapFrame; 23 28 import org.openstreetmap.josm.plugins.tag2link.Tag2LinkRuleChecker; … … 35 40 if (tags != null) { 36 41 JPopupMenu popup = (JPopupMenu) e.getSource(); 37 for (Link link : Tag2LinkRuleChecker.getLinks(tags)) { 42 Optional<BBox> bbox = MainApplication.getLayerManager().getEditDataSet().getSelected() 43 .stream().filter(x -> x.hasTag(tags.getKey(), tags.getValues())).map(OsmPrimitive::getBBox).findAny(); 44 for (Link link : Tag2LinkRuleChecker.getLinks(tags, bbox.isPresent() ? bbox.get().getCenter() : null)) { 38 45 addLink(popup, link); 39 46 } -
applications/editors/josm/plugins/tag2link/test/unit/org/openstreetmap/josm/plugins/tag2link/Tag2LinkRuleCheckerTest.java
r35146 r35148 19 19 @Test 20 20 public void testImageCommons() { 21 Collection<Link> links = Tag2LinkRuleChecker.getLinks(new Tag("image", "File:Witten Brücke Gasstraße.jpg")); 21 Collection<Link> links = Tag2LinkRuleChecker.getLinks(new Tag("image", "File:Witten Brücke Gasstraße.jpg"), null); 22 22 assertEquals(1, links.size()); 23 23 assertEquals("https://commons.wikimedia.org/wiki/File%3AWitten_Br%C3%BCcke_Gasstra%C3%9Fe.jpg", links.iterator().next().url); 24 links = Tag2LinkRuleChecker.getLinks(new Tag("image", "category:JOSM")); 24 links = Tag2LinkRuleChecker.getLinks(new Tag("image", "category:JOSM"), null); 25 25 assertEquals(1, links.size()); 26 26 assertEquals("https://commons.wikimedia.org/wiki/category%3AJOSM", links.iterator().next().url); … … 29 29 @Test 30 30 public void testBrandWikidata() { 31 final Collection<Link> links = Tag2LinkRuleChecker.getLinks(new Tag("brand:wikidata", "Q259340")); 31 final Collection<Link> links = Tag2LinkRuleChecker.getLinks(new Tag("brand:wikidata", "Q259340"), null); 32 32 assertEquals(1, links.size()); 33 33 assertEquals("https://www.wikidata.org/wiki/Q259340", links.iterator().next().url); … … 36 36 @Test 37 37 public void testArchipelagoWikidata() { 38 final Collection<Link> links = Tag2LinkRuleChecker.getLinks(new Tag("archipelago:wikidata", "Q756987")); 38 final Collection<Link> links = Tag2LinkRuleChecker.getLinks(new Tag("archipelago:wikidata", "Q756987"), null); 39 39 assertEquals(1, links.size()); 40 40 assertEquals("https://www.wikidata.org/wiki/Q756987", links.iterator().next().url);
Note:
See TracChangeset
for help on using the changeset viewer.