Skip to content

Commit 272590c

Browse files
authored
Merge pull request #173 from jenetics/issues/JPX-170-lenien_gpx_version
#170: lenien gpx version
2 parents 07ff7fe + 37448f3 commit 272590c

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

jpx/src/main/java/io/jenetics/jpx/GPX.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1966,7 +1966,7 @@ private static String keywords(final GPX gpx) {
19661966
private static XMLReaders
19671967
readers(final Function<? super String, Length> lengthParser) {
19681968
return new XMLReaders()
1969-
.v00(XMLReader.attr("version").map(Version::of))
1969+
.v00(XMLReader.attr("version").map(Version::of, Version.V11))
19701970
.v00(XMLReader.attr("creator"))
19711971
.v11(Metadata.READER)
19721972
.v10(XMLReader.elem("name"))

jpx/src/main/java/io/jenetics/jpx/XMLReader.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ public abstract T read(final XMLStreamReaderAdapter xml, final boolean lenient)
128128
* {@code null}
129129
*/
130130
public <B> XMLReader<B> map(final Function<? super T, ? extends B> mapper) {
131+
return map(mapper, null);
132+
}
133+
134+
/**
135+
* Create a new reader for the new mapped type {@code B}.
136+
*
137+
* @param mapper the mapper function
138+
* @param devault the default value if the mapping function fails and the
139+
* reader is in {@link io.jenetics.jpx.GPX.Reader.Mode#LENIENT} mode
140+
* @param <B> the target type of the new reader
141+
* @return a new reader
142+
* @throws NullPointerException if the given {@code mapper} function is
143+
* {@code null}
144+
*/
145+
public <B> XMLReader<B> map(final Function<? super T, ? extends B> mapper, B devault) {
131146
requireNonNull(mapper);
132147

133148
return new XMLReader<B>(_name, _type) {
@@ -147,7 +162,7 @@ public B read(final XMLStreamReaderAdapter xml, final boolean lenient)
147162
e
148163
);
149164
} else {
150-
return null;
165+
return devault;
151166
}
152167
}
153168
}

jpx/src/test/java/io/jenetics/jpx/GPXTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,4 +943,21 @@ public void issue162_NumberFormattingParsing() {
943943
assertThat(gpx2).isEqualTo(gpx);
944944
}
945945

946+
@Test
947+
public void issue170_InvalidGPXVersion() throws IOException {
948+
final var resource = "/io/jenetics/jpx/ISSUE-170.gpx";
949+
final GPX gpx;
950+
try (InputStream in = getClass().getResourceAsStream(resource)) {
951+
gpx = GPX.Reader.of(Mode.LENIENT).read(in);
952+
}
953+
954+
assertThat(gpx.getVersion()).isEqualTo("1.1");
955+
assertThat(gpx.getCreator()).isEqualTo("Zepp App");
956+
assertThat(gpx.getTracks()).hasSize(1);
957+
assertThat(gpx.getTracks().get(0).getName().orElseThrow())
958+
.isEqualTo("20230507 mile iles");
959+
assertThat(gpx.getTracks().get(0).getSegments()).hasSize(1);
960+
assertThat(gpx.getTracks().get(0).getSegments().get(0)).hasSize(2);
961+
}
962+
946963
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
2+
<gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
3+
xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:ns3="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
5+
creator="Zepp App" version="7.7.5-play"
6+
>
7+
<trk>
8+
<name><![CDATA[20230507 mile iles]]></name>
9+
<trkseg>
10+
<trkpt lat="45.5898" lon="-73.67509">
11+
<ele>42.05</ele>
12+
<time>2023-05-07T18:34:59Z</time>
13+
<extensions>
14+
<ns3:TrackPointExtension>
15+
<ns3:speed>2.3255813</ns3:speed>
16+
<ns3:cad>0.0</ns3:cad>
17+
<ns3:hr>72</ns3:hr>
18+
</ns3:TrackPointExtension>
19+
</extensions>
20+
</trkpt>
21+
<trkpt lat="45.589817" lon="-73.67511">
22+
<ele>42.05</ele>
23+
<time>2023-05-07T18:34:59Z</time>
24+
<extensions>
25+
<ns3:TrackPointExtension>
26+
<ns3:speed>2.3255813</ns3:speed>
27+
<ns3:cad>0.0</ns3:cad>
28+
<ns3:hr>72</ns3:hr>
29+
</ns3:TrackPointExtension>
30+
</extensions>
31+
</trkpt>
32+
</trkseg>
33+
</trk>
34+
</gpx>

0 commit comments

Comments
 (0)