org.springframework.batch.item.xml
Class StaxEventItemWriter

java.lang.Object
  extended by org.springframework.batch.item.util.ExecutionContextUserSupport
      extended by org.springframework.batch.item.xml.StaxEventItemWriter
All Implemented Interfaces:
ItemStream, ItemWriter, InitializingBean

public class StaxEventItemWriter
extends ExecutionContextUserSupport
implements ItemWriter, ItemStream, InitializingBean

An implementation of ItemWriter which uses StAX and EventWriterSerializer for serializing object to XML. This item writer also provides restart, statistics and transaction features by implementing corresponding interfaces. Output is buffered until flush() is called - only then the actual writing to file takes place. The implementation is *not* thread-safe.

Author:
Peter Zozom, Robert Kasanicky

Constructor Summary
StaxEventItemWriter()
           
 
Method Summary
 void afterPropertiesSet()
           
 void clear()
          Clear the output buffer
 void close(ExecutionContext executionContext)
          Flush and close the output source.
protected  void endDocument(javax.xml.stream.XMLEventWriter writer)
          Writes the EndDocument tag manually.
 void flush()
          Writes buffered items to XML stream and marks restore point.
 String getEncoding()
          Get used encoding.
 Map getRootElementAttributes()
          Get attributes of the root element.
 String getRootTagName()
          Get the tag name of the root element.
 String getVersion()
          Get XML version.
 void open(ExecutionContext executionContext)
          Open the output source
 void setEncoding(String encoding)
          Set encoding to be used for output file.
 void setHeaderItems(Object[] headers)
          Setter for the headers.
 void setOverwriteOutput(boolean overwriteOutput)
          Set "overwrite" flag for the output file.
 void setResource(Resource resource)
          Set output file.
 void setRootElementAttributes(Map rootElementAttributes)
          Set the root element attributes to be written.
 void setRootTagName(String rootTagName)
          Set the tag name of the root element.
 void setSaveState(boolean saveState)
           
 void setSerializer(EventWriterSerializer serializer)
          Set Object to XML serializer.
 void setVersion(String version)
          Set XML version to be used for output XML.
 void update(ExecutionContext executionContext)
          Get the restart data.
 void write(Object item)
          Write the value object to internal buffer.
 
Methods inherited from class org.springframework.batch.item.util.ExecutionContextUserSupport
getKey, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StaxEventItemWriter

public StaxEventItemWriter()
Method Detail

setResource

public void setResource(Resource resource)
Set output file.

Parameters:
resource - the output file

setSerializer

public void setSerializer(EventWriterSerializer serializer)
Set Object to XML serializer.

Parameters:
serializer - the Object to XML serializer

getEncoding

public String getEncoding()
Get used encoding.

Returns:
the encoding used

setEncoding

public void setEncoding(String encoding)
Set encoding to be used for output file.

Parameters:
encoding - the encoding to be used

getVersion

public String getVersion()
Get XML version.

Returns:
the XML version used

setVersion

public void setVersion(String version)
Set XML version to be used for output XML.

Parameters:
version - the XML version to be used

getRootTagName

public String getRootTagName()
Get the tag name of the root element.

Returns:
the root element tag name

setRootTagName

public void setRootTagName(String rootTagName)
Set the tag name of the root element. If not set, default name is used ("root").

Parameters:
rootTagName - the tag name to be used for the root element

getRootElementAttributes

public Map getRootElementAttributes()
Get attributes of the root element.

Returns:
attributes of the root element

setRootElementAttributes

public void setRootElementAttributes(Map rootElementAttributes)
Set the root element attributes to be written.

Parameters:
rootElementAttributes - attributes of the root element

setOverwriteOutput

public void setOverwriteOutput(boolean overwriteOutput)
Set "overwrite" flag for the output file. Flag is ignored when output file processing is restarted.

Parameters:
overwriteOutput -

setHeaderItems

public void setHeaderItems(Object[] headers)
Setter for the headers. This list will be marshalled and output before any calls to write(Object).

Parameters:
headers -

setSaveState

public void setSaveState(boolean saveState)

afterPropertiesSet

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

open

public void open(ExecutionContext executionContext)
Open the output source

Specified by:
open in interface ItemStream
See Also:
ItemStream.open(ExecutionContext)

endDocument

protected void endDocument(javax.xml.stream.XMLEventWriter writer)
                    throws javax.xml.stream.XMLStreamException
Writes the EndDocument tag manually.

Parameters:
writer - XML event writer
Throws:
javax.xml.stream.XMLStreamException

close

public void close(ExecutionContext executionContext)
Flush and close the output source.

Specified by:
close in interface ItemStream
Parameters:
executionContext - the current execution context in case it is needed
See Also:
ItemStream.close(ExecutionContext)

write

public void write(Object item)
Write the value object to internal buffer.

Specified by:
write in interface ItemWriter
Parameters:
item - the value object
See Also:
flush()

update

public void update(ExecutionContext executionContext)
Get the restart data.

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

flush

public void flush()
           throws FlushFailedException
Writes buffered items to XML stream and marks restore point.

Specified by:
flush in interface ItemWriter
Throws:
FlushFailedException - in case of an error. If this exception is thrown the writer may be in an inconsistent state and manual intervention might be required to reconcile the data with persistent output.

clear

public void clear()
           throws ClearFailedException
Clear the output buffer

Specified by:
clear in interface ItemWriter
Throws:
ClearFailedException - in case of an error. If this exception is thrown the writer may be in an inconsistent state and manual intervention might be required to reconcile the data with persistent output.


Copyright © 2009 SpringSource. All Rights Reserved.