I poked around and couldn't find any good examples of trying to make a task to do simple XSLT on some source files. (Specifically, I was trying to get an HTML view of checkstyle's report because the XML makes my head hurt...)

Maybe there is something already built into gradle that I missed but anyway, creating my own XSLT task wasn't 'too' bad.

I ended up cobbling from some of the gradle build files, specifically the docbook to HTML task and here's what I came up with:


import javax.xml.transform.TransformerFactory
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource

class Xslt extends SourceTask {

  @OutputFile @Optional
  File destFile

  @OutputDirectory @Optional
  File destDir

  @InputFile
  File stylesheetFile

  @TaskAction
  def transform() {
    if (!((destFile != null) ^ (destDir != null))) {
throw new InvalidUserDataException("Must specify output file or dir.")
    }

    def factory = TransformerFactory.newInstance()
    def transformer = factory.newTransformer(
                          new StreamSource(stylesheetFile))

    source.visit { FileVisitDetails fvd ->
      if (fvd.isDirectory()) {
        return
      }

      File d = destFile;
      if( d == null )
        d = new File( destDir, fvd.file.name )

      transformer.transform(new StreamSource(fvd.file),
                            new StreamResult(d))
    }
  }
}


It seems to work for my limited use-cases but I'm totally open to criticism. Still a little new to groovy so some of the semantics might be off.

Maybe someone else finds it useful? Seems like this (or something similar) would be a good task to have built in.

-Paul


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email


Reply via email to