You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Validator.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data;
  5. import java.io.Serializable;
  6. import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
  7. /**
  8. * Interface that implements a method for validating if an {@link Object} is
  9. * valid or not.
  10. * <p>
  11. * Implementors of this class can be added to any
  12. * {@link com.vaadin.data.Validatable Validatable} implementor to verify its
  13. * value.
  14. * </p>
  15. * <p>
  16. * {@link #validate(Object)} can be used to check if a value is valid. An
  17. * {@link InvalidValueException} with an appropriate validation error message is
  18. * thrown if the value is not valid.
  19. * </p>
  20. * <p>
  21. * Validators must not have any side effects.
  22. * </p>
  23. * <p>
  24. * Since Vaadin 7, the method isValid(Object) does not exist in the interface -
  25. * {@link #validate(Object)} should be used instead, and the exception caught
  26. * where applicable. Concrete classes implementing {@link Validator} can still
  27. * internally implement and use isValid(Object) for convenience or to ease
  28. * migration from earlier Vaadin versions.
  29. * </p>
  30. *
  31. * @author Vaadin Ltd.
  32. * @since 3.0
  33. */
  34. public interface Validator extends Serializable {
  35. /**
  36. * Checks the given value against this validator. If the value is valid the
  37. * method does nothing. If the value is invalid, an
  38. * {@link InvalidValueException} is thrown.
  39. *
  40. * @param value
  41. * the value to check
  42. * @throws Validator.InvalidValueException
  43. * if the value is invalid
  44. */
  45. public void validate(Object value) throws Validator.InvalidValueException;
  46. /**
  47. * Exception that is thrown by a {@link Validator} when a value is invalid.
  48. *
  49. * <p>
  50. * The default implementation of InvalidValueException does not support HTML
  51. * in error messages. To enable HTML support, override
  52. * {@link #getHtmlMessage()} and use the subclass in validators.
  53. * </p>
  54. *
  55. * @author Vaadin Ltd.
  56. * @version
  57. * @VERSION@
  58. * @since 3.0
  59. */
  60. @SuppressWarnings("serial")
  61. public class InvalidValueException extends RuntimeException {
  62. /**
  63. * Array of one or more validation errors that are causing this
  64. * validation error.
  65. */
  66. private InvalidValueException[] causes = null;
  67. /**
  68. * Constructs a new {@code InvalidValueException} with the specified
  69. * message.
  70. *
  71. * @param message
  72. * The detail message of the problem.
  73. */
  74. public InvalidValueException(String message) {
  75. this(message, new InvalidValueException[] {});
  76. }
  77. /**
  78. * Constructs a new {@code InvalidValueException} with a set of causing
  79. * validation exceptions. The causing validation exceptions are included
  80. * when the exception is painted to the client.
  81. *
  82. * @param message
  83. * The detail message of the problem.
  84. * @param causes
  85. * One or more {@code InvalidValueException}s that caused
  86. * this exception.
  87. */
  88. public InvalidValueException(String message,
  89. InvalidValueException[] causes) {
  90. super(message);
  91. if (causes == null) {
  92. throw new NullPointerException(
  93. "Possible causes array must not be null");
  94. }
  95. this.causes = causes;
  96. }
  97. /**
  98. * Check if the error message should be hidden.
  99. *
  100. * An empty (null or "") message is invisible unless it contains nested
  101. * exceptions that are visible.
  102. *
  103. * @return true if the error message should be hidden, false otherwise
  104. */
  105. public boolean isInvisible() {
  106. String msg = getMessage();
  107. if (msg != null && msg.length() > 0) {
  108. return false;
  109. }
  110. if (causes != null) {
  111. for (int i = 0; i < causes.length; i++) {
  112. if (!causes[i].isInvisible()) {
  113. return false;
  114. }
  115. }
  116. }
  117. return true;
  118. }
  119. /**
  120. * Returns the message of the error in HTML.
  121. *
  122. * Note that this API may change in future versions.
  123. */
  124. public String getHtmlMessage() {
  125. return AbstractApplicationServlet
  126. .safeEscapeForHtml(getLocalizedMessage());
  127. }
  128. /**
  129. * Returns the {@code InvalidValueExceptions} that caused this
  130. * exception.
  131. *
  132. * @return An array containing the {@code InvalidValueExceptions} that
  133. * caused this exception. Returns an empty array if this
  134. * exception was not caused by other exceptions.
  135. */
  136. public InvalidValueException[] getCauses() {
  137. return causes;
  138. }
  139. }
  140. /**
  141. * A specific type of {@link InvalidValueException} that indicates that
  142. * validation failed because the value was empty. What empty means is up to
  143. * the thrower.
  144. *
  145. * @author Vaadin Ltd.
  146. * @version
  147. * @VERSION@
  148. * @since 5.3.0
  149. */
  150. @SuppressWarnings("serial")
  151. public class EmptyValueException extends Validator.InvalidValueException {
  152. public EmptyValueException(String message) {
  153. super(message);
  154. }
  155. }
  156. }