From 2771da35deae1bd369d05837730d7fa92b0b9565 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 16 Jan 2015 10:22:52 +0100 Subject: [PATCH] GeoPointParserUtil.getQueryParameters() to return Map of all parameters This parses out all of the parameters in the query string for both http: and geo: URIs. This will only work on URIs with valid syntax, so it will not work on URIs that do odd things like have a query string in the fragment, like this one: http://www.amap.com/#!poi!!q=38.174596,114.995033|2|%E5%AE%BE%E9%A6%86&radius=1000 --- .../net/osmand/util/GeoPointParserUtil.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/OsmAnd-java/src/net/osmand/util/GeoPointParserUtil.java b/OsmAnd-java/src/net/osmand/util/GeoPointParserUtil.java index 6c8e9cff02..afcfa340f3 100644 --- a/OsmAnd-java/src/net/osmand/util/GeoPointParserUtil.java +++ b/OsmAnd-java/src/net/osmand/util/GeoPointParserUtil.java @@ -22,6 +22,58 @@ public class GeoPointParserUtil { int z = GeoParsedPoint.NO_ZOOM; String url; + String noQueryParameters[] = { + "geo:0,0", + "geo:0,0?", + "http://download.osmand.net/go", + "http://download.osmand.net/go?", + }; + for (String s: noQueryParameters) { + URI uri = URI.create(s); + Map map = getQueryParameters(uri); + System.out.print(s + " map: " + map.size() + "..."); + if (map.size() != 0) { + System.out.println(""); + throw new RuntimeException("Map should be 0 but is " + map.size()); + } + System.out.println(" Passed!"); + } + + String oneQueryParameter[] = { + "geo:0,0?m", + "geo:0,0?m=", + "geo:0,0?m=foo", + "http://download.osmand.net/go?lat", + "http://download.osmand.net/go?lat=", + "http://download.osmand.net/go?lat=34.99393", + }; + for (String s: oneQueryParameter) { + URI uri = URI.create(s); + Map map = getQueryParameters(uri); + System.out.print(s + " map: " + map.size() + "..."); + if (map.size() != 1) { + System.out.println(""); + throw new RuntimeException("Map should be 1 but is " + map.size()); + } + System.out.println(" Passed!"); + } + + String twoQueryParameters[] = { + "geo:0,0?z=11&q=Lots+Of+Stuff", + "http://download.osmand.net/go?lat=34.99393&lon=-110.12345", + "http://download.osmand.net/go?lat=34.99393&lon=-110.12345#this+should+be+ignored", + }; + for (String s: twoQueryParameters) { + URI uri = URI.create(s); + Map map = getQueryParameters(uri); + System.out.print(s + " map: " + map.size() + "..."); + if (map.size() != 2) { + System.out.println(""); + throw new RuntimeException("Map should be 2 but is " + map.size()); + } + System.out.println(" Passed!"); + } + // geo:34,-106 url = "geo:" + ilat + "," + ilon; System.out.println("url: " + url); @@ -520,6 +572,43 @@ public class GeoPointParserUtil { return value; } + /** + * This parses out all of the parameters in the query string for both + * http: and geo: URIs. This will only work on URIs with valid syntax, so + * it will not work on URIs that do odd things like have a query string in + * the fragment, like this one: + * http://www.amap.com/#!poi!!q=38.174596,114.995033|2|%E5%AE%BE%E9%A6%86&radius=1000 + * + * @param uri + * @return {@link Map} a Map of the query parameters + */ + private static Map getQueryParameters(URI uri) { + final LinkedHashMap map = new LinkedHashMap(); + String query = null; + if (uri.isOpaque()) { + String schemeSpecificPart = uri.getSchemeSpecificPart(); + int pos = schemeSpecificPart.indexOf("?"); + if (pos == schemeSpecificPart.length()) { + query = ""; + } else if (pos > -1) { + query = schemeSpecificPart.substring(pos + 1); + } + } else { + query = uri.getQuery(); + } + if (query != null && !query.equals("")) { + String[] params = query.split("&"); + for (String p : params) { + String[] keyValue = p.split("="); + if (keyValue.length == 1) + map.put(keyValue[0], ""); + else if (keyValue.length > 1) + map.put(keyValue[0], keyValue[1]); + } + } + return map; + } + /** * Parses geo and map intents: *