blob: 41a32d9d8a6136f62b200a1457affd6574160f65 (
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.data;
import java.io.Serializable;
import com.vaadin.terminal.ErrorMessage;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
/**
* Interface that implements a method for validating if an {@link Object} is
* valid or not.
* <p>
* Implementors of this class can be added to any
* {@link com.vaadin.data.Validatable Validatable} implementor to verify its
* value.
* </p>
* <p>
* {@link #isValid(Object)} and {@link #validate(Object)} can be used to check
* if a value is valid. {@link #isValid(Object)} and {@link #validate(Object)}
* must use the same validation logic so that iff {@link #isValid(Object)}
* returns false, {@link #validate(Object)} throws an
* {@link InvalidValueException}.
* </p>
* <p>
* Validators must not have any side effects.
* </p>
* <p>
* Since Vaadin 7, the method isValid(Object) does not exist in the interface -
* {@link #validate(Object)} should be used instead, and the exception caught
* where applicable. Concrete classes implementing {@link Validator} can still
* internally implement and use isValid(Object) for convenience or to ease
* migration from earlier Vaadin versions.
* </p>
*
* @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 the
* method does nothing. If the value is invalid, an
* {@link InvalidValueException} is thrown.
*
* @param value
* the value to check
* @throws Validator.InvalidValueException
* if the value is invalid
*/
public void validate(Object value) throws Validator.InvalidValueException;
/**
* Exception that is thrown by a {@link Validator} when a value is invalid.
*
* <p>
* The default implementation of InvalidValueException does not support HTML
* in error messages. To enable HTML support, override
* {@link #getHtmlMessage()} and use the subclass in validators.
* </p>
*
* @author IT Mill Ltd.
* @version
* @VERSION@
* @since 3.0
*/
@SuppressWarnings("serial")
public class InvalidValueException extends RuntimeException implements
ErrorMessage {
/**
* Array of one or more validation errors that are causing this
* validation error.
*/
private InvalidValueException[] causes = null;
/**
* Constructs a new {@code InvalidValueException} with the specified
* message.
*
* @param message
* The detail message of the problem.
*/
public InvalidValueException(String message) {
this(message, new InvalidValueException[] {});
}
/**
* Constructs a new {@code InvalidValueException} with a set of causing
* validation exceptions. The causing validation exceptions are included
* when the exception is painted to the client.
*
* @param message
* The detail message of the problem.
* @param causes
* One or more {@code InvalidValueException}s that caused
* this exception.
*/
public InvalidValueException(String message,
InvalidValueException[] causes) {
super(message);
if (causes == null) {
throw new NullPointerException(
"Possible causes array must not be null");
}
this.causes = causes;
}
/**
* Check if the error message should be hidden.
*
* An empty (null or "") message is invisible unless it contains nested
* exceptions that are visible.
*
* @return true if the error message should be hidden, false otherwise
*/
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;
}
/*
* (non-Javadoc)
*
* @see com.vaadin.terminal.ErrorMessage#getErrorLevel()
*/
public final ErrorLevel getErrorLevel() {
return ErrorLevel.ERROR;
}
/*
* (non-Javadoc)
*
* @see
* com.vaadin.terminal.Paintable#paint(com.vaadin.terminal.PaintTarget)
*/
public void paint(PaintTarget target) throws PaintException {
target.startTag("error");
target.addAttribute("level", ErrorLevel.ERROR.getText());
// Error message
final String message = getHtmlMessage();
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");
}
/**
* Returns the message of the error in HTML.
*
* Note that this API may change in future versions.
*/
protected String getHtmlMessage() {
return AbstractApplicationServlet
.safeEscapeForHtml(getLocalizedMessage());
}
/*
* (non-Javadoc)
*
* @see
* com.vaadin.terminal.ErrorMessage#addListener(com.vaadin.terminal.
* Paintable.RepaintRequestListener)
*/
public void addListener(RepaintRequestListener listener) {
}
/*
* (non-Javadoc)
*
* @see
* com.vaadin.terminal.ErrorMessage#removeListener(com.vaadin.terminal
* .Paintable.RepaintRequestListener)
*/
public void removeListener(RepaintRequestListener listener) {
}
/*
* (non-Javadoc)
*
* @see com.vaadin.terminal.ErrorMessage#requestRepaint()
*/
public void requestRepaint() {
}
/*
* (non-Javadoc)
*
* @see com.vaadin.terminal.Paintable#requestRepaintRequests()
*/
public void requestRepaintRequests() {
}
/*
* (non-Javadoc)
*
* @see com.vaadin.terminal.Paintable#getDebugId()
*/
public String getDebugId() {
return null;
}
/*
* (non-Javadoc)
*
* @see com.vaadin.terminal.Paintable#setDebugId(java.lang.String)
*/
public void setDebugId(String id) {
throw new UnsupportedOperationException(
"InvalidValueException cannot have a debug id");
}
/**
* Returns the {@code InvalidValueExceptions} that caused this
* exception.
*
* @return An array containing the {@code InvalidValueExceptions} that
* caused this exception. Returns an empty array if this
* exception was not caused by other exceptions.
*/
public InvalidValueException[] getCauses() {
return causes;
}
}
/**
* A specific type of {@link InvalidValueException} that indicates that
* validation failed because the value was empty. What empty means is up to
* the thrower.
*
* @author IT Mill Ltd.
* @version
* @VERSION@
* @since 5.3.0
*/
@SuppressWarnings("serial")
public class EmptyValueException extends Validator.InvalidValueException {
public EmptyValueException(String message) {
super(message);
}
}
}
|