    001// Copyright 2006-2013 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.internal.services;
    016
    017import org.apache.tapestry5.Binding;
    018import org.apache.tapestry5.ComponentResources;
    019import org.apache.tapestry5.ioc.Location;
    020import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    021import org.apache.tapestry5.ioc.internal.util.TapestryException;
    022import org.apache.tapestry5.ioc.util.ExceptionUtils;
    023import org.apache.tapestry5.services.BindingFactory;
    024import org.apache.tapestry5.services.BindingSource;
    025
    026import java.util.Map;
    027
    028public class BindingSourceImpl implements BindingSource
    029{
    030    private final Map<String, BindingFactory> factories;
    031
    032    private final StringInterner interner;
    033
    034    public BindingSourceImpl(Map<String, BindingFactory> factories, StringInterner interner)
    035    {
    036        this.factories = factories;
    037        this.interner = interner;
    038    }
    039
    040    public Binding newBinding(String description, ComponentResources container, String defaultPrefix, String expression)
    041    {
    042        return newBinding(description, container, container, defaultPrefix, expression, null);
    043    }
    044
    045    public Binding newBinding(String description, ComponentResources container, ComponentResources component,
    046                              String defaultPrefix, String expression, Location location)
    047    {
    048        assert InternalUtils.isNonBlank(description);
    049        assert container != null;
    050        assert InternalUtils.isNonBlank(defaultPrefix);
    051        assert component != null;
    052
    053        // TAP5-845: The expression may be the empty string. This is ok, if it's compatible with
    054        // the default prefix (the empty string is not a valid property expression, but is valid
    055        // as a literal string, perhaps as an informal parameter).
    056
    057        // Location might be null
    058
    059        String subexpression = expression;
    060        int colonx = expression.indexOf(':');
    061
    062        BindingFactory factory = null;
    063
    064        if (colonx > 0)
    065        {
    066            String prefix = expression.substring(0, colonx);
    067
    068            factory = factories.get(prefix);
    069            if (factory != null)
    070                subexpression = expression.substring(colonx + 1);
    071        }
    072
    073        if (factory == null)
    074            factory = factories.get(defaultPrefix);
    075
    076        // And if that's null, what then? We assume that the default prefix is a valid prefix,
    077        // or we'll get an NPE below and report it like any other error.
    078
    079        try
    080        {
    081            return factory.newBinding(interner.intern(description), container, component, subexpression, location);
    082        } catch (Exception ex)
    083        {
    084            throw new TapestryException(String.format("Could not convert '%s' into a component parameter binding: %s", expression, ExceptionUtils.toMessage(ex)), location, ex);
    085        }
    086    }
    087}