01: // Copyright 2006 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.bindings;
16:
17: import org.apache.tapestry.Binding;
18: import org.apache.tapestry.ComponentResources;
19: import org.apache.tapestry.Field;
20: import org.apache.tapestry.FieldValidator;
21: import org.apache.tapestry.ioc.Location;
22: import org.apache.tapestry.ioc.internal.util.TapestryException;
23: import org.apache.tapestry.services.BindingFactory;
24: import org.apache.tapestry.services.FieldValidatorSource;
25:
26: /**
27: * Factory for bindings that provide a {@link FieldValidator} based on a validator specification.
28: * This binding factory is only useable with components that implement the {@link Field} interface.
29: */
30: public class ValidateBindingFactory implements BindingFactory {
31: private final FieldValidatorSource _fieldValidatorSource;
32:
33: public ValidateBindingFactory(
34: FieldValidatorSource fieldValidatorSource) {
35: _fieldValidatorSource = fieldValidatorSource;
36: }
37:
38: public Binding newBinding(String description,
39: ComponentResources container, ComponentResources component,
40: String expression, Location location) {
41: Object fieldAsObject = component.getComponent();
42:
43: if (!Field.class.isInstance(fieldAsObject))
44: throw new TapestryException(BindingsMessages
45: .validateBindingForFieldsOnly(component), location,
46: null);
47:
48: Field field = (Field) fieldAsObject;
49:
50: // The expression is a validator specification, such as "required,minLength=5".
51: // ValidatorBindingFactory is the odd man out becasuse it needs the binding component (the
52: // component whose parameter is to be bound) rather than the containing component, the way
53: // most factories work.
54:
55: FieldValidator validator = _fieldValidatorSource
56: .createValidators(field, expression);
57:
58: return new LiteralBinding(description, validator, location);
59: }
60:
61: }
|