/** * 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; }
/** * @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; }
/** * 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); }
/** * Writes the EndDocument tag manually. * * @param writer XML event writer * * @throws XMLStreamException thrown if error occurs. */ protected void endDocument(XMLEventWriter writer) throws XMLStreamException { // writer.writeEndDocument(); <- this doesn't work after restart // we need to write end tag of the root element manually String nsPrefix = !StringUtils.hasText(getRootTagNamespacePrefix()) ? "" : getRootTagNamespacePrefix() + ":"; try { bufferedWriter.write("</" + nsPrefix + getRootTagName() + ">"); } catch (IOException ioe) { throw new DataAccessResourceFailureException("Unable to close file resource: [" + resource + "]", ioe); } }
@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; }
@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()); }
/** * Test setting writer name. */ @Test public void testSetName() throws Exception { writer.setName("test"); writer.open(executionContext); writer.write(items); writer.update(executionContext); writer.close(); assertTrue("execution context keys should be prefixed with writer name", executionContext.containsKey("test.position")); }
/** * 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"))); }
/** * 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>"))); }
writer.setHeaderCallback(new StaxWriterCallback() { writer.setFooterCallback(new StaxWriterCallback() { writer.setRootTagName("testroot"); writer.setRootElementAttributes(Collections.<String, String> singletonMap("attribute", "value")); writer.open(executionContext); writer.close(); String content = getOutputFileContent();
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(); }
/** * 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); }
writer.setShouldDeleteIfEmpty(true); writer.setHeaderCallback(new StaxWriterCallback() { writer.setFooterCallback(new StaxWriterCallback() { writer.open(executionContext); writer.update(executionContext); writer.close(); assertFalse("file should be deleted" + resource, resource.getFile().exists()); writer.open(executionContext); writer.write(items); writer.update(executionContext); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING));
XMLEventFactory factory = createXmlEventFactory(); writer.add(factory.createStartDocument(getEncoding(), getVersion())); writer.add(factory.createStartElement(getRootTagNamespacePrefix(), getRootTagNamespace(), getRootTagName())); if (StringUtils.hasText(getRootTagNamespace())) { if (StringUtils.hasText(getRootTagNamespacePrefix())) { writer.add(factory.createNamespace(getRootTagNamespacePrefix(), getRootTagNamespace())); writer.add(factory.createNamespace(getRootTagNamespace())); if (!CollectionUtils.isEmpty(getRootElementAttributes())) { for (Map.Entry<String, String> entry : getRootElementAttributes().entrySet()) { String key = entry.getKey(); if (key.startsWith("xmlns")) {
writer.setShouldDeleteIfEmpty(true); writer.setHeaderCallback(new StaxWriterCallback() { writer.setFooterCallback(new StaxWriterCallback() { writer.open(executionContext); writer.close(); assertFalse("file should be deleted" + resource, resource.getFile().exists());
@Before public void setUp() throws Exception { super.createFile(); delegate = new StaxEventItemWriter<>(); delegate.setMarshaller(new SimpleMarshaller()); }
@Test public void testNonExistantResource() throws Exception { Resource doesntExist = mock(Resource.class); when(doesntExist.getFile()).thenReturn(File.createTempFile("arbitrary", null)); when(doesntExist.exists()).thenReturn(false); writer.setResource(doesntExist); try { writer.open(executionContext); fail(); } catch (IllegalStateException e) { assertEquals("Output resource must exist", e.getMessage()); } }
@Test public void testWriteAndForceFlush() throws Exception { writer.setForceSync(true); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
@Test(expected = WriterNotOpenException.class) public void testAssertWriterIsInitialized() throws Exception { StaxEventItemWriter<String> writer = new StaxEventItemWriter<>(); writer.write(Collections.singletonList("foo")); }
/** * Resource is deleted when no items have been written and shouldDeleteIfEmpty flag is set. */ @Test public void testDeleteIfEmptyNoRecordsWritten() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.close(); assertFalse("file should be deleted" + resource, resource.getFile().exists()); }