org.springframework.batch.item.xml
Class StaxEventItemReader

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

public class StaxEventItemReader
extends ExecutionContextUserSupport
implements ItemReader, ItemStream, InitializingBean

Input source for reading XML input based on StAX. It extracts fragments from the input XML document which correspond to records for processing. The fragments are wrapped with StartDocument and EndDocument events so that the fragments can be further processed like standalone XML documents.

Author:
Robert Kasanicky

Constructor Summary
StaxEventItemReader()
           
 
Method Summary
 void afterPropertiesSet()
          Ensure that all required dependencies for the ItemReader to run are provided after all properties have been set.
 void close(ExecutionContext executionContext)
          If any resources are needed for the stream to operate they need to be destroyed here.
 void mark()
          Mark is supported as long as this ItemStream is used in a single-threaded environment.
protected  boolean moveCursorToNextFragment(javax.xml.stream.XMLEventReader reader)
          Responsible for moving the cursor before the StartElement of the fragment root.
 void open(ExecutionContext executionContext)
          Open the stream for the provided ExecutionContext.
 Object read()
          Read in the next root element from the file, and return it.
 void reset()
          Reset the stream to the last mark.
 void setFragmentDeserializer(EventReaderDeserializer eventReaderDeserializer)
           
 void setFragmentRootElementName(String fragmentRootElementName)
           
 void setResource(Resource resource)
           
 void setSaveState(boolean saveState)
          Set the flag that determines whether to save internal data for ExecutionContext.
 void update(ExecutionContext executionContext)
          Indicates that the execution context provided during open is about to be saved.
 
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
 

Constructor Detail

StaxEventItemReader

public StaxEventItemReader()
Method Detail

read

public Object read()
Read in the next root element from the file, and return it.

Specified by:
read in interface ItemReader
Returns:
the next available record, if none exist, return null
See Also:
ItemReader.read()

close

public void close(ExecutionContext executionContext)
Description copied from interface: ItemStream
If any resources are needed for the stream to operate they need to be destroyed here. Once this method has been called all other methods (except open) may throw an exception.

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

open

public void open(ExecutionContext executionContext)
Description copied from interface: ItemStream
Open the stream for the provided ExecutionContext.

Specified by:
open in interface ItemStream

setResource

public void setResource(Resource resource)

setFragmentDeserializer

public void setFragmentDeserializer(EventReaderDeserializer eventReaderDeserializer)
Parameters:
eventReaderDeserializer - maps xml fragments corresponding to records to objects

setFragmentRootElementName

public void setFragmentRootElementName(String fragmentRootElementName)
Parameters:
fragmentRootElementName - name of the root element of the fragment TODO String can be ambiguous due to namespaces, use QName?

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Ensure that all required dependencies for the ItemReader to run are provided after all properties have been set.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
IllegalArgumentException - if the Resource, FragmentDeserializer or FragmentRootElementName is null, or if the root element is empty.
IllegalStateException - if the Resource does not exist.
Exception
See Also:
InitializingBean.afterPropertiesSet()

update

public void update(ExecutionContext executionContext)
Description copied from interface: ItemStream
Indicates that the execution context provided during open is about to be saved. If any state is remaining, but has not been put in the context, it should be added here.

Specified by:
update in interface ItemStream
Parameters:
executionContext - to be updated
See Also:
ItemStream.update(ExecutionContext)

moveCursorToNextFragment

protected boolean moveCursorToNextFragment(javax.xml.stream.XMLEventReader reader)
Responsible for moving the cursor before the StartElement of the fragment root. This implementation simply looks for the next corresponding element, it does not care about element nesting. You will need to override this method to correctly handle composite fragments.

Returns:
true if next fragment was found, false otherwise.

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:
AbstractItemReader.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

setSaveState

public void setSaveState(boolean saveState)
Set the flag that determines whether to save internal data for ExecutionContext. Only switch this to false if you don't want to save any state from this stream, and you don't need it to be restartable.

Parameters:
saveState - flag value (default true)


Copyright © 2008 SpringSource. All Rights Reserved.