Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BeanValidator.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.data.validator;
  17. import java.io.Serializable;
  18. import java.util.Locale;
  19. import java.util.Set;
  20. import javax.validation.ConstraintViolation;
  21. import javax.validation.MessageInterpolator.Context;
  22. import javax.validation.Validation;
  23. import javax.validation.ValidatorFactory;
  24. import javax.validation.metadata.ConstraintDescriptor;
  25. import com.vaadin.data.BeanValidationBinder;
  26. import com.vaadin.v7.data.Validator;
  27. /**
  28. * Vaadin {@link Validator} using the JSR-303 (javax.validation)
  29. * annotation-based bean validation.
  30. *
  31. * The annotations of the fields of the beans are used to determine the
  32. * validation to perform.
  33. *
  34. * Note that a JSR-303 implementation (e.g. Hibernate Validator or Apache Bean
  35. * Validation - formerly agimatec validation) must be present on the project
  36. * classpath when using bean validation.
  37. *
  38. * @since 7.0
  39. *
  40. * @author Petri Hakala
  41. * @author Henri Sara
  42. *
  43. * @deprecated See {@link BeanValidationBinder} and
  44. * {@link com.vaadin.data.validator.BeanValidator}
  45. */
  46. @Deprecated
  47. public class BeanValidator implements Validator {
  48. private static final long serialVersionUID = 1L;
  49. private static ValidatorFactory factory;
  50. private transient javax.validation.Validator javaxBeanValidator;
  51. private String propertyName;
  52. private Class<?> beanClass;
  53. private Locale locale;
  54. /**
  55. * Simple implementation of a message interpolator context that returns
  56. * fixed values.
  57. */
  58. @Deprecated
  59. protected static class SimpleContext implements Context, Serializable {
  60. private final Object value;
  61. private final ConstraintDescriptor<?> descriptor;
  62. /**
  63. * Create a simple immutable message interpolator context.
  64. *
  65. * @param value
  66. * value being validated
  67. * @param descriptor
  68. * ConstraintDescriptor corresponding to the constraint being
  69. * validated
  70. */
  71. public SimpleContext(Object value, ConstraintDescriptor<?> descriptor) {
  72. this.value = value;
  73. this.descriptor = descriptor;
  74. }
  75. @Override
  76. public ConstraintDescriptor<?> getConstraintDescriptor() {
  77. return descriptor;
  78. }
  79. @Override
  80. public Object getValidatedValue() {
  81. return value;
  82. }
  83. }
  84. /**
  85. * Creates a Vaadin {@link Validator} utilizing JSR-303 bean validation.
  86. *
  87. * @param beanClass
  88. * bean class based on which the validation should be performed
  89. * @param propertyName
  90. * property to validate
  91. */
  92. public BeanValidator(Class<?> beanClass, String propertyName) {
  93. this.beanClass = beanClass;
  94. this.propertyName = propertyName;
  95. locale = Locale.getDefault();
  96. }
  97. /*
  98. * (non-Javadoc)
  99. *
  100. * @see com.vaadin.data.Validator#validate(java.lang.Object)
  101. */
  102. @Override
  103. public void validate(final Object value) throws InvalidValueException {
  104. Set<?> violations = getJavaxBeanValidator().validateValue(beanClass,
  105. propertyName, value);
  106. if (!violations.isEmpty()) {
  107. InvalidValueException[] causes = new InvalidValueException[violations
  108. .size()];
  109. int i = 0;
  110. for (Object v : violations) {
  111. final ConstraintViolation<?> violation = (ConstraintViolation<?>) v;
  112. String msg = getJavaxBeanValidatorFactory()
  113. .getMessageInterpolator()
  114. .interpolate(violation.getMessageTemplate(),
  115. new SimpleContext(value,
  116. violation.getConstraintDescriptor()),
  117. locale);
  118. causes[i] = new InvalidValueException(msg);
  119. ++i;
  120. }
  121. throw new InvalidValueException(null, causes);
  122. }
  123. }
  124. /**
  125. * Sets the locale used for validation error messages.
  126. *
  127. * Revalidation is not automatically triggered by setting the locale.
  128. *
  129. * @param locale
  130. */
  131. public void setLocale(Locale locale) {
  132. this.locale = locale;
  133. }
  134. /**
  135. * Gets the locale used for validation error messages.
  136. *
  137. * @return locale used for validation
  138. */
  139. public Locale getLocale() {
  140. return locale;
  141. }
  142. /**
  143. * Returns the underlying JSR-303 bean validator factory used. A factory is
  144. * created using {@link Validation} if necessary.
  145. *
  146. * @return {@link ValidatorFactory} to use
  147. */
  148. protected static ValidatorFactory getJavaxBeanValidatorFactory() {
  149. if (factory == null) {
  150. factory = Validation.buildDefaultValidatorFactory();
  151. }
  152. return factory;
  153. }
  154. /**
  155. * Returns a shared Validator instance to use. An instance is created using
  156. * the validator factory if necessary and thereafter reused by the
  157. * {@link BeanValidator} instance.
  158. *
  159. * @return the JSR-303 {@link javax.validation.Validator} to use
  160. */
  161. protected javax.validation.Validator getJavaxBeanValidator() {
  162. if (javaxBeanValidator == null) {
  163. javaxBeanValidator = getJavaxBeanValidatorFactory().getValidator();
  164. }
  165. return javaxBeanValidator;
  166. }
  167. }