Created
December 2, 2013 09:34
-
-
Save robvalk/7747112 to your computer and use it in GitHub Desktop.
Simple Data Mapping utility that lets developers map between data structures and formats using Groovy Builder syntax.
This file contains 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
package au.com.sixtree.esb.mapper | |
import au.com.sixtree.esb.mapper.GroovyMap; | |
import au.com.sixtree.esb.mapper.DOMBuilder; | |
import groovy.json.JsonBuilder | |
import groovy.xml.MarkupBuilder | |
import groovy.xml.NamespaceBuilder; | |
import groovy.xml.XmlUtil | |
/** | |
* Data Mapping utility that lets developers map between data structures and formats using Groovy Builder syntax. | |
*/ | |
class GroovyMap { | |
private Object[] inputs | |
private BuilderSupport builder | |
private Object[] helpers | |
private Closure mapping | |
private Closure outputSerialiser | |
private GroovyMap(Object[] inputs, BuilderSupport builder, Object[] helpers, Closure mapping, Closure outputSerialiser) { | |
this.inputs = inputs | |
this.builder = builder | |
this.helpers = helpers | |
this.mapping = mapping | |
this.mapping.delegate = this | |
this.outputSerialiser = outputSerialiser | |
this.outputSerialiser.delegate = this | |
} | |
public Object map() { | |
return outputSerialiser.call(mapping.call()) | |
} | |
public static GroovyMap toXmlDom(Object[] inputs, Object[] helpers, Closure mapping) { | |
def domBuilder = DOMBuilder.newInstance() | |
return new GroovyMap(inputs, NamespaceBuilder.newInstance(domBuilder), helpers, mapping, { it.getOwnerDocument() }) | |
} | |
public static GroovyMap toXmlDom(Object input, Closure mapping) { | |
return toXmlDom([input] as Object[], null, mapping) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment