Skip to content

Commit 880dc08

Browse files
committed
improved parsing of TimePosition values
1 parent 8093f28 commit 880dc08

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

src/main/java/org/xmlobjects/gml/model/temporal/TimePosition.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
package org.xmlobjects.gml.model.temporal;
2121

2222
import org.xmlobjects.gml.model.GMLObject;
23+
import org.xmlobjects.gml.util.GMLPatterns;
2324
import org.xmlobjects.xml.TextContent;
2425

2526
import java.time.LocalDate;
26-
import java.time.format.DateTimeFormatter;
27-
import java.time.format.DateTimeParseException;
27+
import java.time.LocalTime;
2828

2929
public class TimePosition extends GMLObject {
3030
private TimePositionValue<?> value;
@@ -66,10 +66,11 @@ public void setValue(String value) {
6666
} else if (content.isDouble()) {
6767
this.value = new TimeCoordinate(content.getAsDouble());
6868
} else {
69-
try {
70-
LocalDate date = LocalDate.parse(value, DateTimeFormatter.ISO_WEEK_DATE);
71-
this.value = new CalendarDate(TextContent.of(date.toString()).getAsDate(), CalenderDateType.DATE);
72-
} catch (DateTimeParseException e) {
69+
this.value = getAsWeekDate(value);
70+
if (this.value == null) {
71+
this.value = getAsTime(value);
72+
}
73+
if (this.value == null) {
7374
this.value = new OrdinalPosition(value);
7475
}
7576
}
@@ -142,4 +143,21 @@ public TimeCoordinate asTimeCoordinate() {
142143
public boolean isTimeCoordinate() {
143144
return value instanceof TimeCoordinate;
144145
}
146+
147+
private CalendarDate getAsWeekDate(String value) {
148+
try {
149+
LocalDate date = LocalDate.parse(value, GMLPatterns.ISO_WEEK_DATE);
150+
return new CalendarDate(TextContent.of(date.toString()).getAsDate(), CalenderDateType.DATE);
151+
} catch (Exception e) {
152+
return null;
153+
}
154+
}
155+
156+
private ClockTime getAsTime(String value) {
157+
try {
158+
return new ClockTime(LocalTime.parse(value, GMLPatterns.ISO_TIME));
159+
} catch (Exception e) {
160+
return null;
161+
}
162+
}
145163
}

src/main/java/org/xmlobjects/gml/util/GMLPatterns.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,58 @@
1919

2020
package org.xmlobjects.gml.util;
2121

22+
import java.time.chrono.IsoChronology;
23+
import java.time.format.DateTimeFormatter;
24+
import java.time.format.DateTimeFormatterBuilder;
25+
import java.time.format.ResolverStyle;
26+
import java.time.format.SignStyle;
27+
import java.time.temporal.IsoFields;
2228
import java.util.regex.Pattern;
2329

30+
import static java.time.temporal.ChronoField.*;
31+
2432
public class GMLPatterns {
2533
public static final Pattern UOM_SYMBOL = Pattern.compile("[^: \\n\\r\\t]+");
2634
public static final Pattern UOM_URI = Pattern.compile("([a-zA-Z][a-zA-Z0-9\\-+.]*:|\\.\\./|\\./|#).*");
2735
public static final Pattern AXIS_DIRECTION = Pattern.compile("[+\\-][1-9][0-9]*");
2836
public static final Pattern OTHER_VALUE = Pattern.compile("other:\\w{2,}");
37+
38+
public static final DateTimeFormatter ISO_WEEK_DATE = new DateTimeFormatterBuilder()
39+
.parseCaseInsensitive()
40+
.appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
41+
.optionalStart()
42+
.appendLiteral('-')
43+
.optionalEnd()
44+
.appendLiteral('W')
45+
.appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2)
46+
.optionalStart()
47+
.appendLiteral('-')
48+
.optionalEnd()
49+
.appendValue(DAY_OF_WEEK, 1)
50+
.optionalStart()
51+
.appendOffsetId()
52+
.toFormatter()
53+
.withResolverStyle(ResolverStyle.STRICT)
54+
.withChronology(IsoChronology.INSTANCE);
55+
56+
public static final DateTimeFormatter ISO_TIME = new DateTimeFormatterBuilder()
57+
.parseCaseInsensitive()
58+
.appendLiteral('T')
59+
.appendValue(HOUR_OF_DAY, 2)
60+
.optionalStart()
61+
.optionalStart()
62+
.appendLiteral(':')
63+
.optionalEnd()
64+
.appendValue(MINUTE_OF_HOUR, 2)
65+
.optionalStart()
66+
.optionalStart()
67+
.appendLiteral(':')
68+
.optionalEnd()
69+
.appendValue(SECOND_OF_MINUTE, 2)
70+
.optionalStart()
71+
.appendFraction(NANO_OF_SECOND, 0, 9, true)
72+
.optionalStart()
73+
.appendOffsetId()
74+
.toFormatter()
75+
.withResolverStyle(ResolverStyle.LENIENT);
2976
}

0 commit comments

Comments
 (0)