    001// Licensed under the Apache License, Version 2.0 (the "License");
    002// you may not use this file except in compliance with the License.
    003// You may obtain a copy of the License at
    004//
    005//     http://www.apache.org/licenses/LICENSE-2.0
    006//
    007// Unless required by applicable law or agreed to in writing, software
    008// distributed under the License is distributed on an "AS IS" BASIS,
    009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    010// See the License for the specific language governing permissions and
    011// limitations under the License.
    012
    013package org.apache.tapestry5.internal.bindings;
    014
    015import org.apache.tapestry5.Binding;
    016import org.apache.tapestry5.ComponentResources;
    017import org.apache.tapestry5.beanmodel.PropertyConduit;
    018import org.apache.tapestry5.beanmodel.services.PropertyConduitSource;
    019import org.apache.tapestry5.commons.Location;
    020import org.apache.tapestry5.commons.internal.services.StringInterner;
    021import org.apache.tapestry5.services.BindingFactory;
    022
    023/**
    024 * Binding factory for reading and updating JavaBean properties.
    025 *
    026 * Expression are evaluated via a {@link PropertyConduit}, which is generated by {@link PropertyConduitSource} (which
    027 * therefore defines the expression language).
    028 */
    029public class PropBindingFactory implements BindingFactory
    030{
    031    private final PropertyConduitSource source;
    032
    033    private final StringInterner interner;
    034
    035    public PropBindingFactory(PropertyConduitSource propertyConduitSource, StringInterner interner)
    036    {
    037        source = propertyConduitSource;
    038        this.interner = interner;
    039    }
    040
    041    public Binding newBinding(String description, ComponentResources container,
    042                              ComponentResources component, String expression, Location location)
    043    {
    044        Object target = container.getComponent();
    045        Class targetClass = target.getClass();
    046
    047        PropertyConduit conduit = source.create(targetClass, expression);
    048
    049        String toString = interner.format("PropBinding[%s %s(%s)]", description, container
    050                .getCompleteId(), expression);
    051
    052        return new PropBinding(location, target, conduit, expression, toString);
    053    }
    054}