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