List of usage examples for javax.xml.stream XMLEventFactory newInstance
public static XMLEventFactory newInstance() throws FactoryConfigurationError
From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java
/** * Item is written to the output file only after flush. */// w ww. j a v a2 s . c o m @Test public void testWriteWithHeader() throws Exception { writer.setHeaderCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createStartElement("", "", "header")); writer.add(factory.createEndElement("", "", "header")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); writer.open(executionContext); writer.write(items); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(("<header/>"))); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java
/** * Open method writes the root tag, close method adds corresponding end tag. *//* www . j a v a 2s .c om*/ @Test public void testOpenAndClose() throws Exception { writer.setHeaderCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createStartElement("", "", "header")); writer.add(factory.createEndElement("", "", "header")); } 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.createStartElement("", "", "footer")); writer.add(factory.createEndElement("", "", "footer")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); writer.setRootTagName("testroot"); writer.setRootElementAttributes(Collections.<String, String>singletonMap("attribute", "value")); writer.open(executionContext); writer.close(); String content = getOutputFileContent(); assertTrue(content.contains("<testroot attribute=\"value\">")); assertTrue(content.contains("<header/>")); assertTrue(content.contains("<footer/>")); assertTrue(content.endsWith("</testroot>")); }
From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java
/** * Resource is deleted when items have not been written and shouldDeleteIfEmpty flag is set. *//*from w w w. j a v a 2 s .c om*/ @Test public void testDeleteIfEmptyNoRecordsWrittenHeaderAndFooter() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.setHeaderCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createStartElement("", "", "header")); writer.add(factory.createEndElement("", "", "header")); } 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.createStartElement("", "", "footer")); writer.add(factory.createEndElement("", "", "footer")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); writer.open(executionContext); writer.close(); assertFalse("file should be deleted" + resource, resource.getFile().exists()); }
From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java
/** * Resource is not deleted when items have been written and shouldDeleteIfEmpty flag is set (restart after delete). */// w w w .j a v a 2 s . c om @Test public void testDeleteIfEmptyNoRecordsWrittenHeaderAndFooterRestartAfterDelete() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.setHeaderCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createStartElement("", "", "header")); writer.add(factory.createEndElement("", "", "header")); } 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.createStartElement("", "", "footer")); writer.add(factory.createEndElement("", "", "footer")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); 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)); }
From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java
private void initWriterForSimpleCallbackTests() throws Exception { writer = createItemWriter();//w ww . j a v a 2s . co m 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(); }
From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java
private void initWriterForComplexCallbackTests() throws Exception { writer = createItemWriter();/*w w w. j av a 2 s . c om*/ writer.setHeaderCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createStartElement("", "", "preHeader")); writer.add(factory.createCharacters("PRE-HEADER")); writer.add(factory.createEndElement("", "", "preHeader")); writer.add(factory.createStartElement("ns", "http://www.springframework.org/test", "group")); writer.add(factory.createStartElement("", "", "subGroup")); writer.add(factory.createStartElement("", "", "postHeader")); writer.add(factory.createCharacters("POST-HEADER")); writer.add(factory.createEndElement("", "", "postHeader")); } 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.createStartElement("", "", "preFooter")); writer.add(factory.createCharacters("PRE-FOOTER")); writer.add(factory.createEndElement("", "", "preFooter")); writer.add(factory.createEndElement("", "", "subGroup")); writer.add(factory.createEndElement("ns", "http://www.springframework.org/test", "group")); writer.add(factory.createStartElement("", "", "postFooter")); writer.add(factory.createCharacters("POST-FOOTER")); writer.add(factory.createEndElement("", "", "postFooter")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); writer.setRootTagName("{http://www.springframework.org/test}ns:testroot"); writer.afterPropertiesSet(); }
From source file:org.springframework.batch.item.xml.TransactionalStaxEventItemWriterTests.java
/** * Item is written to the output file only after flush. *///from ww w.j a v a 2 s . c o m @Test public void testWriteWithHeaderAfterRollback() throws Exception { writer.setHeaderCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createStartElement("", "", "header")); writer.add(factory.createEndElement("", "", "header")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); writer.open(executionContext); try { new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { try { writer.write(items); } catch (Exception e) { throw new RuntimeException(e); } throw new RuntimeException("Planned"); } }); fail("Expected RuntimeException"); } catch (RuntimeException e) { // expected } writer.close(); writer.open(executionContext); new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { try { writer.write(items); } catch (Exception e) { throw new RuntimeException(e); } return null; } }); writer.close(); String content = outputFileContent(); assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, ("<header/>"))); assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, TEST_STRING)); }
From source file:org.springframework.batch.item.xml.TransactionalStaxEventItemWriterTests.java
/** * Item is written to the output file only after flush. */// w w w . j a v a2 s . c om @Test public void testWriteWithHeaderAfterFlushAndRollback() throws Exception { writer.setHeaderCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createStartElement("", "", "header")); writer.add(factory.createEndElement("", "", "header")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); writer.open(executionContext); new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { try { writer.write(items); } catch (Exception e) { throw new RuntimeException(e); } return null; } }); writer.update(executionContext); writer.close(); writer.open(executionContext); try { new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { try { writer.write(items); } catch (Exception e) { throw new RuntimeException(e); } throw new RuntimeException("Planned"); } }); fail("Expected RuntimeException"); } catch (RuntimeException e) { // expected } writer.close(); String content = outputFileContent(); assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, ("<header/>"))); assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, TEST_STRING)); }