fixed line formatting

This commit is contained in:
bvn13 2022-09-29 19:34:37 +03:00
parent 5b7fdcd6a9
commit 6589c3321b
3 changed files with 466 additions and 421 deletions

12
pom.xml
View File

@ -82,15 +82,9 @@
<version>${kotlin-junit.version}</version> <version>${kotlin-junit.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.kotest</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>kotest-runner-junit5-jvm</artifactId> <artifactId>junit-jupiter-api</artifactId>
<version>${kotest.version}</version> <version>${jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-core-jvm</artifactId>
<version>${kotest.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -99,11 +99,11 @@ fun GpxType.toXmlString(clock: Clock?): String = """
xsi:schemaLocation="$SCHEMA_LOCATION"> xsi:schemaLocation="$SCHEMA_LOCATION">
<time>${now(clock)}</time> <time>${now(clock)}</time>
${this.metadata.toXmlString()} ${this.metadata.toXmlString()}
${this.wpt?.toXmlString()} ${this.wpt?.toXmlString() ?: ""}
${this.rte?.toXmlString()} ${this.rte?.toXmlString() ?: ""}
${this.trk?.toXmlString()} ${this.trk?.toXmlString() ?: ""}
</gpx> </gpx>
""".trim() """.trim().removeEmptyStrings()
fun MetadataType.toXmlString(): String = """ fun MetadataType.toXmlString(): String = """
<metadata> <metadata>
@ -113,12 +113,9 @@ fun MetadataType.toXmlString(): String = """
<name>${this.authorName}</name> <name>${this.authorName}</name>
</author> </author>
</metadata> </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) this.toXmlString(nodeName)
} else { } else {
this.toXmlString("wpt") this.toXmlString("wpt")
@ -135,19 +132,19 @@ fun WptType.toXmlString(nodeName: String) = """
${toXmlString(cmt, "cmt")} ${toXmlString(cmt, "cmt")}
${toXmlString(desc, "desc")} ${toXmlString(desc, "desc")}
${toXmlString(src, "src")} ${toXmlString(src, "src")}
${link?.toXmlString()} ${link?.toXmlString() ?: ""}
${toXmlString(sym, "sym")} ${toXmlString(sym, "sym")}
${toXmlString(type, "type")} ${toXmlString(type, "type")}
${fix?.toXmlString()} ${fix?.toXmlString() ?: ""}
${toXmlString(sat, "sat")} ${toXmlString(sat, "sat")}
${toXmlString(hdop, "hdop")} ${toXmlString(hdop, "hdop")}
${toXmlString(vdop, "vdop")} ${toXmlString(vdop, "vdop")}
${toXmlString(pdop, "pdop")} ${toXmlString(pdop, "pdop")}
${toXmlString(ageofgpsdata, "ageofgpsdata")} ${toXmlString(ageofgpsdata, "ageofgpsdata")}
${toXmlString(dgpsid, "dgpsid")} ${toXmlString(dgpsid, "dgpsid")}
${extensions?.toXmlString()} ${extensions?.toXmlString() ?: ""}
</${nodeName}> </${nodeName}>
""".trim() """.trim().removeEmptyStrings()
fun RteType.toXmlString() = """ fun RteType.toXmlString() = """
<rte> <rte>
@ -155,13 +152,13 @@ fun RteType.toXmlString() = """
${toXmlString(this.cmt, "cmt")} ${toXmlString(this.cmt, "cmt")}
${toXmlString(this.desc, "desc")} ${toXmlString(this.desc, "desc")}
${toXmlString(this.src, "src")} ${toXmlString(this.src, "src")}
${this.link?.toXmlString()} ${this.link?.toXmlString() ?: ""}
${toXmlString(this.number, "number")} ${toXmlString(this.number, "number")}
${toXmlString(this.type, "type")} ${toXmlString(this.type, "type")}
${this.extensions?.toXmlString()} ${this.extensions?.toXmlString() ?: ""}
${this.rtept?.toXmlString()} ${this.rtept?.toXmlString() ?: ""}
</rte> </rte>
""".trim() """.trim().removeEmptyStrings()
fun TrkType.toXmlString() = """ fun TrkType.toXmlString() = """
<trk> <trk>
@ -169,22 +166,45 @@ fun TrkType.toXmlString() = """
${toXmlString(this.cmt, "cmt")} ${toXmlString(this.cmt, "cmt")}
${toXmlString(this.desc, "desc")} ${toXmlString(this.desc, "desc")}
${toXmlString(this.src, "src")} ${toXmlString(this.src, "src")}
${this.link?.toXmlString()} ${this.link?.toXmlString() ?: ""}
${toXmlString(this.number, "number")} ${toXmlString(this.number, "number")}
${toXmlString(this.type, "type")} ${toXmlString(this.type, "type")}
${this.extensions?.toXmlString()} ${this.extensions?.toXmlString() ?: ""}
${this.trkseg?.toXmlString()} ${this.trkseg?.toXmlString() ?: ""}
</trk> </trk>
""".trim() """.trim().removeEmptyStrings()
fun List<WptType>.toXmlString(nodeName: String?) = fun LinkType.toXmlString() = """
this.joinToString(prefix = "", postfix = "", separator = "") { <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) it.toXmlString(nodeName)
} }
fun List<ExtensionType>.toXmlString() = this.joinToString(
prefix = "<extensions>\n", postfix = "\n</extensions>", separator = "\n", transform = ExtensionType::toXmlString
)
@JvmName("toXmlStringWptType") @JvmName("toXmlStringWptType")
fun List<WptType>.toXmlString() = fun List<WptType>.toXmlString() = this.joinToString(prefix = "", postfix = "", separator = "") {
this.joinToString(prefix = "", postfix = "", separator = "") {
it.toXmlString() it.toXmlString()
} }
@ -204,76 +224,42 @@ fun List<TrkType>.toXmlString() =
fun List<TrksegType>.toXmlString() = fun List<TrksegType>.toXmlString() =
this.joinToString(prefix = "", postfix = "", separator = "", transform = TrksegType::toXmlString) this.joinToString(prefix = "", postfix = "", separator = "", transform = TrksegType::toXmlString)
fun List<ExtensionType>.toXmlString() = fun toXmlString(value: String?, nodeName: String) = if (value != null) {
this.joinToString( "<${nodeName}>${value}</${nodeName}>"
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()
} else { } else {
"" ""
} }
fun toXmlString(value: Int?, nodeName: String) = fun toXmlString(value: Int?, nodeName: String) = if (value != null) {
if (value != null) { "<${nodeName}>${value}</${nodeName}>"
"""
<${nodeName}>${value}</${nodeName}>
""".trim()
} else { } else {
"" ""
} }
fun toXmlString(value: Double?, nodeName: String) = fun toXmlString(value: Double?, nodeName: String) = if (value != null) {
if (value != null) { "<${nodeName}>${value}</${nodeName}>"
"""
<${nodeName}>${value}</${nodeName}>
""".trim()
} else { } else {
"" ""
} }
fun toXmlString(value: OffsetDateTime?, nodeName: String) = fun toXmlString(value: OffsetDateTime?, nodeName: String) = if (value != null) {
if (value != null) { "<${nodeName}>${value.format(DTF)}</${nodeName}>"
"""
<${nodeName}>${value.format(DTF)}</${nodeName}>
""".trim()
} else { } else {
"" ""
} }
fun toXmlString(value: Map<String, String>?) = fun toXmlString(value: Map<String, String>?) = value?.entries?.joinToString(separator = "") {
value?.entries?.joinToString(separator = "") {
" ${it.key}=\"${it.value}\"" " ${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 { class GpxWriter {
companion object { companion object {
const val HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 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 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" const val SCHEMA_LOCATION = "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
internal val DTF = DateTimeFormatterBuilder() internal val DTF =
.append(ISO_LOCAL_DATE_TIME) // use the existing formatter for date time DateTimeFormatterBuilder().append(ISO_LOCAL_DATE_TIME) // use the existing formatter for date time
.appendOffset("+HH:MM", "+00:00") // set 'noOffsetText' to desired '+00:00' .appendOffset("+HH:MM", "+00:00") // set 'noOffsetText' to desired '+00:00'
.toFormatter() .toFormatter()
} }

View File

@ -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. 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"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with 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 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 java.time.*
import kotlin.test.assertEquals import kotlin.test.assertEquals
class GpxWriterTest : FunSpec() { class GpxWriterTest {
init { @DisplayName("test minimum")
test("first test") { @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( val clock = Clock.fixed(
LocalDateTime.of(2022, 9, 24, 15, 4, 0, 0).toInstant(ZoneOffset.ofHours(3)), ZoneId.of("Europe/Moscow") 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> <cmt>comment track 1</cmt>
<desc>desc track 1</desc> <desc>desc track 1</desc>
<src>src track 1</src> <src>src track 1</src>
null
<number>1234</number> <number>1234</number>
<type>type 1</type> <type>type 1</type>
null
<trkseg> <trkseg>
<trkpt lat="14.64736838389662" lon="7.93212890625"> <trkpt lat="14.64736838389662" lon="7.93212890625">
<ele>10.0</ele> <ele>10.0</ele>
@ -402,9 +463,13 @@ class GpxWriterTest : FunSpec() {
</trkseg> </trkseg>
</trk> </trk>
</gpx> </gpx>
""".trim(), """.trim()
.lineSequence()
.map {
it.trim()
}
.joinToString("\n"),
gpxType.toXmlString(clock) gpxType.toXmlString(clock)
) )
} }
} }
}