fixed line formatting
This commit is contained in:
parent
5b7fdcd6a9
commit
6589c3321b
12
pom.xml
12
pom.xml
@ -82,15 +82,9 @@
|
||||
<version>${kotlin-junit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.kotest</groupId>
|
||||
<artifactId>kotest-runner-junit5-jvm</artifactId>
|
||||
<version>${kotest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.kotest</groupId>
|
||||
<artifactId>kotest-assertions-core-jvm</artifactId>
|
||||
<version>${kotest.version}</version>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -99,11 +99,11 @@ fun GpxType.toXmlString(clock: Clock?): String = """
|
||||
xsi:schemaLocation="$SCHEMA_LOCATION">
|
||||
<time>${now(clock)}</time>
|
||||
${this.metadata.toXmlString()}
|
||||
${this.wpt?.toXmlString()}
|
||||
${this.rte?.toXmlString()}
|
||||
${this.trk?.toXmlString()}
|
||||
${this.wpt?.toXmlString() ?: ""}
|
||||
${this.rte?.toXmlString() ?: ""}
|
||||
${this.trk?.toXmlString() ?: ""}
|
||||
</gpx>
|
||||
""".trim()
|
||||
""".trim().removeEmptyStrings()
|
||||
|
||||
fun MetadataType.toXmlString(): String = """
|
||||
<metadata>
|
||||
@ -113,12 +113,9 @@ fun MetadataType.toXmlString(): String = """
|
||||
<name>${this.authorName}</name>
|
||||
</author>
|
||||
</metadata>
|
||||
""".trim()
|
||||
""".trim().removeEmptyStrings()
|
||||
|
||||
private fun now(clock: Clock?) = OffsetDateTime.now(clock ?: Clock.systemDefaultZone()).format(DTF)
|
||||
|
||||
fun WptType.toXmlString(nodeName: String? = null) =
|
||||
if (nodeName != null) {
|
||||
fun WptType.toXmlString(nodeName: String? = null) = if (nodeName != null) {
|
||||
this.toXmlString(nodeName)
|
||||
} else {
|
||||
this.toXmlString("wpt")
|
||||
@ -135,19 +132,19 @@ fun WptType.toXmlString(nodeName: String) = """
|
||||
${toXmlString(cmt, "cmt")}
|
||||
${toXmlString(desc, "desc")}
|
||||
${toXmlString(src, "src")}
|
||||
${link?.toXmlString()}
|
||||
${link?.toXmlString() ?: ""}
|
||||
${toXmlString(sym, "sym")}
|
||||
${toXmlString(type, "type")}
|
||||
${fix?.toXmlString()}
|
||||
${fix?.toXmlString() ?: ""}
|
||||
${toXmlString(sat, "sat")}
|
||||
${toXmlString(hdop, "hdop")}
|
||||
${toXmlString(vdop, "vdop")}
|
||||
${toXmlString(pdop, "pdop")}
|
||||
${toXmlString(ageofgpsdata, "ageofgpsdata")}
|
||||
${toXmlString(dgpsid, "dgpsid")}
|
||||
${extensions?.toXmlString()}
|
||||
${extensions?.toXmlString() ?: ""}
|
||||
</${nodeName}>
|
||||
""".trim()
|
||||
""".trim().removeEmptyStrings()
|
||||
|
||||
fun RteType.toXmlString() = """
|
||||
<rte>
|
||||
@ -155,13 +152,13 @@ fun RteType.toXmlString() = """
|
||||
${toXmlString(this.cmt, "cmt")}
|
||||
${toXmlString(this.desc, "desc")}
|
||||
${toXmlString(this.src, "src")}
|
||||
${this.link?.toXmlString()}
|
||||
${this.link?.toXmlString() ?: ""}
|
||||
${toXmlString(this.number, "number")}
|
||||
${toXmlString(this.type, "type")}
|
||||
${this.extensions?.toXmlString()}
|
||||
${this.rtept?.toXmlString()}
|
||||
${this.extensions?.toXmlString() ?: ""}
|
||||
${this.rtept?.toXmlString() ?: ""}
|
||||
</rte>
|
||||
""".trim()
|
||||
""".trim().removeEmptyStrings()
|
||||
|
||||
fun TrkType.toXmlString() = """
|
||||
<trk>
|
||||
@ -169,22 +166,45 @@ fun TrkType.toXmlString() = """
|
||||
${toXmlString(this.cmt, "cmt")}
|
||||
${toXmlString(this.desc, "desc")}
|
||||
${toXmlString(this.src, "src")}
|
||||
${this.link?.toXmlString()}
|
||||
${this.link?.toXmlString() ?: ""}
|
||||
${toXmlString(this.number, "number")}
|
||||
${toXmlString(this.type, "type")}
|
||||
${this.extensions?.toXmlString()}
|
||||
${this.trkseg?.toXmlString()}
|
||||
${this.extensions?.toXmlString() ?: ""}
|
||||
${this.trkseg?.toXmlString() ?: ""}
|
||||
</trk>
|
||||
""".trim()
|
||||
""".trim().removeEmptyStrings()
|
||||
|
||||
fun List<WptType>.toXmlString(nodeName: String?) =
|
||||
this.joinToString(prefix = "", postfix = "", separator = "") {
|
||||
fun LinkType.toXmlString() = """
|
||||
<link href="${this.href}">
|
||||
<text>${this.text}</text>
|
||||
<type>${this.type}</type>
|
||||
</link>
|
||||
""".trim().removeEmptyStrings()
|
||||
|
||||
fun FixType.toXmlString() = """
|
||||
<fix>${this.value}</fix>
|
||||
""".trim().removeEmptyStrings()
|
||||
|
||||
fun ExtensionType.toXmlString() = """
|
||||
<${this.nodeName}${toXmlString(this.parameters)}>${this.value ?: ""}</${this.nodeName}>
|
||||
""".trim().removeEmptyStrings()
|
||||
|
||||
fun TrksegType.toXmlString() = """
|
||||
<trkseg>
|
||||
${this.trkpt?.toXmlString("trkpt") ?: ""}
|
||||
</trkseg>
|
||||
""".trim().removeEmptyStrings()
|
||||
|
||||
fun List<WptType>.toXmlString(nodeName: String?) = this.joinToString(prefix = "\n", postfix = "", separator = "") {
|
||||
it.toXmlString(nodeName)
|
||||
}
|
||||
|
||||
fun List<ExtensionType>.toXmlString() = this.joinToString(
|
||||
prefix = "<extensions>\n", postfix = "\n</extensions>", separator = "\n", transform = ExtensionType::toXmlString
|
||||
)
|
||||
|
||||
@JvmName("toXmlStringWptType")
|
||||
fun List<WptType>.toXmlString() =
|
||||
this.joinToString(prefix = "", postfix = "", separator = "") {
|
||||
fun List<WptType>.toXmlString() = this.joinToString(prefix = "", postfix = "", separator = "") {
|
||||
it.toXmlString()
|
||||
}
|
||||
|
||||
@ -204,76 +224,42 @@ fun List<TrkType>.toXmlString() =
|
||||
fun List<TrksegType>.toXmlString() =
|
||||
this.joinToString(prefix = "", postfix = "", separator = "", transform = TrksegType::toXmlString)
|
||||
|
||||
fun List<ExtensionType>.toXmlString() =
|
||||
this.joinToString(
|
||||
prefix = "<extensions>\n",
|
||||
postfix = "\n</extensions>",
|
||||
separator = "\n",
|
||||
transform = ExtensionType::toXmlString
|
||||
)
|
||||
|
||||
fun LinkType.toXmlString() = """
|
||||
<link href="${this.href}">
|
||||
<text>${this.text}</text>
|
||||
<type>${this.type}</type>
|
||||
</link>
|
||||
""".trim()
|
||||
|
||||
fun FixType.toXmlString() = """
|
||||
<fix>${this.value}</fix>
|
||||
""".trim()
|
||||
|
||||
fun ExtensionType.toXmlString() = """
|
||||
<${this.nodeName}${toXmlString(this.parameters)}>${this.value ?: ""}</${this.nodeName}>
|
||||
""".trim()
|
||||
|
||||
fun TrksegType.toXmlString() = """
|
||||
<trkseg>
|
||||
${this.trkpt?.toXmlString("trkpt")}
|
||||
</trkseg>
|
||||
""".trim()
|
||||
|
||||
fun toXmlString(value: String?, nodeName: String) =
|
||||
if (value != null) {
|
||||
"""
|
||||
<${nodeName}>${value}</${nodeName}>
|
||||
""".trim()
|
||||
fun toXmlString(value: String?, nodeName: String) = if (value != null) {
|
||||
"<${nodeName}>${value}</${nodeName}>"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
|
||||
fun toXmlString(value: Int?, nodeName: String) =
|
||||
if (value != null) {
|
||||
"""
|
||||
<${nodeName}>${value}</${nodeName}>
|
||||
""".trim()
|
||||
fun toXmlString(value: Int?, nodeName: String) = if (value != null) {
|
||||
"<${nodeName}>${value}</${nodeName}>"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
|
||||
fun toXmlString(value: Double?, nodeName: String) =
|
||||
if (value != null) {
|
||||
"""
|
||||
<${nodeName}>${value}</${nodeName}>
|
||||
""".trim()
|
||||
fun toXmlString(value: Double?, nodeName: String) = if (value != null) {
|
||||
"<${nodeName}>${value}</${nodeName}>"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
|
||||
fun toXmlString(value: OffsetDateTime?, nodeName: String) =
|
||||
if (value != null) {
|
||||
"""
|
||||
<${nodeName}>${value.format(DTF)}</${nodeName}>
|
||||
""".trim()
|
||||
fun toXmlString(value: OffsetDateTime?, nodeName: String) = if (value != null) {
|
||||
"<${nodeName}>${value.format(DTF)}</${nodeName}>"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
|
||||
fun toXmlString(value: Map<String, String>?) =
|
||||
value?.entries?.joinToString(separator = "") {
|
||||
fun toXmlString(value: Map<String, String>?) = value?.entries?.joinToString(separator = "") {
|
||||
" ${it.key}=\"${it.value}\""
|
||||
} ?: ""
|
||||
|
||||
private fun now(clock: Clock?) = OffsetDateTime.now(clock ?: Clock.systemDefaultZone()).format(DTF)
|
||||
|
||||
private fun String.removeEmptyStrings() = this.lineSequence().map {
|
||||
it.trim()
|
||||
}.filter {
|
||||
it != ""
|
||||
}.joinToString("\n")
|
||||
|
||||
class GpxWriter {
|
||||
companion object {
|
||||
const val HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
@ -281,8 +267,8 @@ class GpxWriter {
|
||||
const val XMLNS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
|
||||
const val SCHEMA_LOCATION = "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
|
||||
|
||||
internal val DTF = DateTimeFormatterBuilder()
|
||||
.append(ISO_LOCAL_DATE_TIME) // use the existing formatter for date time
|
||||
internal val DTF =
|
||||
DateTimeFormatterBuilder().append(ISO_LOCAL_DATE_TIME) // use the existing formatter for date time
|
||||
.appendOffset("+HH:MM", "+00:00") // set 'noOffsetText' to desired '+00:00'
|
||||
.toFormatter()
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
Copyright bvn13, 2022
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
@ -77,13 +77,76 @@ limitations under the License.
|
||||
|
||||
package me.bvn13.sdk.android.gpx
|
||||
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.*
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class GpxWriterTest : FunSpec() {
|
||||
init {
|
||||
test("first test") {
|
||||
class GpxWriterTest {
|
||||
@DisplayName("test minimum")
|
||||
@Test
|
||||
fun testMinimum() {
|
||||
val clock = Clock.fixed(
|
||||
LocalDateTime.of(2022, 9, 24, 15, 4, 0, 0).toInstant(ZoneOffset.ofHours(3)), ZoneId.of("Europe/Moscow")
|
||||
)
|
||||
|
||||
val gpxType = GpxType(
|
||||
MetadataType("test name"),
|
||||
trk = listOf(
|
||||
TrkType(
|
||||
name = "track1",
|
||||
trkseg = listOf(
|
||||
TrksegType(
|
||||
listOf(
|
||||
WptType(
|
||||
lat = 123.toDouble(),
|
||||
lon = 321.toDouble()
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<gpx
|
||||
xmlns="http://www.topografix.com/GPX/1/1"
|
||||
version="1.1"
|
||||
creator="me.bvn13.sdk.android.gpx"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
|
||||
<time>2022-09-24T15:04:00+03:00</time>
|
||||
<metadata>
|
||||
<name>test name</name>
|
||||
<desc></desc>
|
||||
<author>
|
||||
<name></name>
|
||||
</author>
|
||||
</metadata>
|
||||
<trk>
|
||||
<name>track1</name>
|
||||
<trkseg>
|
||||
<trkpt lat="123.0" lon="321.0">
|
||||
</trkpt>
|
||||
</trkseg>
|
||||
</trk>
|
||||
</gpx>
|
||||
""".trim()
|
||||
.lineSequence()
|
||||
.map {
|
||||
it.trim()
|
||||
}
|
||||
.joinToString("\n"),
|
||||
gpxType.toXmlString(clock)
|
||||
)
|
||||
}
|
||||
|
||||
@DisplayName("test maximum")
|
||||
@Test
|
||||
fun maximumTest() {
|
||||
val clock = Clock.fixed(
|
||||
LocalDateTime.of(2022, 9, 24, 15, 4, 0, 0).toInstant(ZoneOffset.ofHours(3)), ZoneId.of("Europe/Moscow")
|
||||
)
|
||||
@ -363,10 +426,8 @@ class GpxWriterTest : FunSpec() {
|
||||
<cmt>comment track 1</cmt>
|
||||
<desc>desc track 1</desc>
|
||||
<src>src track 1</src>
|
||||
null
|
||||
<number>1234</number>
|
||||
<type>type 1</type>
|
||||
null
|
||||
<trkseg>
|
||||
<trkpt lat="14.64736838389662" lon="7.93212890625">
|
||||
<ele>10.0</ele>
|
||||
@ -402,9 +463,13 @@ class GpxWriterTest : FunSpec() {
|
||||
</trkseg>
|
||||
</trk>
|
||||
</gpx>
|
||||
""".trim(),
|
||||
""".trim()
|
||||
.lineSequence()
|
||||
.map {
|
||||
it.trim()
|
||||
}
|
||||
.joinToString("\n"),
|
||||
gpxType.toXmlString(clock)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user