source: josm/trunk/src/org/openstreetmap/josm/tools/GeoUrlToBounds.java@ 11288

Last change on this file since 11288 was 10757, checked in by Don-vip, 8 years ago

sonar - squid:S1659 - Multiple variables should not be declared on the same line

File size: 2.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.regex.Matcher;
7import java.util.regex.Pattern;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.data.Bounds;
11
12/**
13 * Parses a Geo URL (as specified in <a href="https://tools.ietf.org/html/rfc5870">RFC 5870</a>) into {@link Bounds}.
14 */
15public final class GeoUrlToBounds {
16
17 public static final Pattern PATTERN = Pattern.compile("geo:(?<lat>[+-]?[0-9.]+),(?<lon>[+-]?[0-9.]+)(\\?z=(?<zoom>[0-9]+))?");
18
19 private GeoUrlToBounds() {
20 // Hide default constructor for utils classes
21 }
22
23 /**
24 * Parses a Geo URL (as specified in <a href="https://tools.ietf.org/html/rfc5870">RFC 5870</a>) into {@link Bounds}.
25 * @param url the URL to be parsed
26 * @return the parsed {@link Bounds}
27 */
28 public static Bounds parse(final String url) {
29 CheckParameterUtil.ensureParameterNotNull(url, "url");
30 final Matcher m = PATTERN.matcher(url);
31 if (m.matches()) {
32 final double lat;
33 final double lon;
34 final int zoom;
35 try {
36 lat = Double.parseDouble(m.group("lat"));
37 } catch (NumberFormatException e) {
38 Main.warn(tr("URL does not contain valid {0}", tr("latitude")), e);
39 return null;
40 }
41 try {
42 lon = Double.parseDouble(m.group("lon"));
43 } catch (NumberFormatException e) {
44 Main.warn(tr("URL does not contain valid {0}", tr("longitude")), e);
45 return null;
46 }
47 try {
48 zoom = m.group("zoom") != null ? Integer.parseInt(m.group("zoom")) : 18;
49 } catch (NumberFormatException e) {
50 Main.warn(tr("URL does not contain valid {0}", tr("zoom")), e);
51 return null;
52 }
53 return OsmUrlToBounds.positionToBounds(lat, lon, zoom);
54 } else {
55 return null;
56 }
57 }
58}
Note: See TracBrowser for help on using the repository browser.