org.springframework.batch.item.file
Class FlatFileItemReader

java.lang.Object
  extended by org.springframework.batch.item.ExecutionContextUserSupport
      extended by org.springframework.batch.item.file.FlatFileItemReader
All Implemented Interfaces:
ItemReader, ItemStream, InitializingBean

public class FlatFileItemReader
extends ExecutionContextUserSupport
implements ItemReader, ItemStream, InitializingBean

This class represents a ItemReader, that reads lines from text file, tokenizes them to structured tuples (FieldSets) instances and maps the FieldSets to domain objects. The location of the file is defined by the resource property. To separate the structure of the file, LineTokenizer is used to parse data obtained from the file.
A FlatFileItemReader is not thread safe because it maintains state in the form of a ResourceLineReader. Be careful to configure a FlatFileItemReader using an appropriate factory or scope so that it is not shared between threads.

This class supports restart, skipping invalid lines and storing statistics. It can be configured to setup FieldSet column names from the file header, skip given number of lines at the beginning of the file.

Author:
Waseem Malik, Tomas Slanina, Robert Kasanicky, Dave Syer

Field Summary
static String DEFAULT_CHARSET
           
 
Constructor Summary
FlatFileItemReader()
           
 
Method Summary
 void afterPropertiesSet()
           
 void close(ExecutionContext executionContext)
          Close and null out the reader.
protected  LineReader getReader()
           
 void mark()
          Mark is supported as long as this ItemStream is used in a single-threaded environment.
 void open(ExecutionContext executionContext)
          Initialize the reader if necessary.
 Object read()
          Reads a line from input, tokenizes is it using the setLineTokenizer(LineTokenizer) and maps to domain object using setFieldSetMapper(FieldSetMapper).
 void reset()
          Reset the stream to the last mark.
 void setComments(String[] comments)
          Setter for comment prefixes.
 void setEncoding(String encoding)
          Setter for the encoding for this input source.
 void setFieldSetMapper(FieldSetMapper fieldSetMapper)
          Set the FieldSetMapper to be used for each line.
 void setFirstLineIsHeader(boolean firstLineIsHeader)
          Indicates whether first line is a header.
 void setLinesToSkip(int linesToSkip)
          Public setter for the number of lines to skip at the start of a file.
 void setLineTokenizer(LineTokenizer lineTokenizer)
           
 void setRecordSeparatorPolicy(RecordSeparatorPolicy recordSeparatorPolicy)
          Public setter for the recordSeparatorPolicy.
 void setResource(Resource resource)
          Setter for resource property.
 void setSaveState(boolean saveState)
          Set the boolean indicating whether or not state should be saved in the provided ExecutionContext during the ItemStream call to update.
 void update(ExecutionContext executionContext)
          This method returns the execution attributes for the reader.
 
Methods inherited from class org.springframework.batch.item.ExecutionContextUserSupport
getKey, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHARSET

public static final String DEFAULT_CHARSET
See Also:
Constant Field Values
Constructor Detail

FlatFileItemReader

public FlatFileItemReader()
Method Detail

open

public void open(ExecutionContext executionContext)
          throws ItemStreamException
Initialize the reader if necessary.

Specified by:
open in interface ItemStream
Throws:
IllegalStateException - if the resource cannot be opened
ItemStreamException

close

public void close(ExecutionContext executionContext)
           throws ItemStreamException
Close and null out the reader.

Specified by:
close in interface ItemStream
Parameters:
executionContext - TODO
Throws:
ItemStreamException

read

public Object read()
            throws Exception
Reads a line from input, tokenizes is it using the setLineTokenizer(LineTokenizer) and maps to domain object using setFieldSetMapper(FieldSetMapper).

Specified by:
read in interface ItemReader
Throws:
Exception - if an underlying resource is unavailable.
See Also:
ItemReader.read()

update

public void update(ExecutionContext executionContext)
This method returns the execution attributes for the reader. It returns the current Line Count which can be used to reinitialise the batch job in case of restart.

Specified by:
update in interface ItemStream
Parameters:
executionContext - to be updated

mark

public void mark()
Mark is supported as long as this ItemStream is used in a single-threaded environment. The state backing the mark is a single counter, keeping track of the current position, so multiple threads cannot be accommodated.

Specified by:
mark in interface ItemReader
See Also:
ItemReader.mark()

reset

public void reset()
Description copied from interface: ItemReader
Reset the stream to the last mark. After a reset the stream state will be such that changes (items read or written) since the last call to mark will not be visible after a call to close.
In a multi-threaded setting implementations have to ensure that only the state from the current thread is reset.

Specified by:
reset in interface ItemReader

getReader

protected LineReader getReader()
Returns:
line reader used to read input file

setResource

public void setResource(Resource resource)
Setter for resource property. The location of an input stream that can be read.

Parameters:
resource -

setRecordSeparatorPolicy

public void setRecordSeparatorPolicy(RecordSeparatorPolicy recordSeparatorPolicy)
Public setter for the recordSeparatorPolicy. Used to determine where the line endings are and do things like continue over a line ending if inside a quoted string.

Parameters:
recordSeparatorPolicy - the recordSeparatorPolicy to set

setComments

public void setComments(String[] comments)
Setter for comment prefixes. Can be used to ignore header lines as well by using e.g. the first couple of column names as a prefix.

Parameters:
comments - an array of comment line prefixes.

setFirstLineIsHeader

public void setFirstLineIsHeader(boolean firstLineIsHeader)
Indicates whether first line is a header. If the tokenizer is an AbstractLineTokenizer and the column names haven't been set already then the header will be used to setup column names. Default is false.


setLineTokenizer

public void setLineTokenizer(LineTokenizer lineTokenizer)
Parameters:
lineTokenizer - tokenizes each line from file into FieldSet.

setFieldSetMapper

public void setFieldSetMapper(FieldSetMapper fieldSetMapper)
Set the FieldSetMapper to be used for each line.

Parameters:
fieldSetMapper -

setLinesToSkip

public void setLinesToSkip(int linesToSkip)
Public setter for the number of lines to skip at the start of a file. Can be used if the file contains a header without useful (column name) information, and without a comment delimiter at the beginning of the lines.

Parameters:
linesToSkip - the number of lines to skip

setEncoding

public void setEncoding(String encoding)
Setter for the encoding for this input source. Default value is DEFAULT_CHARSET.

Parameters:
encoding - a properties object which possibly contains the encoding for this input file;

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

setSaveState

public void setSaveState(boolean saveState)
Set the boolean indicating whether or not state should be saved in the provided ExecutionContext during the ItemStream call to update. Setting this to false means that it will always start at the beginning.

Parameters:
saveState -


Copyright © 2008 SpringSource. All Rights Reserved.