Changeset 35134 in osm for applications/editors/josm
- Timestamp:
- 2019-09-26T18:17:23+02:00 (5 years ago)
- Location:
- applications/editors/josm/plugins/tag2link
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/tag2link/build.xml
r34559 r35134 1 <?xml version="1.0" encoding="utf-8"?>1 <?xml version="1.0" encoding="utf-8"?> 2 2 <project name="tag2link" default="dist" basedir="."> 3 3 … … 5 5 <property name="commit.message" value="Commit message"/> 6 6 <!-- enter the *lowest* JOSM version this plugin is currently compatible with --> 7 <property name="plugin.main.version" value="1 2847"/>7 <property name="plugin.main.version" value="15376"/> 8 8 9 9 <property name="plugin.author" value="Don-vip & FrViPofm"/> -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/Tag2LinkRuleChecker.java
r34599 r35134 23 23 import java.util.Locale; 24 24 import java.util.Map; 25 import java.util.function.BiFunction; 25 26 import java.util.regex.Matcher; 26 27 import java.util.regex.Pattern; … … 28 29 import org.openstreetmap.josm.data.osm.IPrimitive; 29 30 import org.openstreetmap.josm.data.osm.Tag; 31 import org.openstreetmap.josm.data.osm.Tags; 30 32 import org.openstreetmap.josm.plugins.tag2link.data.Link; 31 33 import org.openstreetmap.josm.plugins.tag2link.data.LinkPost; … … 48 50 49 51 private static boolean initialized = false; 50 52 53 private static final Pattern PLACEHOLDERS = Pattern.compile("%([^%]*)%"); 54 private static final Pattern LANG = Pattern.compile(".*lang(?:\\((\\p{Lower}{2,})(?:,(\\p{Lower}{2,}))*\\))?.*"); 55 51 56 /** 52 57 * Initializes the matching rules mechanism. … … 67 72 return null; 68 73 } 69 74 70 75 private static String replaceParams(String s, EvalResult eval) { 71 76 String result = s; 72 Matcher m = P attern.compile("%([^%]*)%").matcher(s);77 Matcher m = PLACEHOLDERS.matcher(s); 73 78 while (m.find()) { 74 79 String arg = m.group(1); … … 79 84 // No standard value found: test lang() function 80 85 if (val == null) { 81 Matcher lm = Pattern.compile(".*lang(?:\\((\\p{Lower}{2,})(?:,(\\p{Lower}{2,}))*\\))?.*").matcher(arg);86 Matcher lm = LANG.matcher(arg); 82 87 if (lm.matches()) { 83 88 String josmLang = Config.getPref().get("language"); … … 107 112 } 108 113 109 // Has a value been found 114 // Has a value been found? 110 115 if (val != null) { 111 116 try { … … 114 119 val = val.replaceAll(" ", "_"); 115 120 } 116 // Encode param to be included in the URL, except if it is the URL itself 121 // Encode param to be included in the URL, except if it is the URL itself! 117 122 if (!m.group().equals(s)) { 118 123 val = URLEncoder.encode(val, UTF8_ENCODING); … … 124 129 } 125 130 } else { 126 System.err.println("Invalid argument: "+arg);131 Logging.error("Invalid argument: " + arg); 127 132 } 128 133 } … … 164 169 result.add(copy); 165 170 } catch (CloneNotSupportedException e) { 166 System.err.println(e);171 Logging.error(e); 167 172 } 168 173 } … … 170 175 return result; 171 176 } 172 177 178 private static <T> Collection<Link> doGetLinks(BiFunction<Rule, T, EvalResult> evaluator, T obj) { 179 Collection<Link> result = new ArrayList<>(); 180 for (Source source : sources) { 181 for (Rule rule : source.rules) { 182 result.addAll(processEval(evaluator.apply(rule, obj), rule, source)); 183 } 184 } 185 return result; 186 } 187 173 188 /** 174 189 * Replies the links relevant to the given OSM primitive. … … 177 192 */ 178 193 public static Collection<Link> getLinks(IPrimitive p) { 179 Collection<Link> result = new ArrayList<>(); 180 for (Source source : sources) { 181 for (Rule rule : source.rules) { 182 result.addAll(processEval(rule.evaluates(p), rule, source)); 183 } 184 } 185 return result; 194 return doGetLinks(Rule::evaluates, p); 186 195 } 187 196 … … 192 201 */ 193 202 public static Collection<Link> getLinks(Tag tag) { 194 Collection<Link> result = new ArrayList<>(); 195 for (Source source : sources) { 196 for (Rule rule : source.rules) { 197 result.addAll(processEval(rule.evaluates(tag), rule, source)); 198 } 199 } 200 return result; 203 return doGetLinks(Rule::evaluates, tag); 204 } 205 206 /** 207 * Replies the links relevant to the given OSM tags. 208 * @param tags The OSM tags 209 * @return the links relevant to the {@code tags}. 210 */ 211 public static Collection<Link> getLinks(Tags tags) { 212 return doGetLinks(Rule::evaluates, tags); 201 213 } 202 214 } -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/Condition.java
r28547 r35134 40 40 public String id; 41 41 42 /* (non-Javadoc)43 * @see java.lang.Object#toString()44 */45 42 @Override 46 43 public String toString() { -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/Link.java
r32698 r35134 45 45 } 46 46 47 /* (non-Javadoc)48 * @see java.lang.Object#toString()49 */50 47 @Override 51 48 public String toString() { … … 53 50 } 54 51 55 /* (non-Javadoc)56 * @see java.lang.Object#clone()57 */58 52 @Override 59 53 public Link clone() throws CloneNotSupportedException { -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/LinkPost.java
r30717 r35134 59 59 } 60 60 61 /* (non-Javadoc)62 * @see org.openstreetmap.josm.plugins.tag2link.data.Link#containsParams()63 */64 61 @Override 65 62 public boolean containsParams() { … … 67 64 } 68 65 69 /* (non-Javadoc)70 * @see org.openstreetmap.josm.plugins.tag2link.data.Link#clone()71 */72 66 @Override 73 67 public LinkPost clone() throws CloneNotSupportedException { -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/Rule.java
r30717 r35134 16 16 package org.openstreetmap.josm.plugins.tag2link.data; 17 17 18 import static java.util.Collections.singleton; 19 18 20 import java.util.ArrayList; 19 21 import java.util.Collection; 20 22 import java.util.HashMap; 21 23 import java.util.Map; 24 import java.util.function.Function; 22 25 import java.util.regex.Matcher; 23 26 24 27 import org.openstreetmap.josm.data.osm.IPrimitive; 25 28 import org.openstreetmap.josm.data.osm.Tag; 29 import org.openstreetmap.josm.data.osm.Tags; 26 30 27 31 public class Rule { … … 54 58 } 55 59 } 56 /* (non-Javadoc)57 * @see java.lang.Object#toString()58 */59 60 @Override 60 61 public String toString() { … … 75 76 return conditionsNumber > 0 && matchingTags.size() >= conditionsNumber; 76 77 } 77 /* (non-Javadoc)78 * @see java.lang.Object#toString()79 */80 78 @Override 81 79 public String toString() { … … 84 82 } 85 83 } 86 87 public EvalResult evaluates( Map<String, String> tags) {84 85 public EvalResult evaluates(Iterable<String> keys, Function<String, Iterable<String>> valuesFn) { 88 86 EvalResult result = new EvalResult(conditions.size()); 89 87 for (Condition c : conditions) { 90 for (String key : tags.keySet()) {88 for (String key : keys) { 91 89 Matcher keyMatcher = c.keyPattern.matcher(key); 92 90 if (keyMatcher.matches()) { 93 91 String idPrefix = c.id == null ? "" : c.id+"."; 94 MatchingTag tag = new MatchingTag(key, tags.get(key), idPrefix); 95 tag.addParams(keyMatcher, "k"); 96 boolean matchingTag = true; 97 if (c.valPattern != null) { 98 Matcher valMatcher = c.valPattern.matcher(tag.value); 99 if (valMatcher.matches()) { 100 tag.addParams(valMatcher, "v"); 101 } else { 102 matchingTag = false; 92 for (String value : valuesFn.apply(key)) { 93 MatchingTag tag = new MatchingTag(key, value, idPrefix); 94 tag.addParams(keyMatcher, "k"); 95 boolean matchingTag = true; 96 if (c.valPattern != null) { 97 Matcher valMatcher = c.valPattern.matcher(tag.value); 98 if (valMatcher.matches()) { 99 tag.addParams(valMatcher, "v"); 100 } else { 101 matchingTag = false; 102 } 103 103 } 104 }105 if (matchingTag) {106 result.matchingTags.add(tag);104 if (matchingTag) { 105 result.matchingTags.add(tag); 106 } 107 107 } 108 108 } … … 111 111 return result; 112 112 } 113 113 114 114 public EvalResult evaluates(IPrimitive p) { 115 return evaluates(p. getKeys());115 return evaluates(p.keySet(), key -> singleton(p.get(key))); 116 116 } 117 117 118 118 public EvalResult evaluates(Tag tag) { 119 Map<String, String> map = new HashMap<>(); 120 map.put(tag.getKey(), tag.getValue()); 121 return evaluates(map); 119 return evaluates(singleton(tag.getKey()), x -> singleton(tag.getValue())); 122 120 } 123 121 124 /* (non-Javadoc) 125 * @see java.lang.Object#toString() 126 */ 122 public EvalResult evaluates(Tags tags) { 123 return evaluates(singleton(tags.getKey()), x -> tags.getValues()); 124 } 125 127 126 @Override 128 127 public String toString() { 129 return "Rule [conditions=" + conditions + ", links=" + links + "]";128 return "Rule [conditions=" + conditions + ", links=" + links + ']'; 130 129 } 131 130 } -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/Source.java
r30717 r35134 43 43 } 44 44 45 /* (non-Javadoc)46 * @see java.lang.Object#toString()47 */48 45 @Override 49 46 public String toString() { -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/PropertyPopupListener.java
r28547 r35134 19 19 import javax.swing.event.PopupMenuEvent; 20 20 21 import org.openstreetmap.josm.data.osm.Tag ;21 import org.openstreetmap.josm.data.osm.Tags; 22 22 import org.openstreetmap.josm.gui.MapFrame; 23 23 import org.openstreetmap.josm.plugins.tag2link.Tag2LinkRuleChecker; … … 32 32 @Override 33 33 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 34 Tag tag = frame.propertiesDialog.getSelectedProperty();35 if (tag != null) {34 Tags tags = frame.propertiesDialog.getSelectedProperties(); 35 if (tags != null) { 36 36 JPopupMenu popup = (JPopupMenu) e.getSource(); 37 for (Link link : Tag2LinkRuleChecker.getLinks(tag )) {37 for (Link link : Tag2LinkRuleChecker.getLinks(tags)) { 38 38 addLink(popup, link); 39 39 }
Note:
See TracChangeset
for help on using the changeset viewer.