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