Example usage for org.springframework.batch.item.xml.stax UnopenedElementClosingEventWriter UnopenedElementClosingEventWriter

List of usage examples for org.springframework.batch.item.xml.stax UnopenedElementClosingEventWriter UnopenedElementClosingEventWriter

Introduction

In this page you can find the example usage for org.springframework.batch.item.xml.stax UnopenedElementClosingEventWriter UnopenedElementClosingEventWriter.

Prototype

public UnopenedElementClosingEventWriter(XMLEventWriter wrappedEventWriter, Writer ioWriter,
            List<QName> unopenedElements) 

Source Link

Usage

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

/**
 * Flush and close the output source.//from w  w w. ja v a2s .com
 * 
 * @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;
}