Ignore:
Timestamp:
2013-02-07T17:29:07+01:00 (11 years ago)
Author:
akks
Message:

Utilsplugin2: improved pasting tags: paste from JOSM, support JSON format

Location:
applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/PasteTagsExtendedAction.java

    r29226 r29234  
    66import java.util.Collection;
    77import java.util.List;
     8import java.util.Map;
    89import org.openstreetmap.josm.Main;
    910import org.openstreetmap.josm.actions.JosmAction;
     11import org.openstreetmap.josm.actions.PasteTagsAction;
    1012import org.openstreetmap.josm.command.ChangePropertyCommand;
    1113import org.openstreetmap.josm.command.Command;
     
    2426        super(tr("Paste tags [testing]"), "pastetags", tr("Apply tags parsed from buffer to all selected items.."),
    2527            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-V
     28            KeyEvent.VK_T, Shortcut.CTRL), true, "textpastetags", false); // TODO: shortcut is temporary, will be on Ctrl-Shift-V
    2729        //putValue("help", ht("/Action/Paste"));
    2830    }
     
    3941        String buf = Utils.getClipboardContent();
    4042
    41         if (buf==null) return;
    4243        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            }
    4557        }
    4658        if (!commands.isEmpty()) {
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/customurl/URLList.java

    r28028 r29234  
    2828        items.add("Wikipedia RU");
    2929        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");
    3634        items.add("Element history [demo, =Ctrl-Shift-H]");
    3735        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  
    11package org.openstreetmap.josm.plugins.utilsplugin2.helper;
    22
    3 import java.util.ArrayList;
    4 import java.util.Collection;
    5 import java.util.List;
     3import java.util.HashMap;
     4import java.util.Map;
    65import java.util.regex.Matcher;
    76import java.util.regex.Pattern;
    8 import org.openstreetmap.josm.data.osm.Tag;
    97
    108public class TextTagParser {
     
    2624    }
    2725   
    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   
    5627    /**
    5728     * Read tags from "Free format"
    5829     */
    59     Collection<Tag> getParsedTags() {
     30    Map<String, String>  getFreeParsedTags() {
    6031        String k, v;
    61         List<Tag> tags = new ArrayList<Tag>();
     32        Map<String, String> tags = new HashMap<String,String>();
    6233
    6334        while (true) {
     
    7748            }
    7849            v = parseString(false);
    79             tags.add(new Tag(k, v));
     50            tags.put(k, v);
    8051        }
    8152        return tags;
     
    146117    }
    147118
    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"
    149192        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();
    155195        return tags;
    156196
Note: See TracChangeset for help on using the changeset viewer.