@Before public void setUp() throws Exception { File directory = new File("target/data"); directory.mkdirs(); outputFile = File.createTempFile(ClassUtils.getShortName(this.getClass()), ".xml", directory); resource = new FileSystemResource(outputFile); writer.setResource(resource); writer.setMarshaller(getMarshaller()); writer.setRootTagName("{urn:org.springframework.batch.io.oxm.domain}trades"); writer.afterPropertiesSet(); writer.open(new ExecutionContext()); }
/** * 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 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(("<root xmlns=\"http://www.springframework.org/test\">"))); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); assertTrue("Wrong content: " + content, content.contains(("</root>"))); }
/** * 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"))); }
private void initWriterForSimpleCallbackTests() throws Exception { writer = createItemWriter(); writer.setHeaderCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createStartElement("ns", "http://www.springframework.org/test", "group")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); writer.setFooterCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createEndElement("ns", "http://www.springframework.org/test", "group")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); writer.setRootTagName("{http://www.springframework.org/test}ns:testroot"); writer.afterPropertiesSet(); }
writer.setRootTagName("testroot"); writer.setRootElementAttributes(Collections.<String, String> singletonMap("attribute", "value")); writer.open(executionContext);
/** * 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(); marshaller.setNamespace(writer.getRootTagNamespace()); marshaller.setNamespacePrefix(writer.getRootTagNamespacePrefix()); 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\">"))); assertTrue("Wrong content: " + content, content.contains(NS_TEST_STRING)); assertTrue("Wrong content: " + content, content.contains(("</ns:root>"))); assertTrue("Wrong content: " + content, content.contains(("<ns:root"))); }
writer.setRootTagName("{http://www.springframework.org/test}ns:testroot"); writer.afterPropertiesSet();
/** * 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); }
/** * @return new instance of fully configured writer */ private StaxEventItemWriter<Object> createItemWriter() throws Exception { StaxEventItemWriter<Object> source = new StaxEventItemWriter<>(); source.setResource(resource); marshaller = new SimpleMarshaller(); source.setMarshaller(marshaller); source.setEncoding("UTF-8"); source.setRootTagName("root"); source.setVersion("1.0"); source.setOverwriteOutput(true); source.setSaveState(true); source.afterPropertiesSet(); return source; }
/** * @return new instance of fully configured writer */ private StaxEventItemWriter<Object> createItemWriter() throws Exception { StaxEventItemWriter<Object> source = new StaxEventItemWriter<>(); source.setResource(resource); Marshaller marshaller = new SimpleMarshaller(); source.setMarshaller(marshaller); source.setEncoding("UTF-8"); source.setRootTagName("root"); source.setVersion("1.0"); source.setOverwriteOutput(true); source.setSaveState(true); source.afterPropertiesSet(); return source; } }
/** * Namespace prefixes are properly initialized on restart. */ @Test public void testRootTagWithNamespaceRestart() throws Exception { writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{http://www.springframework.org/test}root"); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); writer = createItemWriter(); writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{http://www.springframework.org/test}root"); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); String content = getOutputFileContent(); assertEquals("Wrong content: " + content, "<root xmlns=\"http://www.springframework.org/test\"><item/><item/></root>", content); }
/** * Namespace prefixes are properly initialized on restart. */ @Test public void testRootTagWithNamespaceAndPrefixRestart() throws Exception { writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{http://www.springframework.org/test}ns:root"); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); writer = createItemWriter(); writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{http://www.springframework.org/test}ns:root"); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); String content = getOutputFileContent(); assertEquals("Wrong content: " + content, "<ns:root xmlns:ns=\"http://www.springframework.org/test\"><ns:item/><ns:item/></ns:root>", content); }
@Bean public StaxEventItemWriter<Customer> customerItemWriter() throws Exception { XStreamMarshaller marshaller = new XStreamMarshaller(); Map<String, Class> aliases = new HashMap<>(); aliases.put("customer", Customer.class); marshaller.setAliases(aliases); StaxEventItemWriter<Customer> itemWriter = new StaxEventItemWriter<>(); itemWriter.setRootTagName("customers"); itemWriter.setMarshaller(marshaller); String customerOutputPath = File.createTempFile("customerOutput", ".xml").getAbsolutePath(); System.out.println(">> Output Path: " + customerOutputPath); itemWriter.setResource(new FileSystemResource(customerOutputPath)); itemWriter.afterPropertiesSet(); return itemWriter; }
@Bean public StaxEventItemWriter<Customer> xmlItemWriter() throws Exception { XStreamMarshaller marshaller = new XStreamMarshaller(); Map<String, Class> aliases = new HashMap<>(); aliases.put("customer", Customer.class); marshaller.setAliases(aliases); StaxEventItemWriter<Customer> itemWriter = new StaxEventItemWriter<>(); itemWriter.setRootTagName("customers"); itemWriter.setMarshaller(marshaller); String customerOutputPath = File.createTempFile("customerOutput", ".xml").getAbsolutePath(); System.out.println(">> Output Path: " + customerOutputPath); itemWriter.setResource(new FileSystemResource(customerOutputPath)); itemWriter.afterPropertiesSet(); return itemWriter; }
@Bean ItemWriter<StudentDTO> databaseXmlItemWriter(Environment environment) { StaxEventItemWriter<StudentDTO> xmlFileWriter = new StaxEventItemWriter<>(); String exportFilePath = environment.getRequiredProperty(PROPERTY_XML_EXPORT_FILE_PATH); xmlFileWriter.setResource(new FileSystemResource(exportFilePath)); xmlFileWriter.setRootTagName("students"); Jaxb2Marshaller studentMarshaller = new Jaxb2Marshaller(); studentMarshaller.setClassesToBeBound(StudentDTO.class); xmlFileWriter.setMarshaller(studentMarshaller); return xmlFileWriter; }
@Bean ItemWriter<StudentDTO> databaseXmlItemWriter(Environment environment) { StaxEventItemWriter<StudentDTO> xmlFileWriter = new StaxEventItemWriter<>(); String exportFilePath = environment.getRequiredProperty(PROPERTY_XML_EXPORT_FILE_PATH); xmlFileWriter.setResource(new FileSystemResource(exportFilePath)); xmlFileWriter.setRootTagName("students"); Jaxb2Marshaller studentMarshaller = new Jaxb2Marshaller(); studentMarshaller.setClassesToBeBound(StudentDTO.class); xmlFileWriter.setMarshaller(studentMarshaller); return xmlFileWriter; }
/** * 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; }