public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T> implements ResourceAwareItemWriterItemStream<T>, org.springframework.beans.factory.InitializingBean
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.Constructor and Description |
---|
StaxEventItemWriter() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
void |
close()
Flush and close the output source.
|
protected javax.xml.transform.Result |
createStaxResult()
Subclasses can override to customize the STAX result.
|
protected javax.xml.stream.XMLEventFactory |
createXmlEventFactory()
Subclasses can override to customize the event factory.
|
protected javax.xml.stream.XMLEventWriter |
createXmlEventWriter(javax.xml.stream.XMLOutputFactory outputFactory,
java.io.Writer writer)
Subclasses can override to customize the writer.
|
protected javax.xml.stream.XMLOutputFactory |
createXmlOutputFactory()
Subclasses can override to customize the factory.
|
protected void |
endDocument(javax.xml.stream.XMLEventWriter writer)
Writes the EndDocument tag manually.
|
java.lang.String |
getEncoding()
Get used encoding.
|
java.util.Map<java.lang.String,java.lang.String> |
getRootElementAttributes()
Get attributes of the root element.
|
java.lang.String |
getRootTagName()
Get the tag name of the root element.
|
java.lang.String |
getRootTagNamespace()
Get the namespace of the root element.
|
java.lang.String |
getRootTagNamespacePrefix()
Get the namespace prefix of the root element.
|
java.lang.String |
getVersion()
Get XML version.
|
protected void |
initNamespaceContext(javax.xml.stream.XMLEventWriter writer)
Inits the namespace context of the XMLEventWriter:
rootTagNamespacePrefix for rootTagName
any other xmlns namespace prefix declarations in the root element attributes
|
void |
open(ExecutionContext executionContext)
Open the output source
|
void |
setEncoding(java.lang.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 |
setForceSync(boolean forceSync)
Flag to indicate that changes should be force-synced to disk on flush.
|
void |
setHeaderCallback(StaxWriterCallback headerCallback)
headerCallback is called before writing any items.
|
void |
setMarshaller(org.springframework.oxm.Marshaller marshaller)
Set Object to XML marshaller.
|
void |
setOverwriteOutput(boolean overwriteOutput)
Set "overwrite" flag for the output file.
|
void |
setResource(org.springframework.core.io.Resource resource)
Set output file.
|
void |
setRootElementAttributes(java.util.Map<java.lang.String,java.lang.String> rootElementAttributes)
Set the root element attributes to be written.
|
void |
setRootTagName(java.lang.String rootTagName)
Set the tag name of the root element.
|
void |
setSaveState(boolean saveState) |
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.
|
void |
setTransactional(boolean transactional)
Flag to indicate that writes should be deferred to the end of a
transaction if present.
|
void |
setVersion(java.lang.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(java.util.List<? extends T> items)
Write the value objects and flush them to the file.
|
getExecutionContextKey, setExecutionContextName, setName
public void setResource(org.springframework.core.io.Resource resource)
setResource
in interface ResourceAwareItemWriterItemStream<T>
resource
- the output filepublic void setMarshaller(org.springframework.oxm.Marshaller marshaller)
marshaller
- the Object to XML marshallerpublic void setHeaderCallback(StaxWriterCallback headerCallback)
public void setFooterCallback(StaxWriterCallback footerCallback)
public void setTransactional(boolean transactional)
transactional
- the flag to setpublic void setForceSync(boolean forceSync)
forceSync
- the flag value to setpublic void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty)
shouldDeleteIfEmpty
- the flag value to setpublic java.lang.String getEncoding()
public void setEncoding(java.lang.String encoding)
encoding
- the encoding to be usedpublic java.lang.String getVersion()
public void setVersion(java.lang.String version)
version
- the XML version to be usedpublic java.lang.String getRootTagName()
public void setRootTagName(java.lang.String rootTagName)
{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 the
root
attributes
.rootTagName
- the tag name to be used for the root elementpublic java.lang.String getRootTagNamespacePrefix()
public java.lang.String getRootTagNamespace()
public java.util.Map<java.lang.String,java.lang.String> getRootElementAttributes()
public void setRootElementAttributes(java.util.Map<java.lang.String,java.lang.String> rootElementAttributes)
rootElementAttributes
- attributes of the root elementpublic void setOverwriteOutput(boolean overwriteOutput)
overwriteOutput
- public void setSaveState(boolean saveState)
public void afterPropertiesSet() throws java.lang.Exception
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
java.lang.Exception
InitializingBean.afterPropertiesSet()
public void open(ExecutionContext executionContext)
open
in interface ItemStream
open
in class ItemStreamSupport
executionContext
- current step's ExecutionContext
. Will be the
executionContext from the last run of the step on a restart.ItemStream.open(ExecutionContext)
protected javax.xml.stream.XMLEventWriter createXmlEventWriter(javax.xml.stream.XMLOutputFactory outputFactory, java.io.Writer writer) throws javax.xml.stream.XMLStreamException
outputFactory
- writer
- javax.xml.stream.XMLStreamException
protected javax.xml.stream.XMLOutputFactory createXmlOutputFactory() throws javax.xml.stream.FactoryConfigurationError
javax.xml.stream.FactoryConfigurationError
protected javax.xml.stream.XMLEventFactory createXmlEventFactory() throws javax.xml.stream.FactoryConfigurationError
javax.xml.stream.FactoryConfigurationError
protected javax.xml.transform.Result createStaxResult() throws java.lang.Exception
java.lang.Exception
protected void initNamespaceContext(javax.xml.stream.XMLEventWriter writer) throws javax.xml.stream.XMLStreamException
writer
- XML event writerjavax.xml.stream.XMLStreamException
protected void startDocument(javax.xml.stream.XMLEventWriter writer) throws javax.xml.stream.XMLStreamException
writer
- XML event writerjavax.xml.stream.XMLStreamException
protected void endDocument(javax.xml.stream.XMLEventWriter writer) throws javax.xml.stream.XMLStreamException
writer
- XML event writerjavax.xml.stream.XMLStreamException
public void close()
close
in interface ItemStream
close
in class ItemStreamSupport
ItemStream.close()
public void write(java.util.List<? extends T> items) throws org.springframework.oxm.XmlMappingException, java.lang.Exception
write
in interface ItemWriter<T>
items
- the value objectjava.io.IOException
org.springframework.oxm.XmlMappingException
java.lang.Exception
- if there are errors. The framework will catch the
exception and convert or rethrow it as appropriate.public void update(ExecutionContext executionContext)
update
in interface ItemStream
update
in class ItemStreamSupport
executionContext
- to be updatedItemStream.update(ExecutionContext)