-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for quotes in DelimitedLineAggregator #1139
Comments
Kiichi Kuramoto commented I agree in this issue, too. CSV format is not standardized as an official specification, It is described as below to RFC 4180(Common Format and MIME Type for CSV Files).(No.5,6)
In addition, these are supported in some libraries.
Also, as another one of the reasons, FlatFileItemReader corresponds to the reading of delimited file the fields are enclosed in double-quotes. |
Doug Breaux commented Any update? |
If it help i wrote this class based on BeanWrapperFieldExtractor public class CSVQuotingBeanWrapperFieldExtractor<T> implements FieldExtractor<T>, InitializingBean {
private String[] names;
/**
* @param names field names to be extracted by the {@link #extract(Object)}
* method.
*/
public void setNames(final String[] names) {
Assert.notNull(names, "Names must be non-null");
this.names = Arrays.asList(names).toArray(new String[names.length]);
}
/**
* @see org.springframework.batch.item.file.transform.FieldExtractor#extract(java.lang.Object)
*/
@Override
public Object[] extract(final T item) {
final List<Object> values = new ArrayList<>();
final BeanWrapper bw = new BeanWrapperImpl(item);
for (final String propertyName : this.names) {
if (bw.getPropertyType(propertyName).isAssignableFrom(String.class)) {
values.add(doublequoteIfString(bw.getPropertyValue(propertyName)));
} else {
values.add(bw.getPropertyValue(propertyName));
}
}
return values.toArray();
}
@Override
public void afterPropertiesSet() {
Assert.notNull(names, "The 'names' property must be set.");
}
private String quote(final String str) {
return str != null ? "\"" + str + "\"" : null;
}
private Object doublequoteIfString(final Object obj) {
return obj instanceof String ? quote((String) obj) : obj;
}
} |
resolves spring-projects#1139 We may need to discuss escaping the quotes embedded in the elements. I chose the triple quote method to handle resolve this. An example would be fo"o would be replaced with "fo"""o"
resolves spring-projects#1139 We may need to discuss escaping the quotes embedded in the elements. I chose the triple quote method to handle resolve this. An example would be fo"o would be replaced with "fo"""o"
Doug Breaux opened BATCH-2463 and commented
I can't imagine why org.springframework.batch.item.file.transform.DelimitedLineAggregator doesn't already support a quote character like the delimited tokenizer does. I can see that the StringUtils class being used doesn't provide this capability either, so I'm guessing it just wasn't trivially easy to add, but it seems like a necessary capability for working with delimited files.
I know I need it and am going to have to implement it some other way instead. (As far as I can tell, I can't simply extend an existing Batch class to add a quote delimiter around an individual field value as needed.)
2 votes, 9 watchers
The text was updated successfully, but these errors were encountered: