Tag Archives: closures

Parsing files using Groovy closures

At work I recently created a Groovy automation script that parsed FTP log files and generated a web based report. This script was then added as a scheduled job in the open-source Jenkins continuous integration server. Groovy support is built in to Jenkins.

Groovy certainly made this very easy (from a Java perspective). The script used the Groovy GDK (extensions to Java’s classes) and iteration via closures, first of the target folder, and then on each file’s lines. A regexp was used to grab the info. Finally a Groovy Builder was used to create the HTML page.

After looking at the solution I thought it could be simplified if there was support for this pattern in some kind of template or API. A sample approach is shown below. Of course, this is only applicable for line oriented data files. When data spans multiple lines one must use more stateful approaches or parsing tools.

Updates

  • Sep 23, 2013: Version of this code at a Gist here.
  • Sep 23, 2013: Changed method signature of gather at folder. But, perhaps, the gather at file level does not need the filter closure.

References

  1. Groovy
  2. Closures in Groovy
  3. Builders in Groovy
  4. File in Groovy JDK
  5. Regular Expressions in Groovy
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.