Java Reference
In-Depth Information
techniques that require custom parsing. In the next section, you will look at how to implement your
own LineTokenizer to be able to handle custom file formats.
Custom Record Parsing
In the previous section you looked at how to address the ability to tweak the mapping of fields in your
file to the fields of your domain object by creating a custom FieldSetMapper implementation. However,
that is not the only option. Instead, you can create your own LineTokenizer implementation. This will
allow you to parse each record however you need.
Like the FieldSetMapper interface, the
org.springframework.batch.item.file.transform.LineTokenizer interface has a single method:
tokenize . Listing 7-16 shows the LineTokenizer interface.
Listing 7-16. LineTokenizer interface
package org.springframework.batch.item.file.transform;
public interface LineTokenizer {
FieldSet tokenize(String line);
For this approach you will use the same delimited input file you used previously; however, since the
domain object has the address number and the street combined into a single field, you will combine
those two tokens into a single field in the FieldSet. Listing 7-17 shows the CustomerFileLineTokenizer.
Listing 7-17. CustomerFileLineTokenizer
package com.apress.springbatch.chapter7;
import java.util.ArrayList;
import java.util.List;
import org.springframework.batch.item.file.transform.DefaultFieldSetFactory;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.batch.item.file.transform.FieldSetFactory;
import org.springframework.batch.item.file.transform.LineTokenizer;
public class CustomerFileLineTokenizer implements LineTokenizer {
private String delimiter;
private String names;
private FieldSetFactory fieldSetFactory = new DefaultFieldSetFactory();
public FieldSet tokenize(String record) {
String[] fields = record.split(delimiter);
List<String> parsedFields = new ArrayList<String>();
Search WWH ::

Custom Search