Revision: 241
http://svn.sourceforge.net/openutils/?rev=241&view=rev
Author: fgiust
Date: 2007-02-11 13:49:49 -0800 (Sun, 11 Feb 2007)
Log Message:
-----------
work in progress: don't let hibernate validator resolve messages, pass them back to spring for resolution
Modified Paths:
--------------
trunk/openutils-spring/src/main/java/it/openutils/spring/validation/hibernate/AnnotationValidator.java
Modified: trunk/openutils-spring/src/main/java/it/openutils/spring/validation/hibernate/AnnotationValidator.java
===================================================================
--- trunk/openutils-spring/src/main/java/it/openutils/spring/validation/hibernate/AnnotationValidator.java 2007-02-11 17:22:27 UTC (rev 240)
+++ trunk/openutils-spring/src/main/java/it/openutils/spring/validation/hibernate/AnnotationValidator.java 2007-02-11 21:49:49 UTC (rev 241)
@@ -1,7 +1,13 @@
package it.openutils.spring.validation.hibernate;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.ResourceBundle;
+
import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
@@ -10,12 +16,26 @@
* @author fgiust
* @version $Id$
*/
-public class AnnotationValidator implements Validator
+public class AnnotationValidator implements Validator, ApplicationContextAware
{
/**
- * @see org.springframework.validation.Validator#supports(java.lang.Class)
+ * ApplicationContext needed for message resolution.
*/
+ private ApplicationContext applicationContext;
+
+ /**
+ * Sets the applicationContext.
+ * @param applicationContext the applicationContext to set
+ */
+ public void setApplicationContext(ApplicationContext applicationContext)
+ {
+ this.applicationContext = applicationContext;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
@SuppressWarnings("unchecked")
public boolean supports(Class clazz)
{
@@ -28,15 +48,47 @@
@SuppressWarnings("unchecked")
public void validate(Object obj, Errors errors)
{
- ClassValidator classValidator = new ClassValidator(obj.getClass());
+ ClassValidator classValidator = new ClassValidator(obj.getClass(), new KeybackResourceBundle());
+
InvalidValue[] validationMessages = classValidator.getInvalidValues(obj);
for (InvalidValue value : validationMessages)
{
- String propertyName = value.getPropertyName();
-
+ String propertyName = value.getPropertyPath();
+ String fqPropertyName = errors.getObjectName() + "." + propertyName;
String message = value.getMessage();
- errors.rejectValue(propertyName, null, message);
+ errors.rejectValue(propertyName, message, new Object[]{fqPropertyName }, null);
+
}
}
+
+ /**
+ * ResourceBundle implementation that always returns the same String used as a key. Needed to avoid making
+ * ClassValidator resolve message keys, so that they will still be available to Spring.
+ * @author fgiust
+ * @version $Id$
+ */
+ protected static class KeybackResourceBundle extends ResourceBundle
+ {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Enumeration<String> getKeys()
+ {
+ return new Hashtable<String, String>().keys();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected Object handleGetObject(String key)
+ {
+ return key;
+ }
+
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|