01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import org.apache.tapestry.Field;
18: import org.apache.tapestry.FieldValidator;
19: import org.apache.tapestry.MarkupWriter;
20: import org.apache.tapestry.PageRenderSupport;
21: import org.apache.tapestry.ValidationException;
22: import org.apache.tapestry.Validator;
23: import org.apache.tapestry.ioc.MessageFormatter;
24:
25: public class FieldValidatorImpl implements FieldValidator {
26: private final Field _field;
27:
28: private final Object _constraintValue;
29:
30: private final MessageFormatter _messageFormatter;
31:
32: private final Validator _validator;
33:
34: private final PageRenderSupport _pageRenderSupport;
35:
36: public FieldValidatorImpl(Field field, Object constraintValue,
37: MessageFormatter messageFormatter, Validator validator,
38: PageRenderSupport pageRenderSupport) {
39: _field = field;
40: _constraintValue = constraintValue;
41: _messageFormatter = messageFormatter;
42: _validator = validator;
43: _pageRenderSupport = pageRenderSupport;
44: }
45:
46: @SuppressWarnings("unchecked")
47: public void validate(Object value) throws ValidationException {
48: if (!_validator.invokeIfBlank() && isBlank(value))
49: return;
50:
51: if (value != null
52: && !_validator.getValueType().isInstance(value))
53: return;
54:
55: _validator.validate(_field, _constraintValue,
56: _messageFormatter, value);
57: }
58:
59: @SuppressWarnings("unchecked")
60: public void render(MarkupWriter writer) {
61: // TODO: Skip this step if the Form's clientValidatio parameter is false?
62:
63: _validator.render(_field, _constraintValue, _messageFormatter,
64: writer, _pageRenderSupport);
65: }
66:
67: private boolean isBlank(Object value) {
68: return value == null || value.equals("");
69: }
70:
71: }
|