View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.integration.spring;
14  
15  import net.sf.oval.ConstraintViolation;
16  import net.sf.oval.Validator;
17  import net.sf.oval.context.FieldContext;
18  import net.sf.oval.context.OValContext;
19  import net.sf.oval.exception.ValidationFailedException;
20  import net.sf.oval.internal.Log;
21  
22  import org.springframework.beans.factory.InitializingBean;
23  import org.springframework.util.Assert;
24  import org.springframework.validation.Errors;
25  
26  /**
27   * @author Sebastian Thomschke
28   */
29  public class SpringValidator implements org.springframework.validation.Validator, InitializingBean
30  {
31  	private static final Log LOG = Log.getLog(SpringValidator.class);
32  
33  	private Validator validator;
34  
35  	public SpringValidator()
36  	{
37  		super();
38  	}
39  
40  	public SpringValidator(final Validator validator)
41  	{
42  		this.validator = validator;
43  	}
44  
45  	/**
46  	 * {@inheritDoc}
47  	 */
48  	public void afterPropertiesSet() throws Exception
49  	{
50  		Assert.notNull(validator, "Property [validator] must be set");
51  	}
52  
53  	/**
54  	 * @return the validator
55  	 */
56  	public Validator getValidator()
57  	{
58  		return validator;
59  	}
60  
61  	/**
62  	 * @param validator the validator to set
63  	 */
64  	public void setValidator(final Validator validator)
65  	{
66  		this.validator = validator;
67  	}
68  
69  	/**
70  	 * {@inheritDoc}
71  	 */
72  	public boolean supports(@SuppressWarnings("rawtypes") final Class clazz)
73  	{
74  		return true;
75  	}
76  
77  	/**
78  	 * {@inheritDoc}
79  	 */
80  	public void validate(final Object objectToValidate, final Errors errors)
81  	{
82  		try
83  		{
84  			for (final ConstraintViolation violation : validator.validate(objectToValidate))
85  			{
86  				final OValContext ctx = violation.getContext();
87  				final String errorCode = violation.getErrorCode();
88  				final String errorMessage = violation.getMessage();
89  
90  				if (ctx instanceof FieldContext)
91  				{
92  					final String fieldName = ((FieldContext) ctx).getField().getName();
93  					errors.rejectValue(fieldName, errorCode, errorMessage);
94  				}
95  				else
96  					errors.reject(errorCode, errorMessage);
97  			}
98  		}
99  		catch (final ValidationFailedException ex)
100 		{
101 			LOG.error("Unexpected error during validation", ex);
102 
103 			errors.reject(ex.getMessage());
104 		}
105 	}
106 }