com.google.gwt.validation.client.impl.AbstractGwtSpecificValidator Maven / Gradle / Ivy
/*
* Copyright 2010 Google Inc.
*
* 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 com.google.gwt.validation.client.impl;
import com.google.gwt.validation.client.BaseMessageInterpolator.ContextImpl;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintViolation;
import javax.validation.MessageInterpolator;
import javax.validation.groups.Default;
/**
* EXPERIMENTAL and subject to change. Do not use this in
* production code.
*
* Base methods for implementing a {@link GwtSpecificValidator}.
*
* All methods that do not need to be generated go here.
*
* @param the type object to validate
*/
public abstract class AbstractGwtSpecificValidator implements
GwtSpecificValidator {
/**
* Builds attributes one at a time.
*
* Used to create a attribute map for annotations.
*/
public static final class AttributeBuilder {
private final HashMap tempMap = new HashMap();
private AttributeBuilder() {
}
public Map build() {
return Collections.unmodifiableMap(tempMap);
}
public AttributeBuilder put(String key, Object value) {
tempMap.put(key, value);
return this;
}
}
public static AttributeBuilder attributeBuilder() {
return new AttributeBuilder();
}
protected void addSingleViolation(
GwtValidationContext context, Set> violations,
G object, V value, ConstraintDescriptorImpl constraintDescriptor) {
ConstraintValidatorContextImpl constraintValidatorContext =
context.createConstraintValidatorContext(constraintDescriptor);
addViolations(context, violations, object, value, constraintDescriptor,
constraintValidatorContext);
}
/**
* Perform the actual validation of a single {@link ConstraintValidator}.
*
* As a side effect {@link ConstraintViolation}s may be added to
* {@code violations}.
*
* @return true if there was any constraint violations
*/
protected boolean validate(
GwtValidationContext context, Set> violations,
G object, V value, ConstraintValidator validator,
ConstraintDescriptorImpl constraintDescriptor, Class[] groups) {
validator.initialize(constraintDescriptor.getAnnotation());
ConstraintValidatorContextImpl constraintValidatorContext =
context.createConstraintValidatorContext(constraintDescriptor);
// TODO(nchalko) set empties to Default earlier.
Set> constraintGroup = constraintDescriptor.getGroups();
if (groups.length == 0) {
groups = new Class[]{Default.class};
}
if (constraintGroup.isEmpty()) {
constraintGroup = new HashSet>();
constraintGroup.add(Default.class);
}
// check group
if (!containsAny(groups, constraintGroup)) {
return false;
}
if (!validator.isValid(value, constraintValidatorContext)) {
addViolations(//
context, //
violations, //
object, //
value, //
constraintDescriptor, //
constraintValidatorContext);
return true;
}
return false;
}
private void addViolations(
GwtValidationContext context, Set> violations,
G object, V value, ConstraintDescriptorImpl constraintDescriptor,
ConstraintValidatorContextImpl constraintValidatorContext) {
Set mps = constraintValidatorContext.getMessageAndPaths();
for (MessageAndPath messageAndPath : mps) {
ConstraintViolation violation = createConstraintViolation(//
context, //
object, //
value, //
constraintDescriptor, //
messageAndPath);
violations.add(violation);
}
}
private boolean containsAny(T[] left, Collection right) {
for (T t : left) {
if (right.contains(t)) {
return true;
}
}
return false;
}
private ConstraintViolation createConstraintViolation(
GwtValidationContext context, G object, V value,
ConstraintDescriptorImpl constraintDescriptor,
MessageAndPath messageAndPath) {
MessageInterpolator messageInterpolator = context.getMessageInterpolator();
ContextImpl messageContext = new ContextImpl(constraintDescriptor, value);
String message = messageInterpolator.interpolate(
messageAndPath.getMessage(), messageContext);
ConstraintViolation violation = ConstraintViolationImpl. builder() //
.setConstraintDescriptor(constraintDescriptor) //
.setInvalidValue(value) //
.setLeafBean(object) //
.setMessage(message) //
.setMessageTemplate(messageAndPath.getMessage()) //
.setPropertyPath(messageAndPath.getPath()) //
.setRootBean(context.getRootBean()) //
.setRootBeanClass(context.getRootBeanClass()) //
.build();
return violation;
}
}