Changeset 26921 in osm for applications/editors
- Timestamp:
- 2011-10-21T23:05:50+02:00 (13 years ago)
- Location:
- applications/editors/josm/plugins/tag2link
- Files:
-
- 9 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/tag2link/resources/tag2link_sources.xml
r26918 r26921 31 31 </rule> 32 32 </source> 33 34 35 36 <condition k="wikipedia(?::([\p{Lower}]{2,}))?" />37 <link name="View %name% article" href="http://%k.1: en%.wikipedia.org/wiki/%v%" />38 39 40 41 33 34 <source name="Wikipedia"> 35 <rule> 36 <condition k="wikipedia(?::([\p{Lower}]{2,}))?" v="(?:([\p{Lower}]{2,}):)?(.*)" /> 37 <link name="View %name% article" href="http://%k.1:v.1:en%.wikipedia.org/wiki/%v.2:v.1%" /> 38 </rule> 39 </source> 40 41 <!-- Only for France --> 42 42 43 43 <source name="SANDRE" country-code="FR"> -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/Tag2LinkConstants.java
r26917 r26921 31 31 * File encoding. 32 32 */ 33 public static final String ENCODING = "UTF-8";33 public static final String UTF8_ENCODING = "UTF-8"; 34 34 35 35 /** -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/Tag2LinkRuleChecker.java
r26918 r26921 16 16 package org.openstreetmap.josm.plugins.tag2link; 17 17 18 import java.io.UnsupportedEncodingException; 19 import java.net.URLEncoder; 18 20 import java.util.ArrayList; 19 21 import java.util.Collection; 22 import java.util.regex.Matcher; 23 import java.util.regex.Pattern; 20 24 21 import org.openstreetmap.josm.data.osm. OsmPrimitive;25 import org.openstreetmap.josm.data.osm.IPrimitive; 22 26 import org.openstreetmap.josm.plugins.tag2link.data.Link; 23 27 import org.openstreetmap.josm.plugins.tag2link.data.Rule; … … 40 44 } 41 45 42 public static Collection<Link> getLinks(OsmPrimitive p) { 46 private static String findValue(String arg, Collection<MatchingTag> matchingTags) { 47 for (MatchingTag tag : matchingTags) { 48 if (tag.params.containsKey(arg)) { 49 return tag.params.get(arg); 50 } 51 } 52 return null; 53 } 54 55 public static Collection<Link> getLinks(IPrimitive p) { 43 56 Collection<Link> result = new ArrayList<Link>(); 44 57 for (Source source : sources) { … … 49 62 Link copy = new Link(link); 50 63 copy.name = copy.name.replaceAll("%name%", source.name); 51 MatchingTag firstTag = eval.matchingTags.iterator().next(); 52 copy.url = copy.url.replaceAll("%k%", firstTag.key) 53 .replaceAll("%v%", firstTag.value); 64 Matcher m = Pattern.compile("%([^%]*)%").matcher(copy.url); 65 while (m.find()) { 66 String arg = m.group(1); 67 String val = findValue(arg, eval.matchingTags); 68 if (val == null && arg.contains(":")) { 69 String[] vars = arg.split(":"); 70 for (int i = 0; val == null && i < vars.length-1; i++) { 71 val = findValue(vars[i], eval.matchingTags); 72 } 73 if (val == null) { 74 // Default value 75 val = vars[vars.length-1]; 76 } 77 } 78 if (val != null) { 79 try { 80 // Special hack for Wikipedia that prevents spaces being replaced by "+" characters, but by "_" 81 if (copy.url.contains("wikipedia.")) { 82 val = val.replaceAll(" ", "_"); 83 } 84 // Encode param to be included in the URL, except if it is the URL itself ! 85 if (!m.group().equals(copy.url)) { 86 val = URLEncoder.encode(val, UTF8_ENCODING); 87 } 88 // Finally replace parameter 89 copy.url = copy.url.replaceFirst(m.group(), val); 90 } catch (UnsupportedEncodingException e) { 91 e.printStackTrace(); 92 } 93 } else { 94 System.err.println("Invalid argument: "+arg); 95 } 96 } 54 97 result.add(copy); 55 98 } -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/action/OpenLinkAction.java
r26918 r26921 16 16 17 17 public OpenLinkAction(Link link) { 18 super(tr(link.name), ICON_24, tr("Launch browser with information about the selected object"), null, true);18 super(tr(link.name), ICON_24, tr("Launch browser with information about the selected object"), null, false); 19 19 this.url = link.url; 20 20 } … … 22 22 @Override 23 23 public void actionPerformed(ActionEvent e) { 24 System.out.println("Opening "+url); 24 25 OpenBrowser.displayUrl(url); 25 26 } -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/data/Rule.java
r26918 r26921 7 7 import java.util.regex.Matcher; 8 8 9 import org.openstreetmap.josm.data.osm. OsmPrimitive;9 import org.openstreetmap.josm.data.osm.IPrimitive; 10 10 11 11 public class Rule { … … 16 16 public String key; 17 17 public String value; 18 public final Map<String, String> params = new HashMap<String, String>(); 19 public MatchingTag(String key, String value) { 18 public final Map<String, String> params; 19 private String prefix; 20 public MatchingTag(String key, String value, String prefix) { 20 21 this.key = key; 21 22 this.value = value; 23 this.params = new HashMap<String, String>(); 24 this.prefix = prefix; 25 addKeyValueParams(); 22 26 } 23 public void addParams(Matcher m, String p refix) {27 public void addParams(Matcher m, String paramName) { 24 28 for (int i = 1; i<=m.groupCount(); i++) { 25 this.params.put(prefix+i, m.group(i));29 params.put(prefix+paramName+"."+i, m.group(i)); 26 30 } 31 } 32 private void addKeyValueParams() { 33 params.put("k", key); 34 params.put("v", value); 35 if (!prefix.isEmpty()) { 36 params.put(prefix+"k", key); 37 params.put(prefix+"v", value); 38 } 27 39 } 28 40 } … … 35 47 } 36 48 37 public EvalResult evaluates( OsmPrimitive p) {49 public EvalResult evaluates(IPrimitive p) { 38 50 EvalResult result = new EvalResult(); 39 51 Map<String, String> tags = p.getKeys(); … … 43 55 if (keyMatcher.matches()) { 44 56 String idPrefix = c.id == null ? "" : c.id+"."; 45 MatchingTag tag = new MatchingTag(key, tags.get(key) );46 tag.addParams(keyMatcher, idPrefix+"k.");57 MatchingTag tag = new MatchingTag(key, tags.get(key), idPrefix); 58 tag.addParams(keyMatcher, "k"); 47 59 boolean matchingTag = true; 48 60 if (c.valPattern != null) { 49 61 Matcher valMatcher = c.valPattern.matcher(tag.value); 50 62 if (valMatcher.matches()) { 51 tag.addParams(valMatcher, idPrefix+"v.");63 tag.addParams(valMatcher, "v"); 52 64 } else { 53 65 matchingTag = false; -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/io/SourcesReader.java
r26918 r26921 37 37 try { 38 38 InputStream is = SourcesReader.class.getResourceAsStream(XML_LOCATION); 39 InputStreamReader ir = UTFInputStreamReader.create(is, ENCODING);39 InputStreamReader ir = UTFInputStreamReader.create(is, UTF8_ENCODING); 40 40 XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(ir); 41 41 result.addAll(new SourcesReader(parser).parseDoc()); -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/AbstractIPrimitivePopupListener.java
r26917 r26921 19 19 import javax.swing.event.PopupMenuEvent; 20 20 21 import org.openstreetmap.josm.data.osm. OsmPrimitive;21 import org.openstreetmap.josm.data.osm.IPrimitive; 22 22 import org.openstreetmap.josm.gui.MapFrame; 23 23 import org.openstreetmap.josm.plugins.tag2link.Tag2LinkRuleChecker; 24 24 import org.openstreetmap.josm.plugins.tag2link.data.Link; 25 25 26 public abstract class Abstract OsmPrimitivePopupListener extends AbstractPopupListener {26 public abstract class AbstractIPrimitivePopupListener extends AbstractPopupListener { 27 27 28 protected Abstract OsmPrimitivePopupListener(MapFrame frame) {28 protected AbstractIPrimitivePopupListener(MapFrame frame) { 29 29 super(frame); 30 30 } 31 31 32 protected abstract OsmPrimitive getFirstSelectedPrimitive();32 protected abstract IPrimitive getFirstSelectedPrimitive(); 33 33 34 34 @Override 35 35 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 36 OsmPrimitive p = getFirstSelectedPrimitive();36 IPrimitive p = getFirstSelectedPrimitive(); 37 37 if (p != null) { 38 38 JPopupMenu popup = (JPopupMenu) e.getSource(); -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/MembershipPopupListener.java
r26917 r26921 16 16 package org.openstreetmap.josm.plugins.tag2link.listeners; 17 17 18 import org.openstreetmap.josm.data.osm. OsmPrimitive;18 import org.openstreetmap.josm.data.osm.IPrimitive; 19 19 import org.openstreetmap.josm.gui.MapFrame; 20 20 21 public class MembershipPopupListener extends Abstract OsmPrimitivePopupListener {21 public class MembershipPopupListener extends AbstractIPrimitivePopupListener { 22 22 23 23 public MembershipPopupListener(MapFrame frame) { … … 26 26 27 27 @Override 28 protected OsmPrimitive getFirstSelectedPrimitive() { 29 // TODO Auto-generated method stub 30 return null; 28 protected IPrimitive getFirstSelectedPrimitive() { 29 return frame.propertiesDialog.getSelectedMembershipRelations().iterator().next(); 31 30 } 32 31 } -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/RelationPopupListener.java
r26917 r26921 16 16 package org.openstreetmap.josm.plugins.tag2link.listeners; 17 17 18 import org.openstreetmap.josm.data.osm. OsmPrimitive;18 import org.openstreetmap.josm.data.osm.IPrimitive; 19 19 import org.openstreetmap.josm.gui.MapFrame; 20 20 21 public class RelationPopupListener extends Abstract OsmPrimitivePopupListener {21 public class RelationPopupListener extends AbstractIPrimitivePopupListener { 22 22 23 23 public RelationPopupListener(MapFrame frame) { … … 26 26 27 27 @Override 28 protected OsmPrimitive getFirstSelectedPrimitive() {28 protected IPrimitive getFirstSelectedPrimitive() { 29 29 return frame.relationListDialog.getSelectedRelations().iterator().next(); 30 30 } -
applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/listeners/SelectionPopupListener.java
r26917 r26921 16 16 package org.openstreetmap.josm.plugins.tag2link.listeners; 17 17 18 import org.openstreetmap.josm.data.osm. OsmPrimitive;18 import org.openstreetmap.josm.data.osm.IPrimitive; 19 19 import org.openstreetmap.josm.gui.MapFrame; 20 20 21 public class SelectionPopupListener extends Abstract OsmPrimitivePopupListener {21 public class SelectionPopupListener extends AbstractIPrimitivePopupListener { 22 22 23 23 public SelectionPopupListener(MapFrame frame) { … … 26 26 27 27 @Override 28 protected OsmPrimitive getFirstSelectedPrimitive() {28 protected IPrimitive getFirstSelectedPrimitive() { 29 29 return frame.selectionListDialog.getSelectedPrimitives().iterator().next(); 30 30 }
Note:
See TracChangeset
for help on using the changeset viewer.