@@ -3,6 +3,7 @@ package be.yellowduck.gpx
33import com.sun.xml.txw2.output.IndentingXMLStreamWriter
44import java.io.ByteArrayOutputStream
55import java.io.OutputStream
6+ import java.time.temporal.ChronoUnit
67import javax.xml.stream.XMLOutputFactory
78
89class GPXWriter : IWriter {
@@ -13,16 +14,22 @@ class GPXWriter : IWriter {
1314 * @param stream The OutputStream to write to
1415 */
1516 override fun toStream (gpxFile : GPXFile , stream : OutputStream ) {
17+ var factory: XMLOutputFactory
18+ try {
19+ factory = XMLOutputFactory .newFactory()
20+ } catch (_: NoSuchMethodError ) {
21+ factory = XMLOutputFactory .newInstance()
22+ }
1623
17- val writer = IndentingXMLStreamWriter (XMLOutputFactory .newFactory() .createXMLStreamWriter(stream, " UTF-8" ))
24+ val writer = IndentingXMLStreamWriter (factory .createXMLStreamWriter(stream, " UTF-8" ))
1825
1926 writer.setIndentStep(" " )
2027 writer.document {
2128 element(" gpx" ) {
22- if (! gpxFile.version.isNullOrBlank ()) {
29+ if (gpxFile.version.isNotBlank ()) {
2330 attribute(" version" , gpxFile.version)
2431 }
25- if (! gpxFile.creator.isNullOrBlank ()) {
32+ if (gpxFile.creator.isNotBlank ()) {
2633 attribute(" creator" , gpxFile.creator)
2734 }
2835 if (gpxFile.version == " 1.0" ) {
@@ -31,14 +38,14 @@ class GPXWriter : IWriter {
3138 if (gpxFile.version == " 1.1" ) {
3239 attribute(" xmlns" , " http://www.topografix.com/GPX/1/1" )
3340 }
34- if (! gpxFile.name.isNullOrBlank ()) {
41+ if (gpxFile.name.isNotBlank ()) {
3542 element(" metadata" ) {
3643 element(" name" , gpxFile.name)
3744 }
3845 }
3946 gpxFile.tracks.forEach { track ->
4047 element(" trk" ) {
41- if (! track.name.isNullOrBlank ()) {
48+ if (track.name.isNotBlank ()) {
4249 element(" name" , track.name)
4350 }
4451 track.segments.forEach { segment ->
@@ -47,6 +54,14 @@ class GPXWriter : IWriter {
4754 element(" trkpt" ) {
4855 attribute(" lat" , point.lat.toString())
4956 attribute(" lon" , point.lon.toString())
57+
58+ if (point.ele != 0.0 ) {
59+ element(" ele" , point.ele.toString())
60+ }
61+
62+ point.time?.let { time ->
63+ element(" time" , time.truncatedTo(ChronoUnit .SECONDS ).toString())
64+ }
5065 }
5166 }
5267 }
@@ -66,7 +81,7 @@ class GPXWriter : IWriter {
6681 * @return The GPX as a GPX XML string
6782 */
6883 override fun toString (gpxFile : GPXFile ): String {
69- var stream = ByteArrayOutputStream ()
84+ val stream = ByteArrayOutputStream ()
7085 toStream(gpxFile, stream)
7186 return stream.toString(" UTF-8" )
7287 }
0 commit comments