View Javadoc

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  
17  package org.springframework.batch.item.validator;
18  
19  import java.util.Collection;
20  
21  import org.springframework.beans.factory.InitializingBean;
22  import org.springframework.util.Assert;
23  import org.springframework.validation.BeanPropertyBindingResult;
24  import org.springframework.validation.BindException;
25  import org.springframework.validation.Errors;
26  
27  /**
28   * Adapts the {@link org.springframework.validation.Validator} interface to
29   * {@link org.springframework.batch.item.validator.Validator}.
30   * 
31   * @author Tomas Slanina
32   * @author Robert Kasanicky
33   */
34  public class SpringValidator<T> implements Validator<T>, InitializingBean {
35  
36  	private org.springframework.validation.Validator validator;
37  
38  	/**
39  	 * @see Validator#validate(Object)
40  	 */
41      @Override
42  	public void validate(T item) throws ValidationException {
43  
44  		if (!validator.supports(item.getClass())) {
45  			throw new ValidationException("Validation failed for " + item + ": " + item.getClass().getName()
46  					+ " class is not supported by validator.");
47  		}
48  
49  		BeanPropertyBindingResult errors = new BeanPropertyBindingResult(item, "item");
50  
51  		validator.validate(item, errors);
52  
53  		if (errors.hasErrors()) {
54  			throw new ValidationException("Validation failed for " + item + ": " + errorsToString(errors), new BindException(errors));
55  		}
56  	}
57  
58  	/**
59  	 * @return string of field errors followed by global errors.
60  	 */
61  	private String errorsToString(Errors errors) {
62  		StringBuffer builder = new StringBuffer();
63  
64  		appendCollection(errors.getFieldErrors(), builder);
65  		appendCollection(errors.getGlobalErrors(), builder);
66  
67  		return builder.toString();
68  	}
69  
70  	/**
71  	 * Append the string representation of elements of the collection (separated
72  	 * by new lines) to the given StringBuilder.
73  	 */
74  	private void appendCollection(Collection<?> collection, StringBuffer builder) {
75  		for (Object value : collection) {
76  			builder.append("\n");
77  			builder.append(value.toString());
78  		}
79  	}
80  
81  	public void setValidator(org.springframework.validation.Validator validator) {
82  		this.validator = validator;
83  	}
84  
85      @Override
86  	public void afterPropertiesSet() throws Exception {
87  		Assert.notNull(validator, "validator must be set");
88  
89  	}
90  }