org.springframework.batch.item.xml
Class StaxEventItemWriter<T>

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

public class StaxEventItemWriter<T>
extends ExecutionContextUserSupport
implements ResourceAwareItemWriterItemStream<T>, InitializingBean

An implementation of ItemWriter which uses StAX and Marshaller for serializing object to XML. This item writer also provides restart, statistics and transaction features by implementing corresponding interfaces. The implementation is *not* thread-safe.

Author:
Peter Zozom, Robert Kasanicky

Constructor Summary
StaxEventItemWriter()
           
 
Method Summary
 void afterPropertiesSet()
           
 void close()
          Flush and close the output source.
protected  void endDocument(javax.xml.stream.XMLEventWriter writer)
          Writes the EndDocument tag manually.
 String getEncoding()
          Get used encoding.
 Map<String,String> 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 setFooterCallback(StaxWriterCallback footerCallback)
          footerCallback is called after writing all items but before closing the file
 void setHeaderCallback(StaxWriterCallback headerCallback)
          headerCallback is called before writing any items.
 void setMarshaller(Marshaller marshaller)
          Set Object to XML marshaller.
 void setOverwriteOutput(boolean overwriteOutput)
          Set "overwrite" flag for the output file.
 void setResource(Resource resource)
          Set output file.
 void setRootElementAttributes(Map<String,String> 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 setVersion(String version)
          Set XML version to be used for output XML.
protected  void startDocument(javax.xml.stream.XMLEventWriter writer)
          Writes simple XML header containing: xml declaration - defines encoding and XML version opening tag of the root element and its attributes If this is not sufficient for you, simply override this method.
 void update(ExecutionContext executionContext)
          Get the restart data.
 void write(List<? extends T> items)
          Write the value objects and flush them to the file.
 
Methods inherited from class org.springframework.batch.item.util.ExecutionContextUserSupport
getKey, getName, 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.

Specified by:
setResource in interface ResourceAwareItemWriterItemStream<T>
Parameters:
resource - the output file

setMarshaller

public void setMarshaller(Marshaller marshaller)
Set Object to XML marshaller.

Parameters:
marshaller - the Object to XML marshaller

setHeaderCallback

public void setHeaderCallback(StaxWriterCallback headerCallback)
headerCallback is called before writing any items.


setFooterCallback

public void setFooterCallback(StaxWriterCallback footerCallback)
footerCallback is called after writing all items but before closing the file


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<String,String> getRootElementAttributes()
Get attributes of the root element.

Returns:
attributes of the root element

setRootElementAttributes

public void setRootElementAttributes(Map<String,String> 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 -

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)

startDocument

protected void startDocument(javax.xml.stream.XMLEventWriter writer)
                      throws javax.xml.stream.XMLStreamException
Writes simple XML header containing: If this is not sufficient for you, simply override this method. Encoding, version and root tag name can be retrieved with corresponding getters.

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

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()
Flush and close the output source.

Specified by:
close in interface ItemStream
See Also:
ItemStream.close()

write

public void write(List<? extends T> items)
           throws XmlMappingException,
                  IOException
Write the value objects and flush them to the file.

Specified by:
write in interface ItemWriter<T>
Parameters:
items - the value object
Throws:
IOException
XmlMappingException

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)


Copyright © 2009 SpringSource. All Rights Reserved.