Add support for 'standalone' attribute in StaxEventItemWriter

Issue #758
This commit is contained in:
Parikshit Dutta
2020-08-20 12:41:05 +05:30
committed by Mahmoud Ben Hassine
parent b309ddc0a9
commit d65203e2d3
5 changed files with 189 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2017 the original author or authors.
* Copyright 2006-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -72,7 +72,7 @@ import org.springframework.util.StringUtils;
* @author Peter Zozom
* @author Robert Kasanicky
* @author Michael Minella
*
* @author Parikshit Dutta
*/
public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T> implements
ResourceAwareItemWriterItemStream<T>, InitializingBean {
@@ -85,6 +85,9 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
// default encoding
public static final String DEFAULT_XML_VERSION = "1.0";
// default standalone document declaration, value not set
public static final Boolean DEFAULT_STANDALONE_DOCUMENT = null;
// default root tag name
public static final String DEFAULT_ROOT_TAG_NAME = "root";
@@ -109,6 +112,9 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
// XML version
private String version = DEFAULT_XML_VERSION;
// standalone header attribute
private Boolean standalone = DEFAULT_STANDALONE_DOCUMENT;
// name of the root tag
private String rootTagName = DEFAULT_ROOT_TAG_NAME;
@@ -270,6 +276,29 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
this.version = version;
}
/**
* Get used standalone document declaration.
*
* @return the standalone document declaration used
*
* @since 4.3
*/
public Boolean getStandalone() {
return standalone;
}
/**
* Set standalone document declaration to be used for output XML. If not set,
* standalone document declaration will be omitted.
*
* @param standalone the XML standalone document declaration to be used
*
* @since 4.3
*/
public void setStandalone(Boolean standalone) {
this.standalone = standalone;
}
/**
* Get the tag name of the root element.
*
@@ -606,7 +635,12 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
XMLEventFactory factory = createXmlEventFactory();
// write start document
writer.add(factory.createStartDocument(getEncoding(), getVersion()));
if (getStandalone()==null) {
writer.add(factory.createStartDocument(getEncoding(), getVersion()));
}
else {
writer.add(factory.createStartDocument(getEncoding(), getVersion(), getStandalone()));
}
// write root tag
writer.add(factory.createStartElement(getRootTagNamespacePrefix(), getRootTagNamespace(), getRootTagName()));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ import org.springframework.util.Assert;
* A builder for the {@link StaxEventItemWriter}.
*
* @author Michael Minella
* @author Parikshit Dutta
* @since 4.0
* @see StaxEventItemWriter
*/
@@ -50,6 +51,8 @@ public class StaxEventItemWriterBuilder<T> {
private String version = StaxEventItemWriter.DEFAULT_XML_VERSION;
private Boolean standalone = StaxEventItemWriter.DEFAULT_STANDALONE_DOCUMENT;
private String rootTagName = StaxEventItemWriter.DEFAULT_ROOT_TAG_NAME;
private Map<String, String> rootElementAttributes;
@@ -188,7 +191,7 @@ public class StaxEventItemWriterBuilder<T> {
*
* @param version XML version
* @return the current instance of the builder
* @see StaxEventItemWriter#version
* @see StaxEventItemWriter#setVersion(String)
*/
public StaxEventItemWriterBuilder<T> version(String version) {
this.version = version;
@@ -196,6 +199,21 @@ public class StaxEventItemWriterBuilder<T> {
return this;
}
/**
* Standalone document declaration for the output document. Defaults to null.
*
* @param standalone Boolean standalone document declaration
* @return the current instance of the builder
* @see StaxEventItemWriter#setStandalone(Boolean)
*
* @since 4.3
*/
public StaxEventItemWriterBuilder<T> standalone(Boolean standalone) {
this.standalone = standalone;
return this;
}
/**
* The name of the root tag for the output document.
*
@@ -278,6 +296,7 @@ public class StaxEventItemWriterBuilder<T> {
writer.setTransactional(this.transactional);
writer.setVersion(this.version);
writer.setName(this.name);
writer.setStandalone(this.standalone);
return writer;
}