Ignore:
Timestamp:
2015-06-19T20:23:18+02:00 (10 years ago)
Author:
simon04
Message:

fix #11505 - Download objects: load a range of OSM objects (modified patch by windu.2b)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java

    r8338 r8496  
    55import java.util.ArrayList;
    66import java.util.List;
     7import java.util.regex.MatchResult;
    78import java.util.regex.Matcher;
    89import java.util.regex.Pattern;
     
    1516    private final OsmPrimitiveType type;
    1617
    17     public static final Pattern ID_PATTERN = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)[ /]?)(\\d+)");
     18    public static final Pattern ID_PATTERN = Pattern.compile("(n|node|w|way|r|rel|relation)[ /]?(\\d+)");
     19
     20    public static final Pattern MULTIPLE_IDS_PATTERN = Pattern.compile(ID_PATTERN.pattern() + "(-(\\d+))?");
    1821
    1922    public SimplePrimitiveId(long id, OsmPrimitiveType type) {
     
    8083        final Matcher m = ID_PATTERN.matcher(s);
    8184        if (m.matches()) {
    82             return new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())),
    83                     s.charAt(0) == 'n'
    84                             ? OsmPrimitiveType.NODE
    85                             : s.charAt(0) == 'w'
    86                             ? OsmPrimitiveType.WAY
    87                             : OsmPrimitiveType.RELATION);
     85            return new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())), getOsmPrimitiveType(s.charAt(0)));
    8886        } else {
    8987            throw new IllegalArgumentException("The string " + s + " does not match the pattern " + ID_PATTERN);
     
    9290
    9391    /**
     92     * Parses a range {@code SimplePrimitiveId} from the string {@code s}.
     93     * @param s the string to be parsed, e.g., {@code node1}, {@code node1-7}, {@code node70-7}.
     94     * @return the parsed {@code SimplePrimitiveId}s
     95     * @throws IllegalArgumentException if the string does not match the pattern
     96     */
     97    public static List<SimplePrimitiveId> multipleFromString(String s) {
     98        final Matcher m = MULTIPLE_IDS_PATTERN.matcher(s);
     99        if (m.matches()) {
     100            return extractIdsInto(m, new ArrayList<SimplePrimitiveId>());
     101        } else {
     102            throw new IllegalArgumentException("The string " + s + " does not match the pattern " + MULTIPLE_IDS_PATTERN);
     103        }
     104    }
     105
     106    /**
    94107     * Attempts to parse extract any primitive id from the string {@code s}.
    95      * @param s the string to be parsed, e.g., {@code n1, w1}, {@code node1 and rel2}.
     108     * @param s the string to be parsed, e.g., {@code "n1, w1"}, {@code "node1 and rel2"}, {@code "node 123-29"}.
    96109     * @return the parsed list of {@code OsmPrimitiveType}s.
    97110     */
    98111    public static List<SimplePrimitiveId> fuzzyParse(String s) {
    99112        final List<SimplePrimitiveId> ids = new ArrayList<>();
    100         final Matcher m = ID_PATTERN.matcher(s);
     113        final Matcher m = MULTIPLE_IDS_PATTERN.matcher(s);
    101114        while (m.find()) {
    102             final char firstChar = s.charAt(m.start());
    103             ids.add(new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())),
    104                     firstChar == 'n'
    105                             ? OsmPrimitiveType.NODE
    106                             : firstChar == 'w'
    107                             ? OsmPrimitiveType.WAY
    108                             : OsmPrimitiveType.RELATION));
     115            extractIdsInto(m, ids);
    109116        }
    110117        return ids;
    111118    }
     119
     120    private static List<SimplePrimitiveId> extractIdsInto(MatchResult m, List<SimplePrimitiveId> ids) {
     121        final OsmPrimitiveType type = getOsmPrimitiveType(m.group(1).charAt(0));
     122        final String firstId = m.group(2);
     123        final String lastId = m.group(4);
     124        if (lastId != null) {
     125            final long lastIdParsed;
     126            if (lastId.length() < firstId.length()) {
     127                // parse ranges such as 123-25 or 123-5
     128                lastIdParsed = Long.parseLong(firstId.substring(0, firstId.length() - lastId.length()) + lastId);
     129            } else {
     130                // parse ranges such as 123-125 or 998-1001
     131                lastIdParsed = Long.parseLong(lastId);
     132            }
     133            for (long i = Long.parseLong(firstId); i <= lastIdParsed; i++) {
     134                ids.add(new SimplePrimitiveId(i, type));
     135            }
     136        } else {
     137            ids.add(new SimplePrimitiveId(Long.parseLong(firstId), type));
     138        }
     139        return ids;
     140    }
     141
     142    private static OsmPrimitiveType getOsmPrimitiveType(char firstChar) {
     143        return firstChar == 'n' ? OsmPrimitiveType.NODE : firstChar == 'w' ? OsmPrimitiveType.WAY : OsmPrimitiveType.RELATION;
     144    }
    112145}
Note: See TracChangeset for help on using the changeset viewer.