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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data;
  5. import java.io.Serializable;
  6. import com.vaadin.terminal.ErrorMessage;
  7. import com.vaadin.terminal.PaintException;
  8. import com.vaadin.terminal.PaintTarget;
  9. /**
  10. * Object validator interface. Implementors of this class can be added to any
  11. * {@link com.vaadin.data.Validatable} object to verify its value. The
  12. * <code>Validatable#isValid(Object)</code> iterates all registered
  13. * <code>Validator</code>s, calling their {@link #validate(Object)} methods.
  14. * <code>validate(Object)</code> should throw the
  15. * {@link Validator.InvalidValueException} if the given value is not valid by
  16. * its standards.
  17. *
  18. * Validators should not have side effects on other objects as they can be
  19. * called from Paintable.paint().
  20. *
  21. * @author IT Mill Ltd.
  22. * @version
  23. * @VERSION@
  24. * @since 3.0
  25. */
  26. public interface Validator extends Serializable {
  27. /**
  28. * Checks the given value against this validator. If the value is valid this
  29. * method should do nothing, and if it's not valid, it should throw
  30. * <code>Validator.InvalidValueException</code>
  31. *
  32. * @param value
  33. * the value to check
  34. * @throws Validator.InvalidValueException
  35. * if the value is not valid
  36. */
  37. public void validate(Object value) throws Validator.InvalidValueException;
  38. /**
  39. * Tests if the given value is valid.
  40. *
  41. * @param value
  42. * the value to check
  43. * @return <code>true</code> for valid value, otherwise <code>false</code>.
  44. */
  45. public boolean isValid(Object value);
  46. /**
  47. * Invalid value exception can be thrown by {@link Validator} when a given
  48. * value is not valid.
  49. *
  50. * @author IT Mill Ltd.
  51. * @version
  52. * @VERSION@
  53. * @since 3.0
  54. */
  55. @SuppressWarnings("serial")
  56. public class InvalidValueException extends RuntimeException implements
  57. ErrorMessage {
  58. /** Array of validation errors that are causing the problem. */
  59. private InvalidValueException[] causes = null;
  60. /**
  61. * Constructs a new <code>InvalidValueException</code> with the
  62. * specified detail message.
  63. *
  64. * @param message
  65. * The detail message of the problem.
  66. */
  67. public InvalidValueException(String message) {
  68. this(message, new InvalidValueException[] {});
  69. }
  70. /**
  71. * Constructs a new <code>InvalidValueException</code> with a set of
  72. * causing validation exceptions. The error message contains first the
  73. * given message and then a list of validation errors in the given
  74. * validatables.
  75. *
  76. * @param message
  77. * The detail message of the problem.
  78. * @param causes
  79. * Array of validatables whos invalidities are possiblity
  80. * causing the invalidity.
  81. */
  82. public InvalidValueException(String message,
  83. InvalidValueException[] causes) {
  84. super(message);
  85. if (causes == null) {
  86. throw new NullPointerException(
  87. "Possible causes array must not be null");
  88. }
  89. this.causes = causes;
  90. }
  91. /**
  92. * See if the error message doesn't paint anything visible.
  93. *
  94. * @return True iff the paint method does not paint anything visible.
  95. */
  96. public boolean isInvisible() {
  97. String msg = getMessage();
  98. if (msg != null && msg.length() > 0) {
  99. return false;
  100. }
  101. if (causes != null) {
  102. for (int i = 0; i < causes.length; i++) {
  103. if (!causes[i].isInvisible()) {
  104. return false;
  105. }
  106. }
  107. }
  108. return true;
  109. }
  110. public final int getErrorLevel() {
  111. return ErrorMessage.ERROR;
  112. }
  113. public void paint(PaintTarget target) throws PaintException {
  114. target.startTag("error");
  115. target.addAttribute("level", "error");
  116. // Error message
  117. final String message = getLocalizedMessage();
  118. if (message != null) {
  119. target.addText(message);
  120. }
  121. // Paint all the causes
  122. for (int i = 0; i < causes.length; i++) {
  123. causes[i].paint(target);
  124. }
  125. target.endTag("error");
  126. }
  127. /* Documented in super interface */
  128. public void addListener(RepaintRequestListener listener) {
  129. }
  130. /* Documented in super interface */
  131. public void removeListener(RepaintRequestListener listener) {
  132. }
  133. /* Documented in super interface */
  134. public void requestRepaint() {
  135. }
  136. /* Documented in super interface */
  137. public void requestRepaintRequests() {
  138. }
  139. public String getDebugId() {
  140. return null;
  141. }
  142. public void setDebugId(String id) {
  143. throw new UnsupportedOperationException(
  144. "Setting testing id for this Paintable is not implemented");
  145. }
  146. }
  147. @SuppressWarnings("serial")
  148. public class EmptyValueException extends Validator.InvalidValueException {
  149. public EmptyValueException(String message) {
  150. super(message);
  151. }
  152. }
  153. }