Class StaxEventItemWriter<T>
java.lang.Object
org.springframework.batch.item.ItemStreamSupport
org.springframework.batch.item.support.AbstractItemStreamItemWriter<T>
org.springframework.batch.item.xml.StaxEventItemWriter<T>
- All Implemented Interfaces:
ResourceAwareItemWriterItemStream<T>,ItemStream,ItemStreamWriter<T>,ItemWriter<T>,org.springframework.beans.factory.InitializingBean
public class StaxEventItemWriter<T>
extends AbstractItemStreamItemWriter<T>
implements ResourceAwareItemWriterItemStream<T>, org.springframework.beans.factory.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, Michael Minella, Parikshit Dutta, Mahmoud Ben Hassine
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidclose()Flush and close the output source.protected ResultSubclasses can override to customize the STAX result.protected XMLEventFactorySubclasses can override to customize the event factory.protected XMLEventWritercreateXmlEventWriter(XMLOutputFactory outputFactory, Writer writer) Subclasses can override to customize the writer.protected XMLOutputFactorySubclasses can override to customize the factory.protected voidendDocument(XMLEventWriter writer) Writes the EndDocument tag manually.Get used encoding.Get attributes of the root element.Get the tag name of the root element.Get the namespace of the root element.Get the namespace prefix of the root element.Get used standalone document declaration.Get XML version.protected voidinitNamespaceContext(XMLEventWriter writer) Inits the namespace context of the XMLEventWriter: rootTagNamespacePrefix for rootTagName any other xmlns namespace prefix declarations in the root element attributesvoidopen(ExecutionContext executionContext) Open the output sourcevoidsetEncoding(String encoding) Set encoding to be used for output file.voidsetFooterCallback(StaxWriterCallback footerCallback) footerCallback is called after writing all items but before closing the file.voidsetForceSync(boolean forceSync) Flag to indicate that changes should be force-synced to disk on flush.voidsetHeaderCallback(StaxWriterCallback headerCallback) headerCallback is called before writing any items.voidsetMarshaller(org.springframework.oxm.Marshaller marshaller) Set Object to XML marshaller.voidsetOverwriteOutput(boolean overwriteOutput) Set "overwrite" flag for the output file.voidsetResource(org.springframework.core.io.WritableResource resource) Set output file.voidsetRootElementAttributes(Map<String, String> rootElementAttributes) Set the root element attributes to be written.voidsetRootTagName(String rootTagName) Set the tag name of the root element.voidsetSaveState(boolean saveState) voidsetShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty) Flag to indicate that the target file should be deleted if no items have been written (other than header and footer) on close.voidsetStandalone(Boolean standalone) Set standalone document declaration to be used for output XML.voidsetTransactional(boolean transactional) Flag to indicate that writes should be deferred to the end of a transaction if present.voidsetVersion(String version) Set XML version to be used for output XML.protected voidstartDocument(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.voidupdate(ExecutionContext executionContext) Get the restart data.voidWrite the value objects and flush them to the file.Methods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, setExecutionContextName, setName
-
Field Details
-
DEFAULT_ENCODING
- See Also:
-
DEFAULT_XML_VERSION
- See Also:
-
DEFAULT_STANDALONE_DOCUMENT
-
DEFAULT_ROOT_TAG_NAME
- See Also:
-
-
Constructor Details
-
StaxEventItemWriter
public StaxEventItemWriter()
-
-
Method Details
-
setResource
public void setResource(org.springframework.core.io.WritableResource resource) Set output file.- Specified by:
setResourcein interfaceResourceAwareItemWriterItemStream<T>- Parameters:
resource- the output file
-
setMarshaller
public void setMarshaller(org.springframework.oxm.Marshaller marshaller) Set Object to XML marshaller.- Parameters:
marshaller- the Object to XML marshaller
-
setHeaderCallback
headerCallback is called before writing any items.- Parameters:
headerCallback- theStaxWriterCallbackto be called prior to writing items.
-
setTransactional
public void setTransactional(boolean transactional) Flag to indicate that writes should be deferred to the end of a transaction if present. Defaults to true.- Parameters:
transactional- the flag to set
-
setForceSync
public void setForceSync(boolean forceSync) Flag to indicate that changes should be force-synced to disk on flush. Defaults to false, which means that even with a local disk changes could be lost if the OS crashes in between a write and a cache flush. Setting to true may result in slower performance for usage patterns involving many frequent writes.- Parameters:
forceSync- the flag value to set
-
setShouldDeleteIfEmpty
public void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty) Flag to indicate that the target file should be deleted if no items have been written (other than header and footer) on close. Defaults to false.- Parameters:
shouldDeleteIfEmpty- the flag value to set
-
getEncoding
Get used encoding.- Returns:
- the encoding used
-
setEncoding
Set encoding to be used for output file.- Parameters:
encoding- the encoding to be used
-
getVersion
Get XML version.- Returns:
- the XML version used
-
setVersion
Set XML version to be used for output XML.- Parameters:
version- the XML version to be used
-
getStandalone
Get used standalone document declaration.- Returns:
- the standalone document declaration used
- Since:
- 4.3
-
setStandalone
Set standalone document declaration to be used for output XML. If not set, standalone document declaration will be omitted.- Parameters:
standalone- the XML standalone document declaration to be used- Since:
- 4.3
-
getRootTagName
Get the tag name of the root element.- Returns:
- the root element tag name
-
setRootTagName
Set the tag name of the root element. If not set, default name is used ("root"). Namespace URI and prefix can also be set optionally using the notation:{uri}prefix:rootThe prefix is optional (defaults to empty), but if it is specified then the uri must be provided. In addition you might want to declare other namespaces using theroot attributes.- Parameters:
rootTagName- the tag name to be used for the root element
-
getRootTagNamespacePrefix
Get the namespace prefix of the root element. Empty by default.- Returns:
- the rootTagNamespacePrefix
-
getRootTagNamespace
Get the namespace of the root element.- Returns:
- the rootTagNamespace
-
getRootElementAttributes
Get attributes of the root element.- Returns:
- attributes of the root element
-
setRootElementAttributes
Set the root element attributes to be written. If any of the key names begin with "xmlns:" then they are treated as namespace declarations.- 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- If set to true, output file will be overwritten (this flag is ignored when processing is restart).
-
setSaveState
public void setSaveState(boolean saveState) -
afterPropertiesSet
- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception- thrown if error occurs- See Also:
-
InitializingBean.afterPropertiesSet()
-
open
Open the output source- Specified by:
openin interfaceItemStream- Overrides:
openin classItemStreamSupport- Parameters:
executionContext- the batch context.- See Also:
-
createXmlEventWriter
protected XMLEventWriter createXmlEventWriter(XMLOutputFactory outputFactory, Writer writer) throws XMLStreamException Subclasses can override to customize the writer.- Parameters:
outputFactory- the factory to be used to create anXMLEventWriter.writer- theWriterto be used by theXMLEventWriterfor writing to character streams.- Returns:
- an xml writer
- Throws:
XMLStreamException- thrown if error occured creatingXMLEventWriter.
-
createXmlOutputFactory
Subclasses can override to customize the factory.- Returns:
- a factory for the xml output
- Throws:
FactoryConfigurationError- throw if an instance of this factory cannot be loaded.
-
createXmlEventFactory
Subclasses can override to customize the event factory.- Returns:
- a factory for the xml events
- Throws:
FactoryConfigurationError- thrown if an instance of this factory cannot be loaded.
-
createStaxResult
Subclasses can override to customize the STAX result.- Returns:
- a result for writing to
-
initNamespaceContext
Inits the namespace context of the XMLEventWriter:- rootTagNamespacePrefix for rootTagName
- any other xmlns namespace prefix declarations in the root element attributes
- Parameters:
writer- XML event writer- Throws:
XMLStreamException- thrown if error occurs while setting the prefix or default name space.
-
startDocument
Writes simple XML header containing:- xml declaration - defines encoding and XML version
- opening tag of the root element and its attributes
- Parameters:
writer- XML event writer- Throws:
XMLStreamException- thrown if error occurs.
-
endDocument
Writes the EndDocument tag manually.- Parameters:
writer- XML event writer- Throws:
XMLStreamException- thrown if error occurs.
-
close
public void close()Flush and close the output source.- Specified by:
closein interfaceItemStream- Overrides:
closein classItemStreamSupport- See Also:
-
write
public void write(List<? extends T> items) throws org.springframework.oxm.XmlMappingException, IOException Write the value objects and flush them to the file.- Specified by:
writein interfaceItemWriter<T>- Parameters:
items- the value object- Throws:
IOException- thrown if general error occurs.org.springframework.oxm.XmlMappingException- thrown if error occurs during XML Mapping.
-
update
Get the restart data.- Specified by:
updatein interfaceItemStream- Overrides:
updatein classItemStreamSupport- Parameters:
executionContext- the batch context.- See Also:
-