aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/helper/Parser.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/helper/Parser.java')
-rw-r--r--src/org/traccar/helper/Parser.java211
1 files changed, 129 insertions, 82 deletions
diff --git a/src/org/traccar/helper/Parser.java b/src/org/traccar/helper/Parser.java
index 2253b79fc..69c80adb2 100644
--- a/src/org/traccar/helper/Parser.java
+++ b/src/org/traccar/helper/Parser.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -61,35 +61,71 @@ public class Parser {
return matcher.group(position++);
}
- public int nextInt() {
- return nextInt(10);
+ public Integer nextInt() {
+ if (hasNext()) {
+ return Integer.parseInt(next());
+ } else {
+ return null;
+ }
+ }
+
+ public int nextInt(int defaultValue) {
+ if (hasNext()) {
+ return Integer.parseInt(next());
+ } else {
+ return defaultValue;
+ }
+ }
+
+ public Integer nextHexInt() {
+ if (hasNext()) {
+ return Integer.parseInt(next(), 16);
+ } else {
+ return null;
+ }
+ }
+
+ public int nextHexInt(int defaultValue) {
+ if (hasNext()) {
+ return Integer.parseInt(next(), 16);
+ } else {
+ return defaultValue;
+ }
}
- public int nextInt(int radix) {
+ public Integer nextBinInt() {
if (hasNext()) {
- return Integer.parseInt(next(), radix);
+ return Integer.parseInt(next(), 2);
} else {
- return 0;
+ return null;
}
}
- public long nextLong() {
- return nextLong(10);
+ public int nextBinInt(int defaultValue) {
+ if (hasNext()) {
+ return Integer.parseInt(next(), 2);
+ } else {
+ return defaultValue;
+ }
+ }
+
+ public long nextLong(long defaultValue) {
+ return nextLong(10, defaultValue);
}
- public long nextLong(int radix) {
+ public long nextLong(int radix, long defaultValue) {
if (hasNext()) {
return Long.parseLong(next(), radix);
} else {
- return 0;
+ return defaultValue;
}
}
- public double nextDouble() {
+ public double nextDouble(double defaultValue) {
if (hasNext()) {
return Double.parseDouble(next());
} else {
- return 0.0;
+ return defaultValue;
}
}
@@ -114,44 +150,44 @@ public class Parser {
coordinate = Double.parseDouble(next() + '.' + next());
break;
case DEG_HEM:
- coordinate = nextDouble();
+ coordinate = nextDouble(0);
hemisphere = next();
break;
case DEG_MIN_MIN:
- coordinate = nextInt();
+ coordinate = nextInt(0);
coordinate += Double.parseDouble(next() + '.' + next()) / 60;
break;
case DEG_MIN_MIN_HEM:
- coordinate = nextInt();
+ coordinate = nextInt(0);
coordinate += Double.parseDouble(next() + '.' + next()) / 60;
hemisphere = next();
break;
case HEM_DEG:
hemisphere = next();
- coordinate = nextDouble();
+ coordinate = nextDouble(0);
break;
case HEM_DEG_MIN:
hemisphere = next();
- coordinate = nextInt();
- coordinate += nextDouble() / 60;
+ coordinate = nextInt(0);
+ coordinate += nextDouble(0) / 60;
break;
case HEM_DEG_MIN_HEM:
hemisphere = next();
- coordinate = nextInt();
- coordinate += nextDouble() / 60;
+ coordinate = nextInt(0);
+ coordinate += nextDouble(0) / 60;
if (hasNext()) {
hemisphere = next();
}
break;
case HEM_DEG_MIN_MIN:
hemisphere = next();
- coordinate = nextInt();
+ coordinate = nextInt(0);
coordinate += Double.parseDouble(next() + '.' + next()) / 60;
break;
case DEG_MIN_HEM:
default:
- coordinate = nextInt();
- coordinate += nextDouble() / 60;
+ coordinate = nextInt(0);
+ coordinate += nextDouble(0) / 60;
hemisphere = next();
break;
}
@@ -168,77 +204,108 @@ public class Parser {
}
public enum DateTimeFormat {
- HMS, // HHMMSS
- SMH, // SSMMHH
-
- HMS_YMD, // HHMMSSYYYYMMDD or HHMMSSYYMMDD
- HMS_DMY, // HHMMSSDDMMYYYY or HHMMSSDDMMYY
- SMH_YMD, // SSMMHHYYYYMMDD or SSMMHHYYMMDD
- SMH_DMY, // SSMMHHDDMMYYYY or SSMMHHDDMMYY
-
- DMY_HMS, // DDMMYYYYHHMMSS or DDMMYYHHMMSS
- DMY_HMSS, // DDMMYYYYHHMMSS.sss or DDMMYYHHMMSS.sss
- YMD_HMS, // YYYYMMDDHHMMSS or YYMMDDHHMMSS
- YMD_HMSS, // YYYYMMDDHHMMSS.sss or YYMMDDHHMMSS.sss
+ HMS,
+ SMH,
+ HMS_YMD,
+ HMS_DMY,
+ SMH_YMD,
+ SMH_DMY,
+ DMY_HMS,
+ DMY_HMSS,
+ YMD_HMS,
+ YMD_HMSS,
}
- private static final DateTimeFormat DEFAULT_FORMAT = DateTimeFormat.YMD_HMS;
- private static final String DEFAULT_TZ = "UTC";
- private static final int DEFAULT_RADIX = 10;
-
- public Date nextDateTime(DateTimeFormat format, String tz, int radix) {
+ public Date nextDateTime(DateTimeFormat format, String timeZone) {
int year = 0, month = 0, day = 0;
int hour = 0, minute = 0, second = 0, millisecond = 0;
- TimeZone timeZone = TimeZone.getTimeZone(tz);
switch (format) {
case HMS:
- hour = nextInt(radix); minute = nextInt(radix); second = nextInt(radix);
+ hour = nextInt(0);
+ minute = nextInt(0);
+ second = nextInt(0);
break;
case SMH:
- second = nextInt(radix); minute = nextInt(radix); hour = nextInt(radix);
+ second = nextInt(0);
+ minute = nextInt(0);
+ hour = nextInt(0);
break;
case HMS_YMD:
- hour = nextInt(radix); minute = nextInt(radix); second = nextInt(radix);
- year = nextInt(radix); month = nextInt(radix); day = nextInt(radix);
+ hour = nextInt(0);
+ minute = nextInt(0);
+ second = nextInt(0);
+ year = nextInt(0);
+ month = nextInt(0);
+ day = nextInt(0);
break;
case HMS_DMY:
- hour = nextInt(radix); minute = nextInt(radix); second = nextInt(radix);
- day = nextInt(radix); month = nextInt(radix); year = nextInt(radix);
+ hour = nextInt(0);
+ minute = nextInt(0);
+ second = nextInt(0);
+ day = nextInt(0);
+ month = nextInt(0);
+ year = nextInt(0);
break;
case SMH_YMD:
- second = nextInt(radix); minute = nextInt(radix); hour = nextInt(radix);
- year = nextInt(radix); month = nextInt(radix); day = nextInt(radix);
+ second = nextInt(0);
+ minute = nextInt(0);
+ hour = nextInt(0);
+ year = nextInt(0);
+ month = nextInt(0);
+ day = nextInt(0);
break;
case SMH_DMY:
- second = nextInt(radix); minute = nextInt(radix); hour = nextInt(radix);
- day = nextInt(radix); month = nextInt(radix); year = nextInt(radix);
+ second = nextInt(0);
+ minute = nextInt(0);
+ hour = nextInt(0);
+ day = nextInt(0);
+ month = nextInt(0);
+ year = nextInt(0);
break;
case DMY_HMS:
case DMY_HMSS:
- day = nextInt(radix); month = nextInt(radix); year = nextInt(radix);
- hour = nextInt(radix); minute = nextInt(radix); second = nextInt(radix);
+ day = nextInt(0);
+ month = nextInt(0);
+ year = nextInt(0);
+ hour = nextInt(0);
+ minute = nextInt(0);
+ second = nextInt(0);
break;
case YMD_HMS:
case YMD_HMSS:
default:
- year = nextInt(radix); month = nextInt(radix); day = nextInt(radix);
- hour = nextInt(radix); minute = nextInt(radix); second = nextInt(radix);
+ year = nextInt(0);
+ month = nextInt(0);
+ day = nextInt(0);
+ hour = nextInt(0);
+ minute = nextInt(0);
+ second = nextInt(0);
break;
}
if (format == DateTimeFormat.YMD_HMSS || format == DateTimeFormat.DMY_HMSS) {
- millisecond = nextInt(radix); // (ddd)
+ millisecond = nextInt(0); // (ddd)
}
if (year >= 0 && year < 100) {
year += 2000;
}
- DateBuilder dateBuilder = new DateBuilder(timeZone);
-
- if (format != DateTimeFormat.HMS || format != DateTimeFormat.SMH) {
- dateBuilder.setDate(year, month, day);
+ DateBuilder dateBuilder;
+ if (format != DateTimeFormat.HMS && format != DateTimeFormat.SMH) {
+ if (timeZone != null) {
+ dateBuilder = new DateBuilder(TimeZone.getTimeZone(timeZone));
+ } else {
+ dateBuilder = new DateBuilder();
+ }
+ dateBuilder.setDate(year, month, day);
+ } else {
+ if (timeZone != null) {
+ dateBuilder = new DateBuilder(new Date(), TimeZone.getTimeZone(timeZone));
+ } else {
+ dateBuilder = new DateBuilder(new Date());
+ }
}
dateBuilder.setTime(hour, minute, second, millisecond);
@@ -246,32 +313,12 @@ public class Parser {
return dateBuilder.getDate();
}
- public Date nextDateTime(String tz, int radix) {
- return nextDateTime(DEFAULT_FORMAT, tz, radix);
- }
-
- public Date nextDateTime(DateTimeFormat format, int radix) {
- return nextDateTime(format, DEFAULT_TZ, radix);
- }
-
- public Date nextDateTime(DateTimeFormat format, String tz) {
- return nextDateTime(format, tz, DEFAULT_RADIX);
- }
-
public Date nextDateTime(DateTimeFormat format) {
- return nextDateTime(format, DEFAULT_TZ, DEFAULT_RADIX);
- }
-
- public Date nextDateTime(String tz) {
- return nextDateTime(DEFAULT_FORMAT, tz, DEFAULT_RADIX);
- }
-
- public Date nextDateTime(int radix) {
- return nextDateTime(DEFAULT_FORMAT, DEFAULT_TZ, radix);
+ return nextDateTime(format, null);
}
public Date nextDateTime() {
- return nextDateTime(DEFAULT_FORMAT, DEFAULT_TZ, DEFAULT_RADIX);
+ return nextDateTime(DateTimeFormat.YMD_HMS, null);
}
}