Ignore:
Timestamp:
2013-02-10T09:31:02+01:00 (11 years ago)
Author:
akks
Message:

Utilsplugin2: validation for pasting tags

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

Legend:

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

    r29239 r29241  
    4848            }
    4949        } else { // Paste tags from arbitrary text
    50             Map<String, String> tags = TextTagParser.readTagsFromText(buf);
     50            Map<String, String> tags = TextTagParser.getTagsFromText(buf);
    5151            if (tags==null) return;
    5252            String v;
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/helper/TextTagParser.java

    r29234 r29241  
    55import java.util.regex.Matcher;
    66import java.util.regex.Pattern;
     7import javax.swing.JOptionPane;
     8import org.openstreetmap.josm.Main;
     9import org.openstreetmap.josm.gui.ExtendedDialog;
     10import org.openstreetmap.josm.io.XmlWriter;
     11
     12import static org.openstreetmap.josm.tools.I18n.tr;
    713
    814public class TextTagParser {
     
    163169    }
    164170 
     171    public static Map<String,String> getTagsFromText(String buf) {
     172        Map<String,String> tags = readTagsFromText(buf);
     173        return validateTags(tags) ? tags : null;
     174    }
     175   
    165176    public static Map<String,String> readTagsFromText(String buf) {
    166177        Map<String,String> tags;
     
    191202        // a 1 "b" 2 c=3 d 4 e "5"
    192203        TextTagParser parser = new TextTagParser(buf);
    193         System.out.println("free");
    194204        tags = parser.getFreeParsedTags();
    195205        return tags;
    196 
    197     }
    198 
    199 
    200    
     206    }
     207
     208    private static boolean validateTags(Map<String, String> tags) {
     209        String value;
     210        int r;
     211        if (tags.size()>30) {
     212            r=warning(tr("There was {0} tags found in the buffer, it is suspicious!",tags.size()), "", "toomanytags");
     213            if (r==2) return false; if (r==3) return true;
     214        }
     215        for (String key: tags.keySet()) {
     216            value = tags.get(key);
     217            if (key.length()>50) {
     218                r = warning(tr("Key is too long:"), key+"="+value, "keytoolong");
     219                if (r==2) return false; if (r==3) return true;
     220            }
     221            if (!key.matches("[a-zA-Z:_]*")) {
     222                r = warning(tr("Suspiciouns characters in tag:"), key, "keydoesnotmatch");
     223                if (r==2) return false; if (r==3) return true;
     224            }
     225            if (value.length()>255) {
     226                r= warning(tr("Value too long (max 255 characters):"), value, "valuetoolong");
     227                if (r==2) return false; if (r==3) return true;
     228            }
     229        }
     230        return true;
     231    }
     232   
     233    private static int warning(String text, String data, String code) {
     234        ExtendedDialog ed = new ExtendedDialog(
     235                    Main.parent,
     236                    tr("Do you want to paste these tags?"),
     237                    new String[]{tr("Ok"), tr("Cancel"), tr("Ingore warnings")});
     238        ed.setButtonIcons(new String[]{"ok.png", "cancel.png", "pastetags.png"});
     239        ed.setContent("<html><b>"+text + "</b><br/><br/> <div width=\"300px\">"+XmlWriter.encode(data,true)+"</html>");
     240        ed.setDefaultButton(2);
     241        ed.setCancelButton(2);
     242        ed.setIcon(JOptionPane.WARNING_MESSAGE);
     243        ed.toggleEnable(code);
     244        ed.showDialog();
     245        Object o = ed.getValue();
     246        if (o instanceof Integer)
     247            return ((Integer)o).intValue();
     248        else
     249            return 2;
     250    }
    201251}
Note: See TracChangeset for help on using the changeset viewer.