Posted By

gripnrip on 06/23/09


Tagged

java xml properties groovy


Versions (?)

Create XML from Template and Properties


 / Published in: Groovy
 

Groovy script that creates an XML file using an XML template containing ${params} and a standard Java Properties file

  1. import groovy.text.XmlTemplateEngine
  2. import java.util.Properties
  3. import java.io.File
  4. import java.io.FileWriter
  5.  
  6. //Takes a java props file, an XML template file and creates the given output file
  7. void createFile(propertiesFile, templateFileName, outputFileName) {
  8. // read properties file given
  9. def props = new Properties()
  10. props.load(new FileInputStream(new File(propertiesFile)))
  11.  
  12. // map to the bindings
  13. def bindings = [:]
  14. props.propertyNames().each{prop->
  15. bindings[prop]=props.getProperty(prop)
  16. }
  17.  
  18. // create the template and make the output file
  19. def engine = new XmlTemplateEngine()
  20. def templateFile = new File(templateFileName)
  21. def output = engine.createTemplate(templateFile).make(bindings)
  22.  
  23. def outputFile = new File(outputFileName)
  24. def parentFile = outputFile.getParentFile()
  25. if (parentFile != null) parentFile.mkdirs()
  26. def fileWriter = new FileWriter(outputFile)
  27. fileWriter.write(output.toString())
  28. fileWriter.close()
  29. }

Report this snippet  

You need to login to post a comment.