<commit_before>// Copyright 2006, 2007, 2008 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry.internal.services;

import org.apache.tapestry.Binding;
import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.ioc.Location;
import static org.apache.tapestry.ioc.internal.util.Defense.notBlank;
import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
import org.apache.tapestry.ioc.internal.util.TapestryException;
import org.apache.tapestry.services.BindingFactory;
import org.apache.tapestry.services.BindingSource;

import java.util.Map;

public class BindingSourceImpl implements BindingSource
{
    private final Map<String, BindingFactory> _factories;

    public BindingSourceImpl(Map<String, BindingFactory> factories)
    {
        _factories = factories;
    }

    public Binding newBinding(String description, ComponentResources container,
                              String defaultPrefix, String expression)
    {
        return newBinding(description, container, container, defaultPrefix, expression, null);
    }

    public Binding newBinding(String description, ComponentResources container,
                              ComponentResources component, String defaultPrefix, String expression, Location location)
    {
        notBlank(description, "description");
        notNull(container, "container");
        notNull(component, "component");
        notBlank(defaultPrefix, "defaultPrefix");

        try
        {
            notBlank(expression, "expression");
        }
        catch (final Exception ex)
        {
            throw new TapestryException(ServicesMessages.emptyBinding(description), location, ex);
        }

        // Location might be null

        String subexpression = expression;
        int colonx = expression.indexOf(':');

        BindingFactory factory = null;

        if (colonx > 0)
        {
            String prefix = expression.substring(0, colonx);

            factory = _factories.get(prefix);
            if (factory != null)
                subexpression = expression.substring(colonx + 1);
        }

        if (factory == null)
            factory = _factories.get(defaultPrefix);

        // And if that's null, what then? We assume that the default prefix is a valid prefix,
        // or we'll get an NPE below and report it like any other error.

        try
        {
            return factory.newBinding(description, container, component, subexpression, location);<commit_msg>Replaced a nested exception with a simpler check.<commit_after>// Copyright 2006, 2007, 2008 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry.internal.services;

import org.apache.tapestry.Binding;
import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.ioc.Location;
import static org.apache.tapestry.ioc.internal.util.Defense.notBlank;
import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
import org.apache.tapestry.ioc.internal.util.TapestryException;
import org.apache.tapestry.ioc.internal.util.InternalUtils;
import org.apache.tapestry.services.BindingFactory;
import org.apache.tapestry.services.BindingSource;

import java.util.Map;

public class BindingSourceImpl implements BindingSource
{
    private final Map<String, BindingFactory> _factories;

    public BindingSourceImpl(Map<String, BindingFactory> factories)
    {
        _factories = factories;
    }

    public Binding newBinding(String description, ComponentResources container,
                              String defaultPrefix, String expression)
    {
        return newBinding(description, container, container, defaultPrefix, expression, null);
    }

    public Binding newBinding(String description, ComponentResources container,
                              ComponentResources component, String defaultPrefix, String expression, Location location)
    {
        notBlank(description, "description");
        notNull(container, "container");
        notNull(component, "component");
        notBlank(defaultPrefix, "defaultPrefix");

        if (InternalUtils.isBlank(expression))
            throw new TapestryException(ServicesMessages.emptyBinding(description), location, null);

        // Location might be null

        String subexpression = expression;
        int colonx = expression.indexOf(':');

        BindingFactory factory = null;

        if (colonx > 0)
        {
            String prefix = expression.substring(0, colonx);

            factory = _factories.get(prefix);
            if (factory != null)
                subexpression = expression.substring(colonx + 1);
        }

        if (factory == null)
            factory = _factories.get(defaultPrefix);

        // And if that's null, what then? We assume that the default prefix is a valid prefix,
        // or we'll get an NPE below and report it like any other error.

        try
        {
            return factory.newBinding(description, container, component, subexpression, location);
{'fraction_non_alphanumeric': 0.08469855832241154, 'fraction_numerical': 0.006225425950196592, 'mean_word_length': 2.9823874755381605, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6276818', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1468, 'n_words': 493}