IN PROGRESS - BATCH-826: Create callback for header and footer reading in xml and flat files

changed header and footer items to callbacks in StaxEventItemWriter
This commit is contained in:
robokaso
2008-09-11 09:49:42 +00:00
parent 4b5cbef820
commit 13e5796b85
4 changed files with 125 additions and 43 deletions

View File

@@ -7,7 +7,6 @@ import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -110,9 +109,9 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
private boolean saveState = true;
private List<?> headerItems = new ArrayList<Object>();
private StaxWriterCallback headerCallback;
private List<?> footerItems = new ArrayList<Object>();
private StaxWriterCallback footerCallback;
public StaxEventItemWriter() {
setName(ClassUtils.getShortName(StaxEventItemWriter.class));
@@ -136,6 +135,21 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
this.marshaller = marshaller;
}
/**
* headerCallback is called before writing any items.
*/
public void setHeaderCallback(StaxWriterCallback headerCallback) {
this.headerCallback = headerCallback;
}
/**
* footerCallback is called after writing all items but before closing the
* file
*/
public void setFooterCallback(StaxWriterCallback footerCallback) {
this.footerCallback = footerCallback;
}
/**
* Get used encoding.
*
@@ -219,28 +233,6 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
this.overwriteOutput = overwriteOutput;
}
/**
* 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<?> 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) {
this.saveState = saveState;
}
@@ -275,7 +267,9 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
if (startAtPosition == 0) {
try {
doWrite(headerItems);
if (headerCallback != null) {
headerCallback.write(delegateEventWriter);
}
}
catch (IOException e) {
throw new ItemStreamException("Failed to write headerItems", e);
@@ -396,7 +390,9 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
}
try {
doWrite(footerItems);
if (footerCallback != null) {
footerCallback.write(delegateEventWriter);
}
delegateEventWriter.flush();
endDocument(delegateEventWriter);
}

View File

@@ -0,0 +1,20 @@
package org.springframework.batch.item.xml;
import java.io.IOException;
import javax.xml.stream.XMLEventWriter;
/**
* Callback interface for writing to an XML file - useful e.g. for handling headers
* and footers.
*
* @author Robert Kasanicky
*/
public interface StaxWriterCallback {
/**
* Write contents using the supplied {@link XMLEventWriter}. It is not
* required to flush the writer inside this method.
*/
void write(XMLEventWriter writer) throws IOException;
}