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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.data;
  5. import com.itmill.toolkit.terminal.ErrorMessage;
  6. import com.itmill.toolkit.terminal.PaintException;
  7. import com.itmill.toolkit.terminal.PaintTarget;
  8. /**
  9. * Object validator interface. Implementors of this class can be added to any
  10. * {@link com.itmill.toolkit.data.Validatable} object to verify its value. The
  11. * <code>Validatable#isValid(Object)</code> iterates all registered
  12. * <code>Validator</code>s, calling their {@link #validate(Object)} methods.
  13. * <code>validate(Object)</code> should throw the
  14. * {@link Validator.InvalidValueException} if the given value is not valid by
  15. * its standards.
  16. *
  17. * @author IT Mill Ltd.
  18. * @version
  19. * @VERSION@
  20. * @since 3.0
  21. */
  22. public interface Validator {
  23. /**
  24. * Checks the given value against this validator. If the value is valid this
  25. * method should do nothing, and if it's not valid, it should throw
  26. * <code>Validator.InvalidValueException</code>
  27. *
  28. * @param value
  29. * the value to check
  30. * @throws Validator.InvalidValueException
  31. * if the value is not valid
  32. */
  33. public void validate(Object value) throws Validator.InvalidValueException;
  34. /**
  35. * Tests if the given value is valid.
  36. *
  37. * @param value
  38. * the value to check
  39. * @return <code>true</code> for valid value, otherwise <code>false</code>.
  40. */
  41. public boolean isValid(Object value);
  42. /**
  43. * Adds the proposing functionality to a {@link Validator}. A
  44. * <code>Suggestive</code> validator can propose a valid value for the
  45. * object it is attached to validate. This way the {@link Validatable}
  46. * object may avoid situations where it contains a value that could lead to
  47. * a error.
  48. *
  49. * @author IT Mill Ltd.
  50. * @version
  51. * @VERSION@
  52. * @since 3.0
  53. */
  54. public interface Suggestive extends Validator {
  55. /**
  56. * Suggests another value that can be used instead of the proposedValue
  57. * if it is invalid. If it is valid in the opinion of this validator,
  58. * however, it is returned as is.
  59. *
  60. * @param proposedValue
  61. * Originally proposed value that could be invalid.
  62. * @return Suggested value that's not invalid against this validator
  63. */
  64. public Object suggestValidValue(Object proposedValue);
  65. }
  66. /**
  67. * Invalid value exception can be thrown by {@link Validator} when a given
  68. * value is not valid.
  69. *
  70. * @author IT Mill Ltd.
  71. * @version
  72. * @VERSION@
  73. * @since 3.0
  74. */
  75. public class InvalidValueException extends RuntimeException implements
  76. ErrorMessage {
  77. /**
  78. * Serial generated by eclipse.
  79. */
  80. private static final long serialVersionUID = 3689073941163422257L;
  81. /** Array of validation errors that are causing the problem. */
  82. private InvalidValueException[] causes = null;
  83. /**
  84. * Constructs a new <code>InvalidValueException</code> with the
  85. * specified detail message.
  86. *
  87. * @param message
  88. * The detail message of the problem.
  89. */
  90. public InvalidValueException(String message) {
  91. this(message, new InvalidValueException[] {});
  92. }
  93. /**
  94. * Constructs a new <code>InvalidValueException</code> with a set of
  95. * causing validation exceptions. The error message contains first the
  96. * given message and then a list of validation errors in the given
  97. * validatables.
  98. *
  99. * @param message
  100. * The detail message of the problem.
  101. * @param causes
  102. * Array of validatables whos invalidities are possiblity
  103. * causing the invalidity.
  104. */
  105. public InvalidValueException(String message,
  106. InvalidValueException[] causes) {
  107. super(message);
  108. if (causes == null) {
  109. throw new NullPointerException(
  110. "Possible causes array must not be null");
  111. }
  112. this.causes = causes;
  113. }
  114. public final int getErrorLevel() {
  115. return ErrorMessage.ERROR;
  116. }
  117. public void paint(PaintTarget target) throws PaintException {
  118. target.startTag("error");
  119. target.addAttribute("level", "error");
  120. // Error message
  121. final String message = getLocalizedMessage();
  122. if (message != null) {
  123. target.addText(message);
  124. }
  125. // Paint all the causes
  126. for (int i = 0; i < causes.length; i++) {
  127. causes[i].paint(target);
  128. }
  129. target.endTag("error");
  130. }
  131. /* Documented in super interface */
  132. public void addListener(RepaintRequestListener listener) {
  133. }
  134. /* Documented in super interface */
  135. public void removeListener(RepaintRequestListener listener) {
  136. }
  137. /* Documented in super interface */
  138. public void requestRepaint() {
  139. }
  140. /* Documented in super interface */
  141. public void requestRepaintRequests() {
  142. }
  143. }
  144. }