diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2012-03-08 20:16:01 +0100 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2012-03-08 20:16:01 +0100 |
| commit | 24b8acc8c252a94f14520f9d48a30bae3b3e6f5b (patch) | |
| tree | 30cbb93db370200ce54ef9de61766c127f38bcf4 | |
| parent | 43c16f1aea7b559196d27ccae18c6bfa950838bc (diff) | |
| download | cgeo-24b8acc8c252a94f14520f9d48a30bae3b3e6f5b.zip cgeo-24b8acc8c252a94f14520f9d48a30bae3b3e6f5b.tar.gz cgeo-24b8acc8c252a94f14520f9d48a30bae3b3e6f5b.tar.bz2 | |
refactoring: make JSON parsing more robust by using a regex
| -rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCBase.java | 20 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/connector/gc/GCBaseTest.java | 14 |
2 files changed, 28 insertions, 6 deletions
diff --git a/main/src/cgeo/geocaching/connector/gc/GCBase.java b/main/src/cgeo/geocaching/connector/gc/GCBase.java index 23cee61..e7008aa 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCBase.java +++ b/main/src/cgeo/geocaching/connector/gc/GCBase.java @@ -29,6 +29,8 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * GC.com/Groundspeak (GS) specific stuff @@ -62,6 +64,7 @@ public class GCBase { private final static int POSX_FOUND = 10; private final static int POSY_FOUND = -8; + private final static Pattern PATTERN_JSON_KEY = Pattern.compile("[^\\d]*" + "(\\d+),\\s*(\\d+)" + "[^\\d]*"); // (12, 34) /** * Searches the view port on the live map with Strategy.AUTO * @@ -494,12 +497,17 @@ public class GCBase { * Key in the format (xx, xx) * @return */ - private static int[] splitJSONKey(String key) { - // two possible positions for the , - int separator = key.charAt(2) == ',' ? 2 : 3; - int x = Integer.parseInt(key.substring(1, separator)); - int y = Integer.parseInt(key.substring(separator + 2, key.length() - 1)); - return new int[] { x, y }; + static int[] splitJSONKey(String key) { + final Matcher matcher = PATTERN_JSON_KEY.matcher(key); + try { + if (matcher.matches()) { + final int x = Integer.parseInt(matcher.group(1)); + final int y = Integer.parseInt(matcher.group(2)); + return new int[] { x, y }; + } + } catch (NumberFormatException e) { + } + return new int[] { 0, 0 }; } } diff --git a/tests/src/cgeo/geocaching/connector/gc/GCBaseTest.java b/tests/src/cgeo/geocaching/connector/gc/GCBaseTest.java new file mode 100644 index 0000000..5f07f60 --- /dev/null +++ b/tests/src/cgeo/geocaching/connector/gc/GCBaseTest.java @@ -0,0 +1,14 @@ +package cgeo.geocaching.connector.gc; + +import java.util.Arrays; + +import junit.framework.TestCase; + +public class GCBaseTest extends TestCase { + public static void testSplitJSONKey() { + assertTrue(Arrays.equals(new int[] { 1, 2 }, GCBase.splitJSONKey("(1, 2)"))); + assertTrue(Arrays.equals(new int[] { 12, 34 }, GCBase.splitJSONKey("(12, 34)"))); + assertTrue(Arrays.equals(new int[] { 1234, 56 }, GCBase.splitJSONKey("(1234,56)"))); + assertTrue(Arrays.equals(new int[] { 1234, 567 }, GCBase.splitJSONKey("(1234, 567)"))); + } +} |
