Language extensions

In addition to Groovy’s XmlSlurper and JsonSlurper, Platform 6 provides a FixedLengthSlurper, which is created with the format specification of your file and returns a map of named properties with the parsed data.

The arguments to the FixedLengthSlurper’s constructor are the size of the field, the name to assign to the field, and an optional closure to format the object. Any number of these formatting parameters can be passed. If no formatting closure is provided, the data is just returned as a String.

import com.amalto.b2box.groovy.dsl.FixedLengthSlurper
import java.text.SimpleDateFormat

def dateTime = new SimpleDateFormat('yyyyMMddahhmm')
def date = new SimpleDateFormat('yyyyMMdd')
def parser = new FixedLengthSlurper(
  13, 'dateTime', { dateTime.parse(it) },
  4, 'type',
  8, 'processDate', { date.parse(it) },
  9, 'numberRecords', { new Integer(it) },
  11, 'amount', { String str -> new BigDecimal(str[0..str.size() - 3] + '.' + str[str.size() - 3, str.size() - 1]) },
  1, 'typeCode'  

List values = []
new File('./data/ppld.txt').eachLine { String line ->
 if (!line.startsWith('0000') && !line.startsWith('9999'))
 values << parser.parseText(line)

In this example, the dateTime, and processDate properties are stored as Date objects, the numberRecords an Integer, and the type and typeCode properties are Strings. The amount property gets a little extra parsing. The file stores ‘10044’, but that needs to translate into ‘100.44’, so the closure will break apart the string into the integer and decimal values before creating the BigDecimal object.