| 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.support.DelegatingItemReader; |
| 19 | import org.springframework.util.Assert; |
| 20 | |
| 21 | /** |
| 22 | * Simple extension of {@link DelegatingItemReader} that provides for |
| 23 | * validation before returning input. |
| 24 | * |
| 25 | * @author Lucas Ward |
| 26 | * |
| 27 | */ |
| 28 | public class ValidatingItemReader extends DelegatingItemReader { |
| 29 | |
| 30 | private Validator validator; |
| 31 | |
| 32 | /* (non-Javadoc) |
| 33 | * @see org.springframework.batch.item.reader.DelegatingItemReader#afterPropertiesSet() |
| 34 | */ |
| 35 | public void afterPropertiesSet() throws Exception { |
| 36 | super.afterPropertiesSet(); |
| 37 | Assert.notNull(validator, "Validator must not be null."); |
| 38 | } |
| 39 | |
| 40 | /* (non-Javadoc) |
| 41 | * @see org.springframework.batch.item.reader.DelegatingItemReader#read() |
| 42 | */ |
| 43 | public Object read() throws Exception { |
| 44 | Object input = super.read(); |
| 45 | if(input != null){ |
| 46 | validator.validate(input); |
| 47 | } |
| 48 | return input; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Set the validator used to validate each item. |
| 53 | * |
| 54 | * @param validator |
| 55 | */ |
| 56 | public void setValidator(Validator validator) { |
| 57 | this.validator = validator; |
| 58 | } |
| 59 | } |