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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data;
  5. import java.io.Serializable;
  6. import com.vaadin.data.Validator.InvalidValueException;
  7. import com.vaadin.terminal.ErrorMessage;
  8. import com.vaadin.terminal.PaintException;
  9. import com.vaadin.terminal.PaintTarget;
  10. /**
  11. * Interface that implements a method for validating if an {@link Object} is
  12. * valid or not.
  13. * <p>
  14. * Implementors of this class can be added to any
  15. * {@link com.vaadin.data.Validatable Validatable} implementor to verify its
  16. * value.
  17. * </p>
  18. * <p>
  19. * {@link #isValid(Object)} and {@link #validate(Object)} can be used to check
  20. * if a value is valid. {@link #isValid(Object)} and {@link #validate(Object)}
  21. * must use the same validation logic so that iff {@link #isValid(Object)}
  22. * returns false, {@link #validate(Object)} throws an
  23. * {@link InvalidValueException}.
  24. * </p>
  25. * <p>
  26. * Validators must not have any side effects.
  27. * </p>
  28. *
  29. * @author IT Mill Ltd.
  30. * @version
  31. * @VERSION@
  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. * Tests if the given value is valid. This method must be symmetric with
  48. * {@link #validate(Object)} so that {@link #validate(Object)} throws an
  49. * error iff this method returns false.
  50. *
  51. * @param value
  52. * the value to check
  53. * @return <code>true</code> if the value is valid, <code>false</code>
  54. * otherwise.
  55. */
  56. public boolean isValid(Object value);
  57. /**
  58. * Exception that is thrown by a {@link Validator} when a value is invalid.
  59. *
  60. * @author IT Mill Ltd.
  61. * @version
  62. * @VERSION@
  63. * @since 3.0
  64. */
  65. @SuppressWarnings("serial")
  66. public class InvalidValueException extends RuntimeException implements
  67. ErrorMessage {
  68. /**
  69. * Array of one or more validation errors that are causing this
  70. * validation error.
  71. */
  72. private InvalidValueException[] causes = null;
  73. /**
  74. * Constructs a new {@code InvalidValueException} with the specified
  75. * message.
  76. *
  77. * @param message
  78. * The detail message of the problem.
  79. */
  80. public InvalidValueException(String message) {
  81. this(message, new InvalidValueException[] {});
  82. }
  83. /**
  84. * Constructs a new {@code InvalidValueException} with a set of causing
  85. * validation exceptions. The causing validation exceptions are included
  86. * when the exception is painted to the client.
  87. *
  88. * @param message
  89. * The detail message of the problem.
  90. * @param causes
  91. * One or more {@code InvalidValueException}s that caused
  92. * this exception.
  93. */
  94. public InvalidValueException(String message,
  95. InvalidValueException[] causes) {
  96. super(message);
  97. if (causes == null) {
  98. throw new NullPointerException(
  99. "Possible causes array must not be null");
  100. }
  101. this.causes = causes;
  102. }
  103. /**
  104. * Check if the error message should be hidden.
  105. *
  106. * An empty (null or "") message is invisible unless it contains nested
  107. * exceptions that are visible.
  108. *
  109. * @return true if the error message should be hidden, false otherwise
  110. */
  111. public boolean isInvisible() {
  112. String msg = getMessage();
  113. if (msg != null && msg.length() > 0) {
  114. return false;
  115. }
  116. if (causes != null) {
  117. for (int i = 0; i < causes.length; i++) {
  118. if (!causes[i].isInvisible()) {
  119. return false;
  120. }
  121. }
  122. }
  123. return true;
  124. }
  125. /*
  126. * (non-Javadoc)
  127. *
  128. * @see com.vaadin.terminal.ErrorMessage#getErrorLevel()
  129. */
  130. public final int getErrorLevel() {
  131. return ErrorMessage.ERROR;
  132. }
  133. /*
  134. * (non-Javadoc)
  135. *
  136. * @see
  137. * com.vaadin.terminal.Paintable#paint(com.vaadin.terminal.PaintTarget)
  138. */
  139. public void paint(PaintTarget target) throws PaintException {
  140. target.startTag("error");
  141. target.addAttribute("level", "error");
  142. // Error message
  143. final String message = getLocalizedMessage();
  144. if (message != null) {
  145. target.addText(message);
  146. }
  147. // Paint all the causes
  148. for (int i = 0; i < causes.length; i++) {
  149. causes[i].paint(target);
  150. }
  151. target.endTag("error");
  152. }
  153. /*
  154. * (non-Javadoc)
  155. *
  156. * @see
  157. * com.vaadin.terminal.ErrorMessage#addListener(com.vaadin.terminal.
  158. * Paintable.RepaintRequestListener)
  159. */
  160. public void addListener(RepaintRequestListener listener) {
  161. }
  162. /*
  163. * (non-Javadoc)
  164. *
  165. * @see
  166. * com.vaadin.terminal.ErrorMessage#removeListener(com.vaadin.terminal
  167. * .Paintable.RepaintRequestListener)
  168. */
  169. public void removeListener(RepaintRequestListener listener) {
  170. }
  171. /*
  172. * (non-Javadoc)
  173. *
  174. * @see com.vaadin.terminal.ErrorMessage#requestRepaint()
  175. */
  176. public void requestRepaint() {
  177. }
  178. /*
  179. * (non-Javadoc)
  180. *
  181. * @see com.vaadin.terminal.Paintable#requestRepaintRequests()
  182. */
  183. public void requestRepaintRequests() {
  184. }
  185. /*
  186. * (non-Javadoc)
  187. *
  188. * @see com.vaadin.terminal.Paintable#getDebugId()
  189. */
  190. public String getDebugId() {
  191. return null;
  192. }
  193. /*
  194. * (non-Javadoc)
  195. *
  196. * @see com.vaadin.terminal.Paintable#setDebugId(java.lang.String)
  197. */
  198. public void setDebugId(String id) {
  199. throw new UnsupportedOperationException(
  200. "InvalidValueException cannot have a debug id");
  201. }
  202. /**
  203. * Returns the {@code InvalidValueExceptions} that caused this
  204. * exception.
  205. *
  206. * @return An array containing the {@code InvalidValueExceptions} that
  207. * caused this exception. Returns an empty array if this
  208. * exception was not caused by other exceptions.
  209. */
  210. public InvalidValueException[] getCauses() {
  211. return causes;
  212. }
  213. }
  214. /**
  215. * A specific type of {@link InvalidValueException} that indicates that
  216. * validation failed because the value was empty. What empty means is up to
  217. * the thrower.
  218. *
  219. * @author IT Mill Ltd.
  220. * @version
  221. * @VERSION@
  222. * @since 5.3.0
  223. */
  224. @SuppressWarnings("serial")
  225. public class EmptyValueException extends Validator.InvalidValueException {
  226. public EmptyValueException(String message) {
  227. super(message);
  228. }
  229. }
  230. }