BATCH-1306:
*FieldExtractorLineAggregator now replaces nulls with empty strings before calling doAggregate(). *PassthroughFieldExtractor no longer replaces nulls.
This commit is contained in:
@@ -22,12 +22,12 @@ import org.springframework.util.StringUtils;
|
||||
* delimited list of strings. The default delimiter is a comma.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class DelimitedLineAggregator<T> extends ExtractorLineAggregator<T> {
|
||||
|
||||
private String delimiter = ",";
|
||||
|
||||
|
||||
/**
|
||||
* Public setter for the delimiter.
|
||||
* @param delimiter the delimiter to set
|
||||
@@ -36,11 +36,9 @@ public class DelimitedLineAggregator<T> extends ExtractorLineAggregator<T> {
|
||||
this.delimiter = delimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.batch.item.file.transform.ExtractorLineAggregator#doAggregate(java.lang.Object[])
|
||||
*/
|
||||
public String doAggregate(Object[] item) {
|
||||
return StringUtils.arrayToDelimitedString(item, this.delimiter);
|
||||
@Override
|
||||
public String doAggregate(Object[] fields) {
|
||||
return StringUtils.arrayToDelimitedString(fields, this.delimiter);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,13 +43,29 @@ public abstract class ExtractorLineAggregator<T> implements LineAggregator<T> {
|
||||
|
||||
/**
|
||||
* Extract fields from the given item using the {@link FieldExtractor} and
|
||||
* then aggregate them. Null items are not allowed.
|
||||
* then aggregate them. Any null field returned by the extractor will be
|
||||
* replaced by an empty String. Null items are not allowed.
|
||||
*
|
||||
* @see org.springframework.batch.item.file.transform.LineAggregator#aggregate(java.lang.Object)
|
||||
*/
|
||||
public String aggregate(T item) {
|
||||
Assert.notNull(item);
|
||||
return this.doAggregate(this.fieldExtractor.extract(item));
|
||||
Object[] fields = this.fieldExtractor.extract(item);
|
||||
|
||||
//
|
||||
// Replace nulls with empty strings
|
||||
//
|
||||
Object[] args = new Object[fields.length];
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
if (fields[i] == null) {
|
||||
args[i] = "";
|
||||
}
|
||||
else {
|
||||
args[i] = fields[i];
|
||||
}
|
||||
}
|
||||
|
||||
return this.doAggregate(args);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -67,7 +67,6 @@ public class FormatterLineAggregator<T> extends ExtractorLineAggregator<T> {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Public setter for the locale.
|
||||
* @param locale the locale to set
|
||||
@@ -76,9 +75,7 @@ public class FormatterLineAggregator<T> extends ExtractorLineAggregator<T> {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.batch.item.file.transform.ExtractorLineAggregator#doAggregate(java.lang.Object[])
|
||||
*/
|
||||
@Override
|
||||
protected String doAggregate(Object[] fields) {
|
||||
|
||||
Assert.notNull(format);
|
||||
|
||||
@@ -34,31 +34,13 @@ public class PassThroughFieldExtractor<T> implements FieldExtractor<T> {
|
||||
public Object[] extract(T item) {
|
||||
|
||||
if (item.getClass().isArray()) {
|
||||
Object[] items = (Object[]) item;
|
||||
Object[] args = new Object[items.length];
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
if (items[i] == null)
|
||||
args[i] = "";
|
||||
else
|
||||
args[i] = items[i];
|
||||
}
|
||||
return args;
|
||||
return (Object[]) item;
|
||||
}
|
||||
|
||||
if (item instanceof Collection<?>) {
|
||||
Collection<?> items = (Collection<?>) item;
|
||||
Object[] args = new Object[items.size()];
|
||||
int i = 0;
|
||||
for (Object object : items) {
|
||||
if (object == null)
|
||||
args[i] = "";
|
||||
else
|
||||
args[i] = object;
|
||||
i++;
|
||||
}
|
||||
return args;
|
||||
return ((Collection<?>) item).toArray();
|
||||
}
|
||||
|
||||
|
||||
if (item instanceof FieldSet) {
|
||||
return ((FieldSet) item).getValues();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user