Changeset 29234 in osm for applications/editors
- Timestamp:
- 2013-02-07T17:29:07+01:00 (12 years ago)
- Location:
- applications/editors/josm/plugins/utilsplugin2
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/utilsplugin2/build.xml
r29226 r29234 31 31 32 32 <!-- enter the SVN commit message --> 33 <property name="commit.message" value="Utilsplugin2: added pasting tags from text clipboard"/>33 <property name="commit.message" value="Utilsplugin2: improved pasting tags: paste from JOSM, support JSON format"/> 34 34 <!-- enter the *lowest* JOSM version this plugin is currently compatible with --> 35 35 <property name="plugin.main.version" value="4980"/> -
applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/PasteTagsExtendedAction.java
r29226 r29234 6 6 import java.util.Collection; 7 7 import java.util.List; 8 import java.util.Map; 8 9 import org.openstreetmap.josm.Main; 9 10 import org.openstreetmap.josm.actions.JosmAction; 11 import org.openstreetmap.josm.actions.PasteTagsAction; 10 12 import org.openstreetmap.josm.command.ChangePropertyCommand; 11 13 import org.openstreetmap.josm.command.Command; … … 24 26 super(tr("Paste tags [testing]"), "pastetags", tr("Apply tags parsed from buffer to all selected items.."), 25 27 Shortcut.registerShortcut("tools:pastetags", tr("Tool: {0}", tr("Paste tags")), 26 KeyEvent.VK_T, Shortcut.CTRL), true ); // TODO: shortcut is temporary, will be on Ctrl-Shift-V28 KeyEvent.VK_T, Shortcut.CTRL), true, "textpastetags", false); // TODO: shortcut is temporary, will be on Ctrl-Shift-V 27 29 //putValue("help", ht("/Action/Paste")); 28 30 } … … 39 41 String buf = Utils.getClipboardContent(); 40 42 41 if (buf==null) return;42 43 List<Command> commands = new ArrayList<Command>(); 43 for (Tag tag: TextTagParser.readTagsFromText(buf)) { 44 commands.add(new ChangePropertyCommand(selection, tag.getKey(), "".equals(tag.getValue())?null:tag.getValue())); 44 if (buf==null || buf.matches("(\\d+,)*\\d+")) { // Paste tags from JOSM buffer 45 PasteTagsAction.TagPaster tagPaster = new PasteTagsAction.TagPaster(Main.pasteBuffer.getDirectlyAdded(), selection); 46 for (Tag tag: tagPaster.execute()) { 47 commands.add(new ChangePropertyCommand(selection, tag.getKey(), "".equals(tag.getValue())?null:tag.getValue())); 48 } 49 } else { // Paste tags from arbitrary text 50 Map<String, String> tags = TextTagParser.readTagsFromText(buf); 51 if (tags==null) return; 52 String v; 53 for (String key: tags.keySet()) { 54 v = tags.get(key); 55 commands.add(new ChangePropertyCommand(selection, key, "".equals(v)?null:v)); 56 } 45 57 } 46 58 if (!commands.isEmpty()) { -
applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/customurl/URLList.java
r28028 r29234 28 28 items.add("Wikipedia RU"); 29 29 items.add(defaultURL); 30 items.add("LatLon buildings"); 31 items.add("http://latlon.org/buildings?zoom=17&lat={#lat}&lon={#lon}&layers=B"); 32 items.add("AMDMi3 Russian streets"); 33 items.add("http://addresses.amdmi3.ru/?zoom=11&lat={#lat}&lon={#lon}&layers=B00"); 34 items.add("Mapki - More History with CT"); 35 items.add("http://osm.mapki.com/history/{#type}.php?id={#id}"); 30 items.add("Who Dit It?"); 31 items.add("http://simon04.dev.openstreetmap.org/whodidit/?zoom=12&lat={#lat}&lon={#lat}&layers=BTT"); 32 items.add("Keep Right validator"); 33 items.add("http://keepright.ipax.at/report_map.php?zoom=14&lat={#lat}&lon={#lat}&layers=B0T"); 36 34 items.add("Element history [demo, =Ctrl-Shift-H]"); 37 35 items.add("http://www.openstreetmap.org/browse/{#type}/{#id}/history"); -
applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/helper/TextTagParser.java
r29226 r29234 1 1 package org.openstreetmap.josm.plugins.utilsplugin2.helper; 2 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.List; 3 import java.util.HashMap; 4 import java.util.Map; 6 5 import java.util.regex.Matcher; 7 6 import java.util.regex.Pattern; 8 import org.openstreetmap.josm.data.osm.Tag;9 7 10 8 public class TextTagParser { … … 26 24 } 27 25 28 /** 29 * Read tags from format, tag1\t val1 \n tag2 \t vat2 30 * if possible 31 */ 32 Collection<Tag> getFormattedTags() { 33 String lines[] = data.split("\n"); 34 35 Pattern p = Pattern.compile("(.*?)\t(.*?)"); 36 List<Tag> tags = new ArrayList<Tag>(); 37 String k=null, v=null; 38 for (String line: lines) { 39 if (line.trim().isEmpty()) continue; // skiip empty lines 40 Matcher m = p.matcher(line); 41 if (m.matches()) { 42 k=m.group(1).trim(); v=m.group(2).trim(); 43 tags.add(new Tag(k,v)); 44 } else { 45 tags.clear(); 46 break; 47 } 48 } 49 if (!tags.isEmpty()) { 50 return tags; 51 } else { 52 return null; 53 } 54 } 55 26 56 27 /** 57 28 * Read tags from "Free format" 58 29 */ 59 Collection<Tag> getParsedTags() {30 Map<String, String> getFreeParsedTags() { 60 31 String k, v; 61 List<Tag> tags = new ArrayList<Tag>();32 Map<String, String> tags = new HashMap<String,String>(); 62 33 63 34 while (true) { … … 77 48 } 78 49 v = parseString(false); 79 tags. add(new Tag(k, v));50 tags.put(k, v); 80 51 } 81 52 return tags; … … 146 117 } 147 118 148 public static Collection<Tag> readTagsFromText(String buf) { 119 private static String unescape(String k) { 120 if(! (k.startsWith("\"") && k.endsWith("\"")) ) { 121 if (k.contains("=")) { 122 // '=' not in quotes will be treated as an error! 123 return null; 124 } else { 125 return k; 126 } 127 } 128 String text = k.substring(1,k.length()-1); 129 return (new TextTagParser(text)).parseString(false); 130 } 131 132 /** 133 * Try to find tag-value pairs in given @param text 134 * @param splitRegex - text is splitted into parts with this delimiter 135 * @param tagRegex - each part is matched against this regex 136 * @param unescapeTextInQuotes - if true, matched tag and value will be analyzed more thoroughly 137 */ 138 public static Map<String, String> readTagsByRegexp(String text, String splitRegex, String tagRegex, boolean unescapeTextInQuotes) { 139 String lines[] = text.split(splitRegex); 140 Pattern p = Pattern.compile(tagRegex); 141 Map<String, String> tags = new HashMap<String,String>(); 142 String k=null, v=null; 143 for (String line: lines) { 144 if (line.trim().isEmpty()) continue; // skiip empty lines 145 Matcher m = p.matcher(line); 146 if (m.matches()) { 147 k=m.group(1).trim(); v=m.group(2).trim(); 148 if (unescapeTextInQuotes) { 149 k = unescape(k); 150 v = unescape(v); 151 if (k==null || v==null) return null; 152 } 153 tags.put(k,v); 154 } else { 155 return null; 156 } 157 } 158 if (!tags.isEmpty()) { 159 return tags; 160 } else { 161 return null; 162 } 163 } 164 165 public static Map<String,String> readTagsFromText(String buf) { 166 Map<String,String> tags; 167 168 // Format 169 // tag1\tval1\ntag2\tval2\n 170 tags = readTagsByRegexp(buf, "[\r\n]+]", "(.*?)\t(.*?)", false); 171 // try "tag\tvalue\n" format 172 if (tags!=null) return tags; 173 174 // Format 175 // a=b \n c=d \n "a b"=hello 176 // SORRY: "a=b" = c is not supported fror now, only first = will be considered 177 // a = "b=c" is OK 178 // a = b=c - this method of parsing fails intentionally 179 tags = readTagsByRegexp(buf, "[\\n\\t\\r]+", "(.*?)=(.*?)", true); 180 // try format t1=v1\n t2=v2\n ... 181 if (tags!=null) return tags; 182 183 // JSON-format 184 String bufJson = buf.trim(); 185 if (bufJson.startsWith("{") && bufJson.endsWith("}") ) bufJson = bufJson.substring(1,bufJson.length()-1); 186 tags = readTagsByRegexp(bufJson, "[\\s]*,[\\s]*", 187 "[\\s]*(\\\".*?[^\\\\]\\\")"+"[\\s]*:[\\s]*"+"(\\\".*?[^\\\\]\\\")[\\s]*", true); 188 if (tags!=null) return tags; 189 190 // Free format 191 // a 1 "b" 2 c=3 d 4 e "5" 149 192 TextTagParser parser = new TextTagParser(buf); 150 Collection<Tag> tags; 151 tags = parser.getFormattedTags(); // try "tag\tvalue\n" format 152 if (tags == null) { 153 tags = parser.getParsedTags(); 154 } 193 System.out.println("free"); 194 tags = parser.getFreeParsedTags(); 155 195 return tags; 156 196
Note:
See TracChangeset
for help on using the changeset viewer.