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>
</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>

View File

@ -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,16 +113,13 @@ 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 {
} else {
this.toXmlString("wpt")
}
}
@JvmName("toXmlStringNamed")
fun WptType.toXmlString(nodeName: String) = """
@ -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,24 +166,47 @@ 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()
}
}
@JvmName("toXmlStringLinkType")
fun List<LinkType>.toXmlString() =
@ -204,75 +224,41 @@ 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()
} else {
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()
} else {
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()
} else {
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()
} else {
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 {
@ -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()
}

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.
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")
)
@ -294,7 +357,7 @@ class GpxWriterTest : FunSpec() {
<text>text</text>
<type>hyperlink</type>
</link>
<link href="http://link2-to.site.href">
<link href="http://link2-to.site.href">
<text>text2</text>
<type>hyperlink2</type>
</link>
@ -308,9 +371,9 @@ class GpxWriterTest : FunSpec() {
<ageofgpsdata>44</ageofgpsdata>
<dgpsid>88</dgpsid>
<extensions>
<extension1 first="second" third="fours"></extension1>
<extension2 aa="bb" cc="dd"></extension2>
</extensions>
<extension1 first="second" third="fours"></extension1>
<extension2 aa="bb" cc="dd"></extension2>
</extensions>
</wpt>
<rte>
<name>rte name</name>
@ -324,8 +387,8 @@ class GpxWriterTest : FunSpec() {
<number>1234</number>
<type>route</type>
<extensions>
<ext-1>value1</ext-1>
</extensions>
<ext-1>value1</ext-1>
</extensions>
<wpt lat="14.64736838389662" lon="7.93212890625">
<ele>10.0</ele>
<time>2022-09-24T15:04:00+03:00</time>
@ -339,7 +402,7 @@ class GpxWriterTest : FunSpec() {
<text>text</text>
<type>hyperlink</type>
</link>
<link href="http://link2-to.site.href">
<link href="http://link2-to.site.href">
<text>text2</text>
<type>hyperlink2</type>
</link>
@ -353,9 +416,9 @@ class GpxWriterTest : FunSpec() {
<ageofgpsdata>44</ageofgpsdata>
<dgpsid>88</dgpsid>
<extensions>
<extension1 first="second" third="fours"></extension1>
<extension2 aa="bb" cc="dd"></extension2>
</extensions>
<extension1 first="second" third="fours"></extension1>
<extension2 aa="bb" cc="dd"></extension2>
</extensions>
</wpt>
</rte>
<trk>
@ -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>
@ -381,7 +442,7 @@ class GpxWriterTest : FunSpec() {
<text>text</text>
<type>hyperlink</type>
</link>
<link href="http://link2-to.site.href">
<link href="http://link2-to.site.href">
<text>text2</text>
<type>hyperlink2</type>
</link>
@ -395,16 +456,20 @@ class GpxWriterTest : FunSpec() {
<ageofgpsdata>44</ageofgpsdata>
<dgpsid>88</dgpsid>
<extensions>
<extension1 first="second" third="fours"></extension1>
<extension2 aa="bb" cc="dd"></extension2>
</extensions>
<extension1 first="second" third="fours"></extension1>
<extension2 aa="bb" cc="dd"></extension2>
</extensions>
</trkpt>
</trkseg>
</trk>
</gpx>
""".trim(),
""".trim()
.lineSequence()
.map {
it.trim()
}
.joinToString("\n"),
gpxType.toXmlString(clock)
)
}
}
}