IN PROGRESS - BATCH-763: Create callback for header and footer writing in xml and flat files

added footers to flatfile and stax writers
This commit is contained in:
robokaso
2008-09-05 13:20:26 +00:00
parent 6c1c803de6
commit 16f432e08f
4 changed files with 96 additions and 23 deletions

View File

@@ -80,6 +80,8 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
private List<String> headerLines = new ArrayList<String>();
private List<String> footerLines = new ArrayList<String>();
private String lineSeparator = DEFAULT_LINE_SEPARATOR;
public FlatFileItemWriter() {
@@ -160,6 +162,16 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
this.headerLines = Arrays.asList(headerLines);
}
/**
* Public setter for footer lines. These will be output at the end of the
* file when the writer is closed.
*
* @param footerLines the footer lines to set
*/
public void setFooterLines(String[] footerLines) {
this.footerLines = Arrays.asList(footerLines);
}
/**
* Writes out a string followed by a "new line", where the format of the new
* line separator is determined by the underlying operating system. If the
@@ -190,7 +202,6 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
throw new FlushFailedException("Could not write data. The file may be corrupt.", e);
}
}
}
/**
@@ -198,8 +209,18 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
*/
public void close(ExecutionContext executionContext) {
if (state != null) {
getOutputState().close();
state = null;
try {
for (String line : footerLines) {
state.write(line + lineSeparator);
}
}
catch (IOException e) {
throw new ItemStreamException("Failed to writer footer before closing", e);
}
finally {
getOutputState().close();
state = null;
}
}
}
@@ -352,6 +373,7 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
* Close the open resource and reset counters.
*/
public void close() {
initialized = false;
restarted = false;
try {

View File

@@ -110,7 +110,9 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
private boolean saveState = true;
private List<?> headers = new ArrayList<Object>();
private List<?> headerItems = new ArrayList<Object>();
private List<?> footerItems = new ArrayList<Object>();
public StaxEventItemWriter() {
setName(ClassUtils.getShortName(StaxEventItemWriter.class));
@@ -218,14 +220,25 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
}
/**
* Setter for the headers. This list will be marshalled and output before
* any calls to {@link #write(List)}. Header item type is not restricted,
* but note the {@link #setMarshaller(Marshaller)} needs to support
* the type.
* @param headers
* Setter for the headerItems. This list will be marshalled and output
* before any calls to {@link #write(List)}. Header item type is not
* restricted, but note the {@link #setMarshaller(Marshaller)} needs to
* support the type.
* @param headerItems
*/
public void setHeaderItems(List<?> headers) {
this.headers = headers;
public void setHeaderItems(List<?> headerItems) {
this.headerItems = headerItems;
}
/**
* Setter for the footerItems. This list will be marshalled and output
* immediately before the writer is closed. Footer item type is not
* restricted, but note the {@link #setMarshaller(Marshaller)} needs to
* support the type.
* @param footerItems
*/
public void setFooterItems(List<?> footerItems) {
this.footerItems = footerItems;
}
public void setSaveState(boolean saveState) {
@@ -262,10 +275,10 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
if (startAtPosition == 0) {
try {
doWrite(headers);
doWrite(headerItems);
}
catch (IOException e) {
throw new ItemStreamException("Failed to write headers", e);
throw new ItemStreamException("Failed to write headerItems", e);
}
}
@@ -383,16 +396,33 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
}
try {
doWrite(footerItems);
delegateEventWriter.flush();
endDocument(delegateEventWriter);
eventWriter.close();
channel.close();
}
catch (XMLStreamException xse) {
throw new DataAccessResourceFailureException("Unable to close file resource: [" + resource + "]", xse);
catch (IOException e) {
throw new ItemStreamException("Failed to write footer items", e);
}
catch (IOException ioe) {
throw new DataAccessResourceFailureException("Unable to close file resource: [" + resource + "]", ioe);
catch (XMLStreamException e) {
throw new ItemStreamException("Failed to write end document tag", e);
}
finally {
try {
eventWriter.close();
}
catch (XMLStreamException xse) {
log.error("Unable to close file resource: [" + resource + "] " + xse);
}
finally {
try {
channel.close();
}
catch (IOException ioe) {
log.error("Unable to close file resource: [" + resource + "] " + ioe);
}
}
}
}