Created
November 5, 2013 10:34
-
-
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.
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
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