/** * Returns a configured {@link StaxEventItemWriter} * * @return a StaxEventItemWriter */ public StaxEventItemWriter<T> build() { Assert.notNull(this.marshaller, "A marshaller is required"); if(this.saveState) { Assert.notNull(this.name, "A name is required"); } StaxEventItemWriter<T> writer = new StaxEventItemWriter<>(); writer.setEncoding(this.encoding); writer.setFooterCallback(this.footerCallback); writer.setForceSync(this.forceSync); writer.setHeaderCallback(this.headerCallback); writer.setMarshaller(this.marshaller); writer.setOverwriteOutput(this.overwriteOutput); writer.setResource(this.resource); writer.setRootElementAttributes(this.rootElementAttributes); writer.setRootTagName(this.rootTagName); writer.setSaveState(this.saveState); writer.setShouldDeleteIfEmpty(this.shouldDeleteIfEmpty); writer.setTransactional(this.transactional); writer.setVersion(this.version); writer.setName(this.name); return writer; }
/** * Item is written to the output file with additional namespaces and prefix. */ @Test public void testWriteRootTagWithAdditionalNamespace() throws Exception { writer.setRootTagName("{http://www.springframework.org/test}ns:root"); marshaller.setNamespace("urn:org.test.foo"); marshaller.setNamespacePrefix("foo"); writer.setRootElementAttributes(Collections.singletonMap("xmlns:foo", "urn:org.test.foo")); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content .contains(("<ns:root xmlns:ns=\"http://www.springframework.org/test\" " + "xmlns:foo=\"urn:org.test.foo\">"))); assertTrue("Wrong content: " + content, content.contains(FOO_TEST_STRING)); assertTrue("Wrong content: " + content, content.contains(("</ns:root>"))); assertTrue("Wrong content: " + content, content.contains(("<ns:root"))); }
writer.setRootElementAttributes(Collections.<String, String> singletonMap("attribute", "value")); writer.open(executionContext); writer.close();
/** * Namespace prefixes are properly initialized on restart. */ @Test public void testRootTagWithAdditionalNamespaceRestart() throws Exception { writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{urn:org.test.foo}foo:root"); writer.setRootElementAttributes(Collections.singletonMap("xmlns:ns", "http://www.springframework.org/test")); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); writer = createItemWriter(); writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{urn:org.test.foo}foo:root"); writer.setRootElementAttributes(Collections.singletonMap("xmlns:ns", "http://www.springframework.org/test")); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); String content = getOutputFileContent(); assertEquals("Wrong content: " + content, "<foo:root xmlns:foo=\"urn:org.test.foo\" xmlns:ns=\"http://www.springframework.org/test\"><ns:item/><ns:item/></foo:root>", content); }
/** * Returns a configured {@link StaxEventItemWriter} * * @return a StaxEventItemWriter */ public StaxEventItemWriter<T> build() { Assert.notNull(this.marshaller, "A marshaller is required"); if(this.saveState) { Assert.notNull(this.name, "A name is required"); } StaxEventItemWriter<T> writer = new StaxEventItemWriter<>(); writer.setEncoding(this.encoding); writer.setFooterCallback(this.footerCallback); writer.setForceSync(this.forceSync); writer.setHeaderCallback(this.headerCallback); writer.setMarshaller(this.marshaller); writer.setOverwriteOutput(this.overwriteOutput); writer.setResource(this.resource); writer.setRootElementAttributes(this.rootElementAttributes); writer.setRootTagName(this.rootTagName); writer.setSaveState(this.saveState); writer.setShouldDeleteIfEmpty(this.shouldDeleteIfEmpty); writer.setTransactional(this.transactional); writer.setVersion(this.version); writer.setName(this.name); return writer; }