Skip to content

Instantly share code, notes, and snippets.

@ararog
Created November 5, 2013 10:34
Show Gist options
  • Save ararog/7317049 to your computer and use it in GitHub Desktop.
Save ararog/7317049 to your computer and use it in GitHub Desktop.
A tool to apply a xml stylesheet into a source file and output the styled version to another file.
import javax.xml.transform.TransformerFactory
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource
def cli = new CliBuilder( usage: 'groovy TransformXml.groovy -h -i inputfile -o output -x xsltfile...')
cli.h(longOpt:'help', 'usage information')
cli.i(argName:'input', longOpt:'input', args:1, required:true, type:GString, 'Directory/file for input')
cli.o(argName:'output', longOpt:'output', args:1, required:true, type:GString, 'Direcoty/file for output')
cli.x(argName:'xsltfile', longOpt:'xsltfile', args:1, required:true, type:GString, 'The transformation file')
def opt = cli.parse(args)
if(! opt) return
def input = new File(opt.i)
def output = new File(opt.o)
def xsltFile = new File(opt.x)
if(input.directory) {
def p = ~/.*xml/
input.eachFileMatch(p) {
inputFile ->
def factory = TransformerFactory.newInstance()
def transformer = factory.newTransformer(new StreamSource(xsltFile))
transformer.transform(new StreamSource(inputFile), new StreamResult(new File(output.absolutePath + File.separator + inputFile.name)))
}
}
else {
def factory = TransformerFactory.newInstance()
def transformer = factory.newTransformer(new StreamSource(xsltFile))
transformer.transform(new StreamSource(input), new StreamResult(output))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment