Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: recursively flatten files #8

Merged
merged 7 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions src/main/kotlin/XmlIncludeFlattener.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.tacascer

import io.github.oshai.kotlinlogging.KotlinLogging
import org.jdom2.Document
import org.jdom2.Element
import org.jdom2.filter.Filters
import org.jdom2.input.SAXBuilder
Expand Down Expand Up @@ -35,27 +36,37 @@ class XmlIncludeFlattener(private val input: Path, format: Format = Format.getPr
* @throws Exception if an error occurs during processing
*/
fun process(): String {
doProcess()
val output = process(inputDocument)
return StringWriter().use {
outputter.output(inputDocument, it)
outputter.output(output, it)
it.toString()
}
}

private fun doProcess() {
inputDocument.getDescendants(Filters.element()).filter { it.name == "include" }.forEach {
it.detach()
inlineInclude(it)
}
private fun process(inputDocument: Document): Document {
val output = inputDocument.clone()
val includeElements = output.getDescendants(Filters.element()).filter { it.name == "include" }
includeElements.forEach(Element::detach)
includeElements
.map { it.toDocument().inline() }
.forEach { includedDocument ->
output.rootElement.addContent(
includedDocument.rootElement.getDescendants(Filters.element()).map(Element::clone)
)
}
return output
}

private fun inlineInclude(include: Element) {
val includedSchema = include.getAttributeValue("schemaLocation")
private fun Element.toDocument(): Document {
val includedSchema = getAttributeValue("schemaLocation")
logger.info { "Processing included schema: $includedSchema" }
val includedDocument = saxBuilder.build(URI(includedSchema).toPath().inputStream())
includedDocument.rootElement.getDescendants(Filters.element()).toList().forEach {
it.detach()
inputDocument.rootElement.addContent(it)
}
return includedDocument
}

private fun Document.inline(): Document {
val inlinedDocument = process(this)
val output = inlinedDocument.clone()
return output
}
}
45 changes: 43 additions & 2 deletions src/test/kotlin/XmlIncludeFlattenerTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import io.github.tacascer.XmlIncludeFlattener
import io.kotest.core.spec.style.FunSpec
import io.kotest.engine.spec.tempdir
Expand Down Expand Up @@ -37,6 +36,48 @@ class XmlIncludeFlattenerTest : FunSpec({
<xs:element name="sampleOne" type="xs:string" />
</xs:schema>
""".trimIndent()
XmlIncludeFlattener(includingFile).process().trimIndent() shouldBe expectedText
XmlIncludeFlattener(includingFile).process().trimIndent() shouldBe expectedText
}

test("given one xsd that includes another xsd that includes another, then should return a single xsd with all elements") {
val testDir = tempdir()
val includingFile = testDir.toPath().resolve("sample.xsd").createFile()
val includedFile = testDir.toPath().resolve("sample_1.xsd").createFile()
val includedFile2 = testDir.toPath().resolve("sample_2.xsd").createFile()
@Language("XML") val includingFileText = """
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.sample.com">
<xs:include schemaLocation="${testDir.toPath().resolve("sample_1.xsd").toUri()}"/>
<xs:element name="sample" type="xs:string"/>
</xs:schema>
""".trimIndent()
includingFile.writeText(
includingFileText
)
@Language("XML") val includedFileText = """
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="${testDir.toPath().resolve("sample_2.xsd").toUri()}"/>
<xs:element name="sampleOne" type="xs:string"/>
</xs:schema>
""".trimIndent()
includedFile.writeText(includedFileText)
@Language("XML") val includedFile2Text = """
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="sampleTwo" type="xs:string"/>
</xs:schema>
""".trimIndent()
includedFile2.writeText(includedFile2Text)

@Language("XML") val expectedText = """
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.sample.com">
<xs:element name="sample" type="xs:string" />
<xs:element name="sampleOne" type="xs:string" />
<xs:element name="sampleTwo" type="xs:string" />
</xs:schema>
""".trimIndent()
XmlIncludeFlattener(includingFile).process().trimIndent() shouldBe expectedText
}
})