RESOLVED - issue BATCH-694: make line separator a configurable property of FlatFileItemWriter
This commit is contained in:
@@ -66,7 +66,7 @@ import org.springframework.util.ClassUtils;
|
||||
*/
|
||||
public class FlatFileItemWriter extends ExecutionContextUserSupport implements ItemWriter, ItemStream, InitializingBean {
|
||||
|
||||
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
||||
private static final String DEFAULT_LINE_SEPARATOR = System.getProperty("line.separator");
|
||||
|
||||
private static final String WRITTEN_STATISTICS_NAME = "written";
|
||||
|
||||
@@ -94,6 +94,8 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
|
||||
|
||||
private List headerLines = new ArrayList();
|
||||
|
||||
private String lineSeparator = DEFAULT_LINE_SEPARATOR;
|
||||
|
||||
public FlatFileItemWriter() {
|
||||
setName(ClassUtils.getShortName(FlatFileItemWriter.class));
|
||||
}
|
||||
@@ -108,6 +110,15 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
|
||||
Assert.notNull(fieldSetCreator, "A FieldSetCreator must be provided.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the line separator. Defaults to the System property
|
||||
* line.separator.
|
||||
* @param lineSeparator the line separator to set
|
||||
*/
|
||||
public void setLineSeparator(String lineSeparator) {
|
||||
this.lineSeparator = lineSeparator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the {@link LineAggregator}. This will be used to
|
||||
* translate a {@link FieldSet} into a line for output.
|
||||
@@ -197,7 +208,7 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
|
||||
*/
|
||||
public void write(Object data) throws Exception {
|
||||
FieldSet fieldSet = fieldSetCreator.mapItem(data);
|
||||
lineBuffer.add(lineAggregator.aggregate(fieldSet) + LINE_SEPARATOR);
|
||||
lineBuffer.add(lineAggregator.aggregate(fieldSet) + lineSeparator);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -229,7 +240,7 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
|
||||
if (outputState.lastMarkedByteOffsetPosition == 0) {
|
||||
for (Iterator iterator = headerLines.iterator(); iterator.hasNext();) {
|
||||
String line = (String) iterator.next();
|
||||
lineBuffer.add(line + LINE_SEPARATOR);
|
||||
lineBuffer.add(line + lineSeparator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,14 +181,21 @@ public class FlatFileItemWriterTests extends TestCase {
|
||||
*/
|
||||
public void testWriteRecord() throws Exception {
|
||||
String args = "1";
|
||||
|
||||
// AggregatorStub ignores the LineDescriptor, so we pass null
|
||||
writer.write(args);
|
||||
writer.flush();
|
||||
String lineFromFile = readLine();
|
||||
assertEquals(args, lineFromFile);
|
||||
}
|
||||
|
||||
public void testWriteRecordWithrecordSeparator() throws Exception {
|
||||
writer.setLineSeparator("|");
|
||||
writer.write("1");
|
||||
writer.write("2");
|
||||
writer.flush();
|
||||
String lineFromFile = readLine();
|
||||
assertEquals("1|2|", lineFromFile);
|
||||
}
|
||||
|
||||
public void testRollback() throws Exception {
|
||||
writer.write("testLine1");
|
||||
// rollback
|
||||
|
||||
Reference in New Issue
Block a user