001/* 002 * Copyright 2006-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.springframework.batch.item.validator; 017 018import org.springframework.batch.item.ItemProcessor; 019import org.springframework.beans.factory.InitializingBean; 020import org.springframework.util.Assert; 021 022/** 023 * Simple implementation of {@link ItemProcessor} that validates input and 024 * returns it without modifications. Should the given {@link Validator} throw a 025 * {@link ValidationException} this processor will re-throw it to indicate the 026 * item should be skipped, unless {@link #setFilter(boolean)} is set to 027 * <code>true</code>, in which case <code>null</code> will be returned to 028 * indicate the item should be filtered. 029 * 030 * @author Robert Kasanicky 031 */ 032public class ValidatingItemProcessor<T> implements ItemProcessor<T, T>, InitializingBean { 033 034 private Validator<? super T> validator; 035 036 private boolean filter = false; 037 038 /** 039 * Default constructor 040 */ 041 public ValidatingItemProcessor() { 042 } 043 044 /** 045 * Creates a ValidatingItemProcessor based on the given Validator. 046 * 047 * @param validator the {@link Validator} instance to be used. 048 */ 049 public ValidatingItemProcessor(Validator<? super T> validator) { 050 this.validator = validator; 051 } 052 053 /** 054 * Set the validator used to validate each item. 055 * 056 * @param validator the {@link Validator} instance to be used. 057 */ 058 public void setValidator(Validator<? super T> validator) { 059 this.validator = validator; 060 } 061 062 /** 063 * Should the processor filter invalid records instead of skipping them? 064 * 065 * @param filter if set to {@code true}, items that fail validation are filtered 066 * ({@code null} is returned). Otherwise, a {@link ValidationException} will be 067 * thrown. 068 */ 069 public void setFilter(boolean filter) { 070 this.filter = filter; 071 } 072 073 /** 074 * Validate the item and return it unmodified 075 * 076 * @return the input item 077 * @throws ValidationException if validation fails 078 */ 079 @Override 080 public T process(T item) throws ValidationException { 081 try { 082 validator.validate(item); 083 } 084 catch (ValidationException e) { 085 if (filter) { 086 return null; // filter the item 087 } 088 else { 089 throw e; // skip the item 090 } 091 } 092 return item; 093 } 094 095 @Override 096 public void afterPropertiesSet() throws Exception { 097 Assert.notNull(validator, "Validator must not be null."); 098 } 099 100}