![JAR search and dependency download from the Maven repository](/logo.png)
com.google.gwt.validation.client.Validation 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;
import com.google.gwt.core.client.GWT;
import java.util.List;
import javax.validation.Configuration;
import javax.validation.ValidationException;
import javax.validation.ValidationProviderResolver;
import javax.validation.ValidatorFactory;
import javax.validation.bootstrap.GenericBootstrap;
import javax.validation.bootstrap.ProviderSpecificBootstrap;
import javax.validation.spi.BootstrapState;
import javax.validation.spi.ValidationProvider;
/**
* EXPERIMENTAL and subject to change. Do not use this in
* production code.
*
* This class is the entry point for Bean Validation. There are three ways to
* bootstrap it:
*
* -
* The easiest approach is to build the default
ValidatorFactory
.
*
* {@code ValidatorFactory factory = Validation.buildDefaultValidatorFactory();}
* In this case, the default validation provider resolver will be used to locate
* available providers. The chosen provider is defined as followed:
*
* - Since GWT does not support XML configuration, the first provider returned
* by the
ValidationProviderResolver
instance is used.
*
*
* -
* The second bootstrap approach allows to choose a custom
*
ValidationProviderResolver
. The chosen
* ValidationProvider
is then determined in the same way as in the
* default bootstrapping case (see above).
*
* {@code
* Configuration> configuration = Validation
* .byDefaultProvider()
* .providerResolver( new MyResolverStrategy() )
* .configure();
* ValidatorFactory factory = configuration.buildValidatorFactory();}
*
*
* -
* The third approach allows you to specify explicitly and in a type safe
* fashion the expected provider.
*
* Optionally you can choose a custom
ValidationProviderResolver
.
*
* {@code
* ACMEConfiguration configuration = Validation
* .byProvider(ACMEProvider.class)
* .providerResolver( new MyResolverStrategy() ) // optionally set the provider resolver
* .configure();
* ValidatorFactory factory = configuration.buildValidatorFactory();}
*
*
*
* Note:
*
* -
* The
ValidatorFactory
object built by the bootstrap process
* should be cached and shared amongst Validator
consumers.
* -
* This class is thread-safe.
*
*
* This class was modified by Google from the original
* javax.validation.Validation source to make it suitable for GWT.
*/
public class Validation {
// private class, not exposed
private static class GenericBootstrapImpl implements GenericBootstrap,
BootstrapState {
private ValidationProviderResolver defaultResolver;
private ValidationProviderResolver resolver;
public Configuration> configure() {
ValidationProviderResolver aResolver = this.resolver == null
? getDefaultValidationProviderResolver() : this.resolver;
List> resolvers;
try {
resolvers = aResolver.getValidationProviders();
} catch (RuntimeException re) {
throw new ValidationException(
"Unable to get available provider resolvers.", re);
}
if (resolvers.size() == 0) {
// FIXME looks like an assertion error almost
throw new ValidationException("Unable to find a default provider");
}
Configuration> config;
try {
config = aResolver.getValidationProviders().get(0).createGenericConfiguration(
this);
} catch (RuntimeException re) {
throw new ValidationException("Unable to instantiate Configuration.",
re);
}
return config;
}
public ValidationProviderResolver getDefaultValidationProviderResolver() {
if (defaultResolver == null) {
defaultResolver = GWT.create(ValidationProviderResolver.class);
}
return defaultResolver;
}
public ValidationProviderResolver getValidationProviderResolver() {
return resolver;
}
public GenericBootstrap providerResolver(ValidationProviderResolver resolver) {
this.resolver = resolver;
return this;
}
}
// private class, not exposed
private static class ProviderSpecificBootstrapImpl
, U extends ValidationProvider>
implements ProviderSpecificBootstrap {
private ValidationProviderResolver resolver;
private final Class validationProviderClass;
public ProviderSpecificBootstrapImpl(Class validationProviderClass) {
this.validationProviderClass = validationProviderClass;
}
/**
* Determine the provider implementation suitable for byProvider(Class) and
* delegate the creation of this specific Configuration subclass to the
* provider.
*
* @return a Configuration sub interface implementation
*/
public T configure() {
if (validationProviderClass == null) {
throw new ValidationException(
"builder is mandatory. Use Validation.byDefaultProvider() to use the generic provider discovery mechanism");
}
// used mostly as a BootstrapState
GenericBootstrapImpl state = new GenericBootstrapImpl();
if (resolver == null) {
resolver = state.getDefaultValidationProviderResolver();
} else {
// stay null if no resolver is defined
state.providerResolver(resolver);
}
List> resolvers;
try {
resolvers = resolver.getValidationProviders();
} catch (RuntimeException re) {
throw new ValidationException(
"Unable to get available provider resolvers.", re);
}
for (ValidationProvider> provider : resolvers) {
// GWT validation only support exact matches.
if (validationProviderClass.equals(provider.getClass())) {
@SuppressWarnings("unchecked")
ValidationProvider specificProvider = (ValidationProvider) provider;
return specificProvider.createSpecializedConfiguration(state);
}
}
throw new ValidationException("Unable to find provider: "
+ validationProviderClass);
}
/**
* Optionally define the provider resolver implementation used. If not
* defined, use the default ValidationProviderResolver
*
* @param resolver ValidationProviderResolver implementation used
*
* @return self
*/
public ProviderSpecificBootstrap providerResolver(
ValidationProviderResolver resolver) {
this.resolver = resolver;
return this;
}
}
/**
* Build and return a ValidatorFactory
instance based on the
* default Bean Validation provider.
*
* The provider list is resolved using the default validation provider
* resolver logic.
*
* The code is semantically equivalent to
* Validation.byDefaultProvider().configure().buildValidatorFactory()
*
* @return ValidatorFactory
instance.
*
* @throws ValidationException if the ValidatorFactory cannot be built
*/
public static ValidatorFactory buildDefaultValidatorFactory() {
return byDefaultProvider().configure().buildValidatorFactory();
}
/**
* Build a Configuration
. The provider list is resolved using the
* strategy provided to the bootstrap state.
*
*
* Configuration<?> configuration = Validation
* .byDefaultProvider()
* .providerResolver( new MyResolverStrategy() )
* .configure();
* ValidatorFactory factory = configuration.buildValidatorFactory();
*
*
* The first available provider will be returned.
*
* @return instance building a generic Configuration
complaint
* with the bootstrap state provided.
*/
public static GenericBootstrap byDefaultProvider() {
return new GenericBootstrapImpl();
}
/**
* Build a Configuration
for a particular provider
* implementation. Optionally overrides the provider resolution strategy used
* to determine the provider.
*
* Used by applications targeting a specific provider programmatically.
*
*
*
* ACMEConfiguration configuration =
* Validation.byProvider(ACMEProvider.class)
* .providerResolver( new MyResolverStrategy() )
* .configure();
*
* , where ACMEConfiguration
is the Configuration
* sub interface uniquely identifying the ACME Bean Validation provider. and
* ACMEProvider
is the ValidationProvider
* implementation of the ACME provider.
*
* @param providerType the ValidationProvider
implementation type
*
* @return instance building a provider specific Configuration
* sub interface implementation.
*/
public static ,U extends ValidationProvider>
ProviderSpecificBootstrap byProvider(Class providerType) {
return new ProviderSpecificBootstrapImpl(providerType);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy