diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java index 37499a37d..f41cf93d3 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java @@ -232,7 +232,15 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implemen /** * Set the tag name of the root element. If not set, default name is used - * ("root"). + * ("root"). Namespace URI and prefix can also be set optionally using the + * notation: + * + *
+	 * {uri}prefix:root
+	 * 
+ * + * The prefix is optional (defaults to empty), but if it is specified then the + * uri must be provided. * * @param rootTagName the tag name to be used for the root element */ @@ -249,16 +257,6 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implemen return rootTagNamespacePrefix; } - /** - * Set the namespace prefix of the root element. If not set, the namespace - * prefix "xmlns" is used. - * - * @param rootTagNamespacePrefix the rootTagNamespacePrefix to set - */ - public void setRootTagNamespacePrefix(String rootTagNamespacePrefix) { - this.rootTagNamespacePrefix = rootTagNamespacePrefix; - } - /** * Get the namespace of the root element. * @@ -268,15 +266,6 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implemen return rootTagNamespace; } - /** - * Set the namespace of the root element. If not set, no namespace is used. - * - * @param rootTagNamespace the rootTagNamespace to set - */ - public void setRootTagNamespace(String rootTagNamespace) { - this.rootTagNamespace = rootTagNamespace; - } - /** * Get attributes of the root element. * @@ -315,6 +304,15 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implemen */ public void afterPropertiesSet() throws Exception { Assert.notNull(marshaller); + if (rootTagName.contains("{")) { + rootTagNamespace = rootTagName.replaceAll("\\{(.*)\\}.*", "$1"); + rootTagName = rootTagName.replaceAll("\\{.*\\}(.*)", "$1"); + if (rootTagName.contains(":")) + { + rootTagNamespacePrefix = rootTagName.replaceAll("(.*):.*", "$1"); + rootTagName = rootTagName.replaceAll(".*:(.*)", "$1"); + } + } } /** diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java index d72d5cd08..df698d7b9 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java @@ -263,6 +263,40 @@ public class StaxEventItemWriterTests { } } + /** + * Item is written to the output file with namespace. + */ + @Test + public void testWriteRootTagWithNamespace() throws Exception { + writer.setRootTagName("{http://www.springframework.org/test}root"); + writer.afterPropertiesSet(); + writer.open(executionContext); + writer.write(items); + writer.close(); + String content = getOutputFileContent(); + assertTrue("Wrong content: " + content, content + .contains((""))); + assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); + assertTrue("Wrong content: " + content, content.contains((""))); + } + + /** + * Item is written to the output file with namespace and prefix. + */ + @Test + public void testWriteRootTagWithNamespaceAndPrefix() throws Exception { + writer.setRootTagName("{http://www.springframework.org/test}ns:root"); + writer.afterPropertiesSet(); + writer.open(executionContext); + writer.write(items); + writer.close(); + String content = getOutputFileContent(); + assertTrue("Wrong content: " + content, content + .contains((""))); + assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); + assertTrue("Wrong content: " + content, content.contains((""))); + } + /** * Writes object's toString representation as XML comment. */ @@ -317,38 +351,5 @@ public class StaxEventItemWriterTests { return source; } - - /** - * Item is written to the output file with namespace. - */ - @Test - public void testWriteRootTagWithNamespace() throws Exception { - writer.setRootTagNamespace("http://www.springframework.org/test"); - writer.open(executionContext); - writer.write(items); - writer.close(); - String content = getOutputFileContent(); - assertTrue("Wrong content: " + content, content - .contains((""))); - assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); - assertTrue("Wrong content: " + content, content.contains((""))); - } - - /** - * Item is written to the output file with namespace and prefix. - */ - @Test - public void testWriteRootTagWithNamespaceAndPrefix() throws Exception { - writer.setRootTagNamespacePrefix("ns"); - writer.setRootTagNamespace("http://www.springframework.org/test"); - writer.open(executionContext); - writer.write(items); - writer.close(); - String content = getOutputFileContent(); - assertTrue("Wrong content: " + content, content - .contains((""))); - assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); - assertTrue("Wrong content: " + content, content.contains((""))); - } }