Example usage for javax.xml.stream XMLEventFactory createCharacters

List of usage examples for javax.xml.stream XMLEventFactory createCharacters

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventFactory createCharacters.

Prototype

public abstract Characters createCharacters(String content);

Source Link

Document

Create a Characters event, this method does not check if the content is all whitespace.

Usage

From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java

/**
 * Flush and close the output source.//from  w  ww  .j av a  2 s .  c om
 * 
 * @see org.springframework.batch.item.ItemStream#close()
 */
@Override
public void close() {
    super.close();

    XMLEventFactory factory = createXmlEventFactory();
    try {
        delegateEventWriter.add(factory.createCharacters(""));
    } catch (XMLStreamException e) {
        log.error(e);
    }

    try {
        if (footerCallback != null) {
            XMLEventWriter footerCallbackWriter = delegateEventWriter;
            if (restarted && !unclosedHeaderCallbackElements.isEmpty()) {
                footerCallbackWriter = new UnopenedElementClosingEventWriter(delegateEventWriter,
                        bufferedWriter, unclosedHeaderCallbackElements);
            }
            footerCallback.write(footerCallbackWriter);
        }
        delegateEventWriter.flush();
        endDocument(delegateEventWriter);
    } catch (IOException e) {
        throw new ItemStreamException("Failed to write footer items", e);
    } catch (XMLStreamException e) {
        throw new ItemStreamException("Failed to write end document tag", e);
    } finally {

        try {
            delegateEventWriter.close();
        } catch (XMLStreamException e) {
            log.error("Unable to close file resource: [" + resource + "] " + e);
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                log.error("Unable to close file resource: [" + resource + "] " + e);
            } finally {
                if (!transactional) {
                    closeStream();
                }
            }
        }
        if (currentRecordCount == 0 && shouldDeleteIfEmpty) {
            try {
                resource.getFile().delete();
            } catch (IOException e) {
                throw new ItemStreamException("Failed to delete empty file on close", e);
            }
        }
    }

    this.initialized = false;
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

private void initWriterForComplexCallbackTests() throws Exception {
    writer = createItemWriter();//from   w w  w. j ava  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("", "", "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();
}