reportsMakerXML.groovy
import net.sf.jasperreports.engine.*
import net.sf.jasperreports.engine.export.*
 
def oldStdOut=System.out
 
//"репозиторий" откомпилированных шаблонов от iReport
// (*.jrxml)--> iReport--> (*.jasper)
def jasperDir='c:/DROPBOX/edunet/2014/is10-99/jrxml/'
def groovyDir='c:/DROPBOX/edunet/2014/is10-99/groovy/'
def jarDir='c:/DROPBOX/edunet/2014/is10-99/jar/'
 
def reportsMaker={template, xmlURL, xf, params->
  def jasperFile= "${jasperDir}${template}.jasper"
  def docFile= "$jasperDir$template$xf".toString()
 
  def jasperReport = net.sf.jasperreports.engine.util.JRLoader.loadObject(new File(jasperFile))
  def queryText=jasperReport.getQuery().getText()
  def defaults = net.sf.jasperreports.engine.fill.JRParameterDefaultValuesEvaluator.
    evaluateParameterDefaultValues(jasperReport, new HashMap())
 
  def par, XMLrequest = new StringBuffer()
  def map = [XML_URL:xmlURL]
  params.tokenize('&').each{
    def xx=it.tokenize('=')
    map[xx[0]]= xx[1] != null ? xx[1] : null
  }
  println map
  defaults.each{k,v->
    par = ""
    if(k.startsWith("XML_POST_PARAM_")) par= k[15..-1]
    if(k.startsWith("XML_GET_PARAM_")) par= k[14..-1]
    if(par!=""){
      if(map[par]) XMLrequest<<par<<"="<<map[par]<<"&"
      else XMLrequest<<$par<<"="<<v<<"&" // defaults
    }
  }
 
  //запрос XML-контента
  def con, jasperPrint, xml
  def bufStr = new ByteArrayOutputStream()
  if (xmlURL.startsWith("http://") || xmlURL.startsWith("https://")){ //remote XML
    println "${xmlURL}/$XMLrequest $queryText"
    con = new net.sf.jasperreports.engine.data.JRXmlDataSource("${xmlURL}/$XMLrequest", queryText)
  } else {
    if (xmlURL.endsWith(".groovy")){// groovy XML
      println "${groovyDir}$xmlURL $XMLrequest"
      System.out = new PrintStream(bufStr)
      new GroovyShell().run(new File("${groovyDir}$xmlURL"), "$XMLrequest")
    }
    if (xmlURL.endsWith(".jar")){// jar XML
      println "${jarDir}$xmlURL $XMLrequest"
      System.out = new PrintStream(bufStr)
      getClass().classLoader.rootLoader.addURL(new File("${jarDir}$xmlURL").toURL())
      Class.forName("$xmlURL"[0..-5]).newInstance().main("$XMLrequest")
    }
    System.out = oldStdOut
    xml= bufStr.toString()
    //println xml
    def doc=net.sf.jasperreports.engine.util.JRXmlUtils.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")))
    con= new net.sf.jasperreports.engine.data.JRXmlDataSource(doc,queryText)
  }
  //заполнение шаблона контентом
  jasperPrint = JasperFillManager.fillReport(jasperFile, map, con)
 
  def exporter, exporterParameter
  switch(xf){
    case ".html":
      JasperExportManager.exportReportToHtmlFile(jasperPrint,docFile);
      break;
    case ".rtf":
      exporter= new JRRtfExporter()
      exporterParameter= new JRExporterParameter()
      exporter.setParameter(exporterParameter.JASPER_PRINT,jasperPrint)
      exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, docFile);
      exporter.exportReport()
      break
    case ".xls":
      exporter= new JExcelApiExporter()
      exporterParameter= new JRXlsExporterParameter()
      exporter.setParameter(exporterParameter.IS_DETECT_CELL_TYPE, true)
      exporter.setParameter(exporterParameter.IS_WHITE_PAGE_BACKGROUND, false)
      exporter.setParameter(exporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, true)
      exporter.setParameter(exporterParameter.JASPER_PRINT,jasperPrint)
      exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, docFile);
      exporter.exportReport()
      break
    default: //.pdf
      JasperExportManager.exportReportToPdfFile(jasperPrint, "$jasperDir${template}.pdf")
   }
   java.awt.Desktop.getDesktop().browse(new URI(docFile))
}
 
def args = this.args
if(args){
  reportsMaker args[0], args[1], args[2], args[3].replace("'","&")// !! &
  return
}
 
reportsMaker 'invalidHTTP', 'http://54.88.19.150:8000/groovy/invalid', '.pdf', "pLevel=h2&pServ=2&pType=a&pStamp=201101061100"
reportsMaker 'binlogHTTP', 'http://54.88.19.150:8000/groovy/binlog', '.pdf', "pCode=2RH31S101XG01&pStart=200910061523&pStop=201103281305"
 
reportsMaker 'invalidHTTP', 'invalid.groovy', '.rtf', "pLevel=h2&pServ=2&pType=a&pStamp=201101061100"
reportsMaker 'binlogHTTP', 'binlog.groovy', '.rtf', "pCode=1RH31S101XG01&pStart=200910061523&pStop=201103281305"
 
reportsMaker 'invalidHTTP', 'invalid.jar', '.html', "pLevel=h2&pServ=2&pType=a&pStamp=201101061100"
reportsMaker 'binlogHTTP', 'binlog.jar', '.html', "pCode=1RH31S101XG01&pStart=200910061523&pStop=201103281305"

c:\opt\Groovy\bin\groovy.bat -cp c:\DROPBOX\amazonaws\src\jrlib.jar reportsMakerXML.groovy 
  binlogHTTP binlog.groovy .html pCode=2RH31S101XG01'pStart=200910061523'pStop=201103281305