Last active
March 13, 2019 07:58
-
-
Save geowarin/64931fbf1eb6a4953da62c9acf55222c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.sun.org.apache.xerces.internal.dom.DeferredAttrImpl | |
import org.intellij.lang.annotations.Language | |
import org.w3c.dom.Document | |
import org.w3c.dom.Node | |
import org.w3c.dom.NodeList | |
import java.io.File | |
import javax.xml.parsers.DocumentBuilderFactory | |
import javax.xml.xpath.XPath | |
import javax.xml.xpath.XPathConstants | |
import javax.xml.xpath.XPathFactory | |
val file = File("file.xml") | |
val xPath: XPath = XPathFactory.newInstance().newXPath() | |
class XPathEvaluator(documentFile: File) { | |
private val document = document(documentFile) | |
private fun document(file: File): Document { | |
file.inputStream().use { stream -> | |
val builderFactory = DocumentBuilderFactory.newInstance() | |
val builder = builderFactory.newDocumentBuilder() | |
return builder.parse(stream) | |
} | |
} | |
fun getNodes(@Language("XPath") expression: String): List<Node> { | |
val result = xPath.compile(expression).evaluate(document, XPathConstants.NODESET) as NodeList | |
return result.toList() | |
} | |
fun getNode(@Language("XPath") expression: String): Node { | |
return xPath.compile(expression).evaluate(document, XPathConstants.NODE) as Node | |
} | |
} | |
fun NodeList.toList(): List<Node> { | |
val list: MutableList<Node> = mutableListOf() | |
for (i in 0 until this.length) { | |
list.add(this.item(i)) | |
} | |
return list | |
} | |
fun Node.attr(name: String): String? = this.attrs()[name] | |
fun Node.attrs(): Map<String, String> { | |
val attributesMap = mutableMapOf<String, String>() | |
for (i in 0 until this.attributes.length) { | |
val item = this.attributes.item(i) as DeferredAttrImpl | |
attributesMap[item.name] = item.value | |
} | |
return attributesMap | |
} | |
private fun Node.find(function: (Node) -> Boolean): Node = this.childNodes.toList().find(function)!! | |
private fun Node.filter(function: (Node) -> Boolean): List<Node> = this.childNodes.toList().filter(function) | |
fun main() { | |
val streamName = "GIPS_MEMBERSHIPS" | |
val xpath = XPathEvaluator(file) | |
val root = xpath.getNode("/dictionary/stream[@type='$streamName']") | |
val streamCode = root.attr("type") | |
val streamFullName = root.find { it.nodeName == "i18n" }.attr("en") | |
println("# $streamFullName - $streamCode") | |
println("## Rules") | |
val result = xpath.getNodes("/dictionary/stream[@type='$streamName']/rule[@defaultEnabled=true()]") | |
for (node in result) { | |
val i18n = node.find { it.nodeName == "i18n" } | |
val params = node.filter { it.nodeName == "parameter" } | |
println("### " + i18n.attr("en")) | |
println("`" + node.attr("name") + "`") | |
println("table=" + node.attr("table")) | |
for (param in params) { | |
println(param.attr("name") + "=" + param.attr("value")) | |
} | |
println("") | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment