| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.springframework.batch.item.validator; |
| 17 | |
| 18 | import org.springframework.batch.item.ItemProcessor; |
| 19 | import org.springframework.beans.factory.InitializingBean; |
| 20 | import org.springframework.util.Assert; |
| 21 | |
| 22 | /** |
| 23 | * Simple implementation of {@link ItemProcessor} that validates input and |
| 24 | * returns it without modifications. Should the given {@link Validator} throw a |
| 25 | * {@link ValidationException} this processor will re-throw it to indicate the |
| 26 | * item should be skipped, unless {@link #setFilter(boolean)} is set to |
| 27 | * <code>true</code>, in which case <code>null</code> will be returned to |
| 28 | * indicate the item should be filtered. |
| 29 | * |
| 30 | * @author Robert Kasanicky |
| 31 | */ |
| 32 | public class ValidatingItemProcessor<T> implements ItemProcessor<T, T>, InitializingBean { |
| 33 | |
| 34 | private Validator<? super T> validator; |
| 35 | |
| 36 | private boolean filter = false; |
| 37 | |
| 38 | /** |
| 39 | * Default constructor |
| 40 | */ |
| 41 | public ValidatingItemProcessor() { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Creates a ValidatingItemProcessor based on the given Validator. |
| 46 | */ |
| 47 | public ValidatingItemProcessor(Validator<? super T> validator) { |
| 48 | this.validator = validator; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Set the validator used to validate each item. |
| 53 | * |
| 54 | * @param validator |
| 55 | */ |
| 56 | public void setValidator(Validator<? super T> validator) { |
| 57 | this.validator = validator; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Should the processor filter invalid records instead of skipping them? |
| 62 | * |
| 63 | * @param filter |
| 64 | */ |
| 65 | public void setFilter(boolean filter) { |
| 66 | this.filter = filter; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Validate the item and return it unmodified |
| 71 | * |
| 72 | * @return the input item |
| 73 | * @throws ValidationException if validation fails |
| 74 | */ |
| 75 | @Override |
| 76 | public T process(T item) throws ValidationException { |
| 77 | try { |
| 78 | validator.validate(item); |
| 79 | } |
| 80 | catch (ValidationException e) { |
| 81 | if (filter) { |
| 82 | return null; // filter the item |
| 83 | } |
| 84 | else { |
| 85 | throw e; // skip the item |
| 86 | } |
| 87 | } |
| 88 | return item; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void afterPropertiesSet() throws Exception { |
| 93 | Assert.notNull(validator, "Validator must not be null."); |
| 94 | } |
| 95 | |
| 96 | } |