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.8KB

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