@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.afterPropertiesSet(); writer.open(new ExecutionContext()); }
@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()); }
@Before public void setUp() throws Exception { super.createFile(); delegate = new StaxEventItemWriter<>(); delegate.setMarshaller(new SimpleMarshaller()); }
/** * 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; }
/** * Test with OXM Marshaller that closes the XMLEventWriter. */ // BATCH-2054 @Test public void testMarshallingClosingEventWriter() throws Exception { writer.setMarshaller(new SimpleMarshaller() { @Override public void marshal(Object graph, Result result) throws XmlMappingException, IOException { super.marshal(graph, result); try { StaxTestUtils.getXmlEventWriter(result).close(); } catch (Exception e) { throw new RuntimeException("Exception while writing to output file", e); } } }); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(items); writer.write(items); }
/** * 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> 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 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 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; }