This repository has been archived by the owner on Aug 19, 2020. It is now read-only.
XML file editing with the groovy XML library is unwieldy in kotlin #225
Labels
Comments
Here's another example: fun getEclipseFormatterSettings() : Collection<Pair<String, String>> {
val eclipseFormatterSettingsXml = file("${rootProject.projectDir}/eclipse-formatter-settings.xml")
val eclipseFormatter = XmlParser().parse(eclipseFormatterSettingsXml)
return ((eclipseFormatter["profile"] as NodeList)[0] as Node).children().map {
val node = it as Node
node.attribute("id").toString() to node.attribute("value").toString()
}
} |
Hi @JLLeitschuh, thanks for the report.
withXml {
def moduleRoot = it.asElement() // instead of asNode()
} I didn't try that myself yet, I'll give it a shot soon, in the meantime I hope this helps. |
I did end up using that to solve the problem. Good suggestion. |
@JLLeitschuh would you mind sharing a snippet of the resulting build script? |
configure<IdeaModel> {
module.apply {
generatedSourceDirs.apply {
add(generatedDaoFile)
add(generatedJobFile)
}
iml.apply {
withXml {
val moduleRoot = it.asElement()
val doc = moduleRoot.ownerDocument
fun NodeList.asElementList() = asList().filterIsInstance<Element>()
val facetManager = moduleRoot
.getElementsByTagName("component")
.asElementList()
.singleOrNull { it.getAttribute("name") == "FacetManager" }
?: moduleRoot
.appendChild(doc.createElement("component").apply {
setAttribute("name", "FacetManager")
}) as Element
// Add spring facet
facetManager
.getElementsByTagName("facet")
.asElementList()
.singleOrNull { it.getAttribute("name") == "Spring" && it.getAttribute("type") == "Spring" }
?: facetManager
.appendChild(doc.createElement("facet").apply {
setAttribute("name", "Spring")
setAttribute("type", "Spring")
appendChild(doc.createElement("configuration"))
})
// Add AspectJ facet
facetManager
.getElementsByTagName("facet")
.asElementList()
.singleOrNull { it.getAttribute("name") == "AspectJ" && it.getAttribute("type") == "AspectJ" }
?: facetManager
.appendChild(doc.createElement("facet").apply {
setAttribute("name", "AspectJ")
setAttribute("type", "AspectJ")
appendChild(doc.createElement("configuration").apply {
appendChild(doc.createElement("option").apply {
setAttribute("name", "aspectPath")
appendChild(doc.createElement("projectLibrary").apply {
appendChild(doc.createElement("option").apply {
setAttribute("name", "name")
setAttribute("value", "Gradle: org.springframework:spring-aspects:$springVersion")
})
})
})
})
})
}
}
}
}
// Helper Utilities
fun NodeList.asList() = (0..length).map { item(it) } |
I just started using kotlinx.dom and it results in much more succinct code. Great project and it is from JetBrains! |
Closing as answered. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Trying to do something like this:
http://mrhaki.blogspot.com/2016/03/gradle-goodness-add-spring-facet-to.html
Is incredibly hard in kotlin because many of the methods rely on gradle's duck typing system.
The text was updated successfully, but these errors were encountered: