List of usage examples for javax.xml.stream XMLEventFactory createStartElement
public abstract StartElement createStartElement(String prefix, String namespaceUri, String localName);
From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java
/** * Writes simple XML header containing:/*from w ww . jav a2 s. co m*/ * <ul> * <li>xml declaration - defines encoding and XML version</li> * <li>opening tag of the root element and its attributes</li> * </ul> * If this is not sufficient for you, simply override this method. Encoding, * version and root tag name can be retrieved with corresponding getters. * * @param writer XML event writer * @throws XMLStreamException */ protected void startDocument(XMLEventWriter writer) throws XMLStreamException { XMLEventFactory factory = createXmlEventFactory(); // write start document writer.add(factory.createStartDocument(getEncoding(), getVersion())); // write root tag writer.add( factory.createStartElement(getRootTagNamespacePrefix(), getRootTagNamespace(), getRootTagName())); if (StringUtils.hasText(getRootTagNamespace())) { if (StringUtils.hasText(getRootTagNamespacePrefix())) { writer.add(factory.createNamespace(getRootTagNamespacePrefix(), getRootTagNamespace())); } else { writer.add(factory.createNamespace(getRootTagNamespace())); } } // write root tag attributes if (!CollectionUtils.isEmpty(getRootElementAttributes())) { for (Map.Entry<String, String> entry : getRootElementAttributes().entrySet()) { String key = entry.getKey(); if (key.startsWith("xmlns")) { String prefix = ""; if (key.contains(":")) { prefix = key.substring(key.indexOf(":") + 1); } writer.add(factory.createNamespace(prefix, entry.getValue())); } else { writer.add(factory.createAttribute(key, entry.getValue())); } } } /* * This forces the flush to write the end of the root element and avoids * an off-by-one error on restart. */ writer.add(factory.createIgnorableSpace("")); writer.flush(); }
From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java
/** * Item is written to the output file only after flush. *///from w w w . ja 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. *//*from w w w.ja v a 2 s . 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. *//*ww w . j a v a 2 s. c o m*/ @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). *//*from w w w .jav 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();//ww w .j a v a 2 s. 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();/*ww w. j a va 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 av a2 s. c om*/ @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. *//*from w w w . j a v a 2 s .co m*/ @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)); }