RESOLVED - BATCH-1177: Allow ValidatingItemProcessor to filter items on ValidationException

This commit is contained in:
dhgarrette
2009-04-04 15:01:00 +00:00
parent 17dd5bb3f4
commit d71b260cc6
2 changed files with 53 additions and 17 deletions

View File

@@ -30,6 +30,8 @@ public class ValidatingItemProcessor<T> implements ItemProcessor<T, T> {
private Validator<? super T> validator;
private boolean filter = false;
public ValidatingItemProcessor(Validator<? super T> validator){
Assert.notNull(validator, "Validator must not be null.");
this.validator = validator;
@@ -44,6 +46,15 @@ public class ValidatingItemProcessor<T> implements ItemProcessor<T, T> {
this.validator = validator;
}
/**
* Should the processor filter invalid records instead of skipping them?
*
* @param filter
*/
public void setFilter(boolean filter) {
this.filter = filter;
}
/**
* Validate the item and return it unmodified
*
@@ -51,7 +62,16 @@ public class ValidatingItemProcessor<T> implements ItemProcessor<T, T> {
* @throws ValidationException if validation fails
*/
public T process(T item) throws ValidationException {
validator.validate(item);
try {
validator.validate(item);
} catch (ValidationException e) {
if (filter) {
return null; // filter the item
}
else {
throw e; // skip the item
}
}
return item;
}