Author: epbernard
Date: 2008-11-03 16:28:09 -0500 (Mon, 03 Nov 2008)
New Revision: 15487
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintContextImpl.java
validator/trunk/validation-api/src/main/java/javax/validation/ConstraintContext.java
Removed:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/InvalidConstraintImpl.java
validator/trunk/validation-api/src/main/java/javax/validation/ReportAsSingleInvalidConstraint.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/LengthConstraint.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmptyConstraint.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotNullConstraint.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/PatternConstraint.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/NoGroupsConstraint.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/NoMessageConstraint.java
validator/trunk/validation-api/src/main/java/javax/validation/Constraint.java
Log:
BVAL-53 rename Context into ConstraintContext
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/LengthConstraint.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/LengthConstraint.java 2008-11-03
20:07:15 UTC (rev 15486)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/LengthConstraint.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -20,7 +20,7 @@
import javax.validation.Constraint;
import javax.validation.StandardConstraint;
import javax.validation.StandardConstraintDescriptor;
-import javax.validation.Context;
+import javax.validation.ConstraintContext;
/**
* Check that a string's length is between min and max.
@@ -37,7 +37,7 @@
max = parameters.max();
}
- public boolean isValid(Object value, Context context) {
+ public boolean isValid(Object value, ConstraintContext constraintContext) {
if ( value == null ) {
return true;
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmptyConstraint.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmptyConstraint.java 2008-11-03
20:07:15 UTC (rev 15486)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmptyConstraint.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -18,7 +18,7 @@
package org.hibernate.validation.constraints;
import javax.validation.Constraint;
-import javax.validation.Context;
+import javax.validation.ConstraintContext;
/**
* @author Hardy Ferentschik
@@ -29,7 +29,7 @@
public void initialize(NotEmpty parameters) {
}
- public boolean isValid(Object object, Context context) {
+ public boolean isValid(Object object, ConstraintContext constraintContext) {
if ( object == null ) {
return true;
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotNullConstraint.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotNullConstraint.java 2008-11-03
20:07:15 UTC (rev 15486)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotNullConstraint.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -18,7 +18,7 @@
package org.hibernate.validation.constraints;
import javax.validation.Constraint;
-import javax.validation.Context;
+import javax.validation.ConstraintContext;
import javax.validation.constraints.NotNull;
/**
@@ -31,7 +31,7 @@
public void initialize(NotNull parameters) {
}
- public boolean isValid(Object object, Context context) {
+ public boolean isValid(Object object, ConstraintContext constraintContext) {
return object != null;
}
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/PatternConstraint.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/PatternConstraint.java 2008-11-03
20:07:15 UTC (rev 15486)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/PatternConstraint.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -19,7 +19,7 @@
import java.util.regex.Matcher;
import javax.validation.Constraint;
-import javax.validation.Context;
+import javax.validation.ConstraintContext;
/**
* @author Hardy Ferentschik
@@ -35,7 +35,7 @@
);
}
- public boolean isValid(Object value, Context context) {
+ public boolean isValid(Object value, ConstraintContext constraintContext) {
if ( value == null ) {
return true;
}
Copied:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintContextImpl.java
(from rev 15465,
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ContextImpl.java)
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintContextImpl.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintContextImpl.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -0,0 +1,69 @@
+package org.hibernate.validation.engine;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+import javax.validation.ConstraintContext;
+import javax.validation.ConstraintDescriptor;
+
+import org.hibernate.validation.impl.ConstraintDescriptorImpl;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class ConstraintContextImpl implements ConstraintContext {
+ private final ConstraintDescriptor constraintDescriptor;
+ private final List<ErrorMessage> errorMessages;
+ private boolean defaultDisabled;
+
+ public ConstraintContextImpl(ConstraintDescriptorImpl constraintDescriptor) {
+ this.constraintDescriptor = constraintDescriptor;
+ this.errorMessages = new ArrayList<ErrorMessage>(3);
+ }
+
+ public void disableDefaultError() {
+ defaultDisabled = true;
+ }
+
+ public String getDefaultErrorMessage() {
+ return ( String ) constraintDescriptor.getParameters().get("message");
+ }
+
+ public void addError(String message) {
+ //FIXME get the default property if property-level
+ errorMessages.add( new ErrorMessage( message, null ) );
+ }
+
+ public void addError(String message, String property) {
+ //FIXME: make sure the property is valid
+ errorMessages.add( new ErrorMessage( message, property ) );
+ }
+
+ public List<ErrorMessage> getErrorMessages() {
+ List<ErrorMessage> returnedErrorMessages = new ArrayList<ErrorMessage>(
errorMessages.size() + 1 );
+ Collections.copy( returnedErrorMessages, errorMessages );
+ if ( ! defaultDisabled ) {
+ //FIXME get the default property if property-level
+ returnedErrorMessages.add( new ErrorMessage( getDefaultErrorMessage(), null) );
+ }
+ return returnedErrorMessages;
+ }
+
+ public static class ErrorMessage {
+ private final String message;
+ private final String property;
+
+ private ErrorMessage(String message, String property) {
+ this.message = message;
+ this.property = property;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public String getProperty() {
+ return property;
+ }
+ }
+}
Property changes on:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintContextImpl.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2008-11-03
20:07:15 UTC (rev 15486)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -30,7 +30,6 @@
import java.util.concurrent.ConcurrentHashMap;
import javax.validation.ConstraintDescriptor;
import javax.validation.ConstraintFactory;
-import javax.validation.ElementDescriptor;
import javax.validation.ConstraintViolation;
import javax.validation.MessageResolver;
import javax.validation.Validator;
@@ -171,10 +170,10 @@
final Object leafBeanInstance = context.peekValidatedObject();
Object value = metaConstraint.getValue( leafBeanInstance );
- ContextImpl contextImpl = new ContextImpl(constraintDescriptor);
+ ConstraintContextImpl contextImpl = new ConstraintContextImpl(constraintDescriptor);
if ( !constraintDescriptor.getConstraintImplementation().isValid( value, contextImpl )
) {
- for ( ContextImpl.ErrorMessage error : contextImpl.getErrorMessages() ) {
+ for ( ConstraintContextImpl.ErrorMessage error : contextImpl.getErrorMessages() ) {
String message = messageResolver.interpolate(
error.getMessage(),
constraintDescriptor,
@@ -304,10 +303,10 @@
continue;
}
- ContextImpl contextImpl = new ContextImpl(wrapper.descriptor);
+ ConstraintContextImpl contextImpl = new ConstraintContextImpl(wrapper.descriptor);
if ( !wrapper.descriptor.getConstraintImplementation().isValid( wrapper.value,
contextImpl ) ) {
- for ( ContextImpl.ErrorMessage error : contextImpl.getErrorMessages() ) {
+ for ( ConstraintContextImpl.ErrorMessage error : contextImpl.getErrorMessages() ) {
String message = messageResolver.interpolate(
error.getMessage(),
wrapper.descriptor,
@@ -367,9 +366,9 @@
continue;
}
- ContextImpl contextImpl = new ContextImpl(constraintDescriptor);
+ ConstraintContextImpl contextImpl = new ConstraintContextImpl(constraintDescriptor);
if ( !constraintDescriptor.getConstraintImplementation().isValid( object, contextImpl
) ) {
- for ( ContextImpl.ErrorMessage error : contextImpl.getErrorMessages() ) {
+ for ( ConstraintContextImpl.ErrorMessage error : contextImpl.getErrorMessages() ) {
String message = messageResolver.interpolate(
error.getMessage(),
constraintDescriptor,
Deleted:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/InvalidConstraintImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/InvalidConstraintImpl.java 2008-11-03
20:07:15 UTC (rev 15486)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/InvalidConstraintImpl.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -1,138 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
http://www.apache.org/licenses/LICENSE-2.0
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-package org.hibernate.validation.impl;
-
-import java.util.HashSet;
-import java.util.Set;
-import javax.validation.InvalidConstraint;
-
-/**
- * @author Emmanuel Bernard
- * @author Hardy Ferentschik
- */
-public class InvalidConstraintImpl<T> implements InvalidConstraint<T> {
- private String message;
- private T rootBean;
- private Class<T> beanClass;
- private Object value;
- private String propertyPath;
- private HashSet<String> groups;
- private Object leafBeanInstance;
-
-
- public InvalidConstraintImpl(String message, T rootBean, Class<T> beanClass,
Object leafBeanInstance, Object value, String propertyPath, String group) {
- this.message = message;
- this.rootBean = rootBean;
- this.beanClass = beanClass;
- this.value = value;
- this.propertyPath = propertyPath;
- groups = new HashSet<String>();
- groups.add( group );
- this.leafBeanInstance = leafBeanInstance;
- }
-
- /**
- * {@inheritDoc}
- */
- public String getMessage() {
- return message;
- }
-
- /**
- * {@inheritDoc}
- */
- public T getRootBean() {
- return rootBean;
- }
-
- public Object getLeafBean() {
- return leafBeanInstance;
- }
-
- /**
- * {@inheritDoc}
- */
- public Class<T> getBeanClass() {
- return beanClass;
- }
-
- /**
- * {@inheritDoc}
- */
- public Object getValue() {
- return value;
- }
-
- /**
- * {@inheritDoc}
- */
- public String getPropertyPath() {
- return propertyPath;
- }
-
- /**
- * {@inheritDoc}
- */
- public Set<String> getGroups() {
- return groups;
- }
-
- public void addGroups(Set<String> groupSet) {
- groups.addAll( groupSet );
- }
-
- @Override
- public boolean equals(Object o) {
- if ( this == o ) {
- return true;
- }
- if ( !( o instanceof InvalidConstraintImpl ) ) {
- return false;
- }
-
- InvalidConstraintImpl that = ( InvalidConstraintImpl ) o;
-
- if ( beanClass != null ? !beanClass.equals( that.beanClass ) : that.beanClass != null )
{
- return false;
- }
- if ( message != null ? !message.equals( that.message ) : that.message != null ) {
- return false;
- }
- if ( propertyPath != null ? !propertyPath.equals( that.propertyPath ) :
that.propertyPath != null ) {
- return false;
- }
- if ( rootBean != null ? !rootBean.equals( that.rootBean ) : that.rootBean != null ) {
- return false;
- }
- if ( value != null ? !value.equals( that.value ) : that.value != null ) {
- return false;
- }
-
- return true;
- }
-
- @Override
- public int hashCode() {
- int result = message != null ? message.hashCode() : 0;
- result = 31 * result + ( rootBean != null ? rootBean.hashCode() : 0 );
- result = 31 * result + ( beanClass != null ? beanClass.hashCode() : 0 );
- result = 31 * result + ( value != null ? value.hashCode() : 0 );
- result = 31 * result + ( propertyPath != null ? propertyPath.hashCode() : 0 );
- return result;
- }
-}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java 2008-11-03
20:07:15 UTC (rev 15486)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -31,7 +31,7 @@
import javax.validation.Validator;
import javax.validation.ValidatorBuilder;
import javax.validation.ValidatorFactory;
-import javax.validation.Context;
+import javax.validation.ConstraintContext;
import javax.validation.bootstrap.SpecializedBuilderFactory;
import javax.validation.spi.ValidationProvider;
@@ -237,7 +237,7 @@
class BadlyBehavedNotNullConstraint extends NotNullConstraint {
@Override
- public boolean isValid(Object object, Context context) {
+ public boolean isValid(Object object, ConstraintContext constraintContext) {
return true;
}
}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/NoGroupsConstraint.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/NoGroupsConstraint.java 2008-11-03
20:07:15 UTC (rev 15486)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/NoGroupsConstraint.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -18,7 +18,7 @@
package org.hibernate.validation.eg.constraint;
import javax.validation.Constraint;
-import javax.validation.Context;
+import javax.validation.ConstraintContext;
/**
* @author Hardy Ferentschik
@@ -28,7 +28,7 @@
public void initialize(NoGroups parameters) {
}
- public boolean isValid(Object object, Context context) {
+ public boolean isValid(Object object, ConstraintContext constraintContext) {
return false;
}
}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/NoMessageConstraint.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/NoMessageConstraint.java 2008-11-03
20:07:15 UTC (rev 15486)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/NoMessageConstraint.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -18,7 +18,7 @@
package org.hibernate.validation.eg.constraint;
import javax.validation.Constraint;
-import javax.validation.Context;
+import javax.validation.ConstraintContext;
/**
* @author Hardy Ferentschik
@@ -28,7 +28,7 @@
public void initialize(NoMessage parameters) {
}
- public boolean isValid(Object object, Context context) {
+ public boolean isValid(Object object, ConstraintContext constraintContext) {
return false;
}
}
\ No newline at end of file
Modified: validator/trunk/validation-api/src/main/java/javax/validation/Constraint.java
===================================================================
---
validator/trunk/validation-api/src/main/java/javax/validation/Constraint.java 2008-11-03
20:07:15 UTC (rev 15486)
+++
validator/trunk/validation-api/src/main/java/javax/validation/Constraint.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -44,6 +44,6 @@
*
* @return true if object pass the constraint
*/
- boolean isValid(Object object, Context validationContext);
+ boolean isValid(Object object, ConstraintContext validationContext);
}
\ No newline at end of file
Copied:
validator/trunk/validation-api/src/main/java/javax/validation/ConstraintContext.java (from
rev 15475, validator/trunk/validation-api/src/main/java/javax/validation/Context.java)
===================================================================
--- validator/trunk/validation-api/src/main/java/javax/validation/ConstraintContext.java
(rev 0)
+++
validator/trunk/validation-api/src/main/java/javax/validation/ConstraintContext.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -0,0 +1,55 @@
+package javax.validation;
+
+/**
+ * Provide contextual data and operation when applying a given constraint implementation
+ *
+ * @author Emmanuel Bernard
+ */
+public interface ConstraintContext {
+ /**
+ * Disable default error message and default ConstraintViolation object generation.
+ * Useful to set a different error message or generate an ConstraintViolation based on
+ * a different property
+ *
+ * @see #addError(String)
+ * @see #addError(String, String)
+ */
+ void disableDefaultError();
+
+ /**
+ * return the current unexpanded default message
+ * TODO: is it needed
+ */
+ String getDefaultErrorMessage();
+
+ /**
+ * Add a new unexpanded error message.
+ * <p/>
+ * If isValid returns false, a ConstraintViolation object will be built per error
message
+ * including the default one unless #disableDefaultErrorMEssage() has been called.
+ * <p/>
+ * Aside from the error message, ConstraintViolation objects generated from such a call
+ * contains the same contextual information (root bean, path and so on)
+ * <p/>
+ * This method can be called multiple time. One ConstraintViolation instance per call is
created.
+ *
+ * @param message new unexpanded error message
+ */
+ void addError(String message);
+
+ /**
+ * Add a new unexpanded error message to a given sub property.
+ * <p/>
+ * If isValid returns false, a ConstraintViolation object will be built per error
message including the default one
+ * if null apply to the current property or the bean the constraint is applied on,
otherwise apply to the <code>property</code> named
+ * <p/>
+ * TODO exception or swallowed when bean level instance is not present?
+ *
+ * @param message new unexpanded error message
+ * @param property property name the ConstraintViolation is targeting
+ *
+ * @throws ValidationException when the property is not present on the bean level
object
+ */
+ void addError(String message, String property);
+
+}
Property changes on:
validator/trunk/validation-api/src/main/java/javax/validation/ConstraintContext.java
___________________________________________________________________
Name: svn:keywords
+ Id
Deleted:
validator/trunk/validation-api/src/main/java/javax/validation/ReportAsSingleInvalidConstraint.java
===================================================================
---
validator/trunk/validation-api/src/main/java/javax/validation/ReportAsSingleInvalidConstraint.java 2008-11-03
20:07:15 UTC (rev 15486)
+++
validator/trunk/validation-api/src/main/java/javax/validation/ReportAsSingleInvalidConstraint.java 2008-11-03
21:28:09 UTC (rev 15487)
@@ -1,35 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
http://www.apache.org/licenses/LICENSE-2.0
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-package javax.validation;
-
-import java.lang.annotation.Target;
-import java.lang.annotation.Retention;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
-
-/**
- * A constraint annotation annotated with ReportAsSingleInvalidConstraint
- * will return the composed annotation error report if any of the composing annotations
- * fail. The error reports of each individual composing constraint is ignored.
- *
- * @author Emmanuel Bernard
- */
-@Target({ ANNOTATION_TYPE })
-@Retention(RUNTIME)
-public @interface ReportAsSingleInvalidConstraint {
-}