Ticket #11505: 0001-Load-a-range-of-OSM-objects-using-a-hyphen-as-a-sepa.patch

File 0001-Load-a-range-of-OSM-objects-using-a-hyphen-as-a-sepa.patch, 7.0 KB (added by windu.2b, 9 years ago)
  • src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java

    From bdbae0690eb4cb22e05d1d072624066e90bc9b84 Mon Sep 17 00:00:00 2001
    From: Francescu Garoby <windu.2b@gmail.com>
    Date: Sun, 31 May 2015 16:45:11 +0200
    Subject: [PATCH] Load a range of OSM objects, using a hyphen as a separator
    
    Using a hyphen as a separator, range of objects IDs can be loaded.
    It is easier to write "w1-5" than "w1, w2, w3, w4, w5", and the result
    is exactly the same.
    ---
     .../josm/data/osm/SimplePrimitiveId.java              | 19 ++++++++++++++++---
     .../josm/gui/dialogs/OsmIdSelectionDialog.java        |  7 +++++--
     .../josm/gui/widgets/OsmIdTextField.java              |  6 +++++-
     .../josm/data/osm/SimplePrimitiveIdTest.groovy        | 12 ++++++++++++
     4 files changed, 38 insertions(+), 6 deletions(-)
    
    diff --git a/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java b/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java
    index 6bf4779..0e29563 100644
    a b public class SimplePrimitiveId implements PrimitiveId, Serializable {  
    1616
    1717    public static final Pattern ID_PATTERN = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)[ /]?)(\\d+)");
    1818
     19    public static final Pattern MULTIPLE_IDS_PATTERN = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)/?)(\\d+)(-(\\d+))?");
     20
    1921    public SimplePrimitiveId(long id, OsmPrimitiveType type) {
    2022        this.id = id;
    2123        this.type = type;
    public class SimplePrimitiveId implements PrimitiveId, Serializable {  
    9799     */
    98100    public static List<SimplePrimitiveId> fuzzyParse(String s) {
    99101        final List<SimplePrimitiveId> ids = new ArrayList<>();
    100         final Matcher m = ID_PATTERN.matcher(s);
     102        final Matcher m = MULTIPLE_IDS_PATTERN.matcher(s);
    101103        while (m.find()) {
    102104            final char firstChar = s.charAt(m.start());
    103             ids.add(new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())),
    104                     firstChar == 'n'
     105            if (null != m.group(m.groupCount())) {
     106                for(long r = Long.parseLong(m.group(m.groupCount())) - 2; r <= Long.parseLong(m.group(m.groupCount())); r ++) {
     107                    ids.add(new SimplePrimitiveId(r,
     108                            firstChar == 'n'
    105109                            ? OsmPrimitiveType.NODE
    106110                            : firstChar == 'w'
    107111                            ? OsmPrimitiveType.WAY
    108112                            : OsmPrimitiveType.RELATION));
     113                }
     114            } else {
     115                ids.add(new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())) - 2,
     116                        firstChar == 'n'
     117                        ? OsmPrimitiveType.NODE
     118                        : firstChar == 'w'
     119                        ? OsmPrimitiveType.WAY
     120                        : OsmPrimitiveType.RELATION));
     121            }
    109122        }
    110123        return ids;
    111124    }
  • src/org/openstreetmap/josm/gui/dialogs/OsmIdSelectionDialog.java

    diff --git a/src/org/openstreetmap/josm/gui/dialogs/OsmIdSelectionDialog.java b/src/org/openstreetmap/josm/gui/dialogs/OsmIdSelectionDialog.java
    index 0c53f8e..8ca3249 100644
    a b public class OsmIdSelectionDialog extends ExtendedDialog implements WindowListen  
    9393        HtmlPanel help = new HtmlPanel(/* I18n: {0} and {1} contains example strings not meant for translation. {2}=n, {3}=w, {4}=r. */
    9494                tr("Object IDs can be separated by comma or space.<br/>"
    9595                        + "Examples: {0}<br/>"
    96                         + "In mixed mode, specify objects like this: {1}<br/>"
    97                         + "({2} stands for <i>node</i>, {3} for <i>way</i>, and {4} for <i>relation</i>)",
     96                        + "In mixed mode, specify objects like this: {1}<br/><br/>"
     97                        + "Ranges of object IDs can also be catched, by a hyphen.<br/>"
     98                        + "Examples: {2}<br/>"
     99                        + "({3} stands for <i>node</i>, {4} for <i>way</i>, and {5} for <i>relation</i>)",
    98100                        "<b>" + Utils.joinAsHtmlUnorderedList(Arrays.asList("1 2 5", "1,2,5")) + "</b>",
    99101                        "<b>w123, n110, w12, r15</b>",
     102                        "<b>" + Utils.joinAsHtmlUnorderedList(Arrays.asList("w1-5", "n1-7")) + "</b>",
    100103                        "<b>n</b>", "<b>w</b>", "<b>r</b>"
    101104                ));
    102105        help.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
  • src/org/openstreetmap/josm/gui/widgets/OsmIdTextField.java

    diff --git a/src/org/openstreetmap/josm/gui/widgets/OsmIdTextField.java b/src/org/openstreetmap/josm/gui/widgets/OsmIdTextField.java
    index 1d11acb..9e7df4b 100644
    a b public class OsmIdTextField extends AbstractIdTextField<OsmIdTextField.OsmIdVali  
    122122                                return false;
    123123                            }
    124124                        } catch (IllegalArgumentException ex2) {
    125                             return false;
     125                            try {
     126                                ids.addAll(SimplePrimitiveId.fuzzyParse(s));
     127                            } catch (IllegalArgumentException ex3) {
     128                                return false;
     129                            }
    126130                        }
    127131                    }
    128132                }
  • test/unit/org/openstreetmap/josm/data/osm/SimplePrimitiveIdTest.groovy

    diff --git a/test/unit/org/openstreetmap/josm/data/osm/SimplePrimitiveIdTest.groovy b/test/unit/org/openstreetmap/josm/data/osm/SimplePrimitiveIdTest.groovy
    index 75205e4..81f2df0 100644
    a b class SimplePrimitiveIdTest extends GroovyTestCase {  
    3333        assert SimplePrimitiveId.fromString("way 123") == new SimplePrimitiveId(123, OsmPrimitiveType.WAY)
    3434        assert SimplePrimitiveId.fromString("relation 123") == new SimplePrimitiveId(123, OsmPrimitiveType.RELATION)
    3535    }
     36
     37    void testMultipleIDs() {
     38        assert SimplePrimitiveId.fuzzyParse("node/123-125").toString() == "[node 123, node 124, node 125]"
     39        assert SimplePrimitiveId.fuzzyParse("n/123-125").toString() == "[node 123, node 124, node 125]"
     40        assert SimplePrimitiveId.fuzzyParse("node123-125").toString() == "[node 123, node 124, node 125]"
     41        assert SimplePrimitiveId.fuzzyParse("way/123-125").toString() == "[way 123, way 124, way 125]"
     42        assert SimplePrimitiveId.fuzzyParse("w/123-125").toString() == "[way 123, way 124, way 125]"
     43        assert SimplePrimitiveId.fuzzyParse("way123-125").toString() == "[way 123, way 124, way 125]"
     44        assert SimplePrimitiveId.fuzzyParse("relation/123-125").toString() == "[relation 123, relation 124, relation 125]"
     45        assert SimplePrimitiveId.fuzzyParse("r/123-125").toString() == "[relation 123, relation 124, relation 125]"
     46        assert SimplePrimitiveId.fuzzyParse("relation123-125").toString() == "[relation 123, relation 124, relation 125]"
     47    }
    3648}