001// Copyright 2005 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.tapestry.annotations;
016
017import java.lang.reflect.Method;
018
019import org.apache.hivemind.ApplicationRuntimeException;
020import org.apache.hivemind.Location;
021import org.apache.tapestry.enhance.EnhancementOperation;
022import org.apache.tapestry.spec.BindingSpecification;
023import org.apache.tapestry.spec.BindingType;
024import org.apache.tapestry.spec.ContainedComponent;
025import org.apache.tapestry.spec.IBindingSpecification;
026import org.apache.tapestry.spec.IComponentSpecification;
027import org.apache.tapestry.spec.IContainedComponent;
028
029/**
030 * Adds a {@link org.apache.tapestry.spec.IContainedComponent} to the
031 * {@link org.apache.tapestry.spec.IComponentSpecification}.
032 * 
033 * @author Howard Lewis Ship
034 * @since 4.0
035 * @see org.apache.tapestry.annotations.Component
036 * @see org.apache.tapestry.annotations.Binding
037 */
038public class ComponentAnnotationWorker implements MethodAnnotationEnhancementWorker
039{
040
041    public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
042            Method method, Location location)
043    {
044        Component component = method.getAnnotation(Component.class);
045
046        String propertyName = AnnotationUtils.getPropertyName(method);
047
048        IContainedComponent cc = new ContainedComponent();
049
050        cc.setInheritInformalParameters(component.inheritInformalParameters());
051        cc.setType(component.type());
052        cc.setPropertyName(propertyName);
053        cc.setLocation(location);
054
055        for (String binding : component.bindings())
056        {
057            addBinding(cc, binding, location);
058        }
059
060        String id = component.id();
061
062        if (id.equals(""))
063            id = propertyName;
064
065        spec.addComponent(id, cc);
066    }
067
068    void addBinding(IContainedComponent component, String binding, Location location)
069    {
070        int equalsx = binding.indexOf('=');
071
072        if (equalsx < 1)
073            invalidBinding(binding);
074
075        if (equalsx + 1 >= binding.length())
076            invalidBinding(binding);
077
078        String name = binding.substring(0, equalsx).trim();
079        String value = binding.substring(equalsx + 1).trim();
080
081        IBindingSpecification bs = new BindingSpecification();
082        bs.setType(BindingType.PREFIXED);
083        bs.setValue(value);
084        bs.setLocation(location);
085
086        component.setBinding(name, bs);
087    }
088
089    private void invalidBinding(String binding)
090    {
091        throw new ApplicationRuntimeException(AnnotationMessages.bindingWrongFormat(binding));
092    }
093}