Skip to content

Instantly share code, notes, and snippets.

@welshstew
Last active August 29, 2015 14:18
Show Gist options
  • Save welshstew/2f659cfa564341de2f91 to your computer and use it in GitHub Desktop.
Save welshstew/2f659cfa564341de2f91 to your computer and use it in GitHub Desktop.
Builds a skeleton for building an XML Document with groovy
import groovy.util.slurpersupport.NodeChild
/**
* Generates the groovy script to create the xml document builder markup skeleton based
* on an existing xml input
* Created by swinchester on 31/08/2014.
*/
void nodeTraverser(node, parent, sb, root, markupBuilderName){
printNode(node, sb, root, markupBuilderName)
node.children().each { NodeChild child ->
nodeTraverser(child, node, sb, root, markupBuilderName)
}
if(node.children().size() > 0){
def thisLine = "}" + System.getProperty("line.separator")
sb.append(thisLine)
}
}
void printNode(node, sb, root, markupBuilderName){
def nodePath;
def nodePathList = new ArrayList<String>()
if(node.equals(root)){
def thisLine
thisLine = markupBuilderName + "." + normalizeNodeName(node) + " (" + printAttributes(node) + ") {" + System.getProperty("line.separator")
// thisLine = markupBuilderName + "." + node.name() + " (xmlns: \'" + node.namespaceURI() + "\') {" + System.getProperty("line.separator")
print thisLine
sb.append(thisLine)
}else {
if (node.children().size() == 0) {
def atts = printAttributes(node)
def thisLine = normalizeNodeName(node) + "(" + printAttributes(node) + ")" + System.getProperty("line.separator")
print thisLine
sb.append(thisLine)
} else {
def thisLine = normalizeNodeName(node) + "(" + printAttributes(node) + ") { " + System.getProperty("line.separator")
print thisLine
sb.append(thisLine)
}
}
//is this the last child in the list of the parent?
// if (node.parent().children()[node.parent().children().size() - 1].name().equals(node.name())) {
// //then close it off
// def thisLine = "}" + System.getProperty("line.separator")
// print thisLine
// sb.append(thisLine)
// }
}
def normalizeNodeName(node){
def nodeName = node.name()
if(nodeName.contains('-')){
nodeName = "\'" + node.name() + "\'"
}
return nodeName.trim()
}
def printAttributes(node){
def attList = []
if(node.attributes()){
node.attributes().each(){
attList.add(it.key + ": " + "\'" + it.value + "\'")
}
}
return attList.join(', ')
}
StringBuilder sb = new StringBuilder()
sb.append("import groovy.xml.MarkupBuilder" + System.getProperty("line.separator"))
def count = 1
args.each { String filePath ->
File xmlInput = new File(filePath)
root = new XmlSlurper().parseText(xmlInput.text);
sb.append("def xmlStringWriter" + count + " = new StringWriter()\n" +
"def xmlout" + count + " = new MarkupBuilder(xmlStringWriter" + count + ")\n" +
"\n" +
"xmlout" + count + ".mkp.xmlDeclaration(version: \"1.0\", encoding: \"UTF-8\")\n")
nodeTraverser(root, null, sb, root, "xmlout" + count)
sb.append("println xmlStringWriter" + count + ".toString()")
count++
println sb.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment