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 static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
18:
19: import java.util.List;
20: import java.util.Locale;
21:
22: import org.apache.tapestry.Field;
23: import org.apache.tapestry.FieldValidator;
24: import org.apache.tapestry.ioc.AnnotationProvider;
25: import org.apache.tapestry.ioc.Messages;
26: import org.apache.tapestry.services.FieldValidatorDefaultSource;
27: import org.apache.tapestry.services.FieldValidatorSource;
28: import org.apache.tapestry.services.ValidationConstraintGenerator;
29:
30: public class FieldValidatorDefaultSourceImpl implements
31: FieldValidatorDefaultSource {
32: private final ValidationConstraintGenerator _validationConstraintGenerator;
33:
34: private final FieldValidatorSource _fieldValidatorSource;
35:
36: public FieldValidatorDefaultSourceImpl(
37: ValidationConstraintGenerator validationConstraintGenerator,
38: FieldValidatorSource fieldValidatorSource) {
39: _validationConstraintGenerator = validationConstraintGenerator;
40: _fieldValidatorSource = fieldValidatorSource;
41: }
42:
43: public FieldValidator createDefaultValidator(Field field,
44: String overrideId, Messages overrideMessages,
45: Locale locale, Class propertyType,
46: AnnotationProvider propertyAnnotations) {
47: List<FieldValidator> validators = newList();
48:
49: for (String constraint : _validationConstraintGenerator
50: .buildConstraints(propertyType, propertyAnnotations)) {
51: int equalsx = constraint.indexOf('=');
52:
53: String validatorType = equalsx > 0 ? constraint.substring(
54: 0, equalsx) : constraint;
55: String constraintValue = equalsx > 0 ? constraint
56: .substring(equalsx + 1) : null;
57:
58: FieldValidator validator = _fieldValidatorSource
59: .createValidator(field, validatorType,
60: constraintValue, overrideId,
61: overrideMessages, locale);
62:
63: validators.add(validator);
64: }
65:
66: return validators.size() == 1 ? validators.get(0)
67: : new CompositeFieldValidator(validators);
68: }
69: }
|