From d9f6e0fa8ae131574436cf4375d7feb2d7ff76a7 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 15 Oct 2015 14:21:08 +1300 Subject: Clean up some text protocols --- src/org/traccar/helper/Parser.java | 42 +++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'src/org/traccar/helper') diff --git a/src/org/traccar/helper/Parser.java b/src/org/traccar/helper/Parser.java index e89104094..2fd615ca5 100644 --- a/src/org/traccar/helper/Parser.java +++ b/src/org/traccar/helper/Parser.java @@ -20,7 +20,7 @@ import java.util.regex.Pattern; public class Parser { - private int position = 1; + private int position; private Matcher matcher; public Parser(Pattern pattern, String input) { @@ -28,9 +28,15 @@ public class Parser { } public boolean matches() { + position = 1; return matcher.matches(); } + public boolean find() { + position = 1; + return matcher.find(); + } + public boolean hasNext() { if (matcher.group(position) != null) { return true; @@ -64,15 +70,37 @@ public class Parser { } } - // Format: (degrees)(minutes)(hemisphere) - public double nextCoordinate() { - double coordinate = nextDouble(); - coordinate += nextDouble() / 60; - String hemisphere = next(); - if (hemisphere.equals("S") || hemisphere.equals("W")) { + public enum CoordinateFormat { + DEG_MIN_HEM, + HEM_DEG + } + + public double nextCoordinate(CoordinateFormat format) { + double coordinate; + String hemisphere; + + switch (format) { + case HEM_DEG: + hemisphere = next(); + coordinate = nextDouble(); + break; + case DEG_MIN_HEM: + default: + coordinate = nextDouble(); + coordinate += nextDouble() / 60; + hemisphere = next(); + break; + } + + if (hemisphere != null && (hemisphere.equals("S") || hemisphere.equals("W"))) { coordinate = -coordinate; } + return coordinate; } + public double nextCoordinate() { + return nextCoordinate(CoordinateFormat.DEG_MIN_HEM); + } + } -- cgit v1.2.3