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
015 package org.apache.tapestry.annotations;
016
017 import java.lang.reflect.Method;
018
019 import org.apache.hivemind.ApplicationRuntimeException;
020 import org.apache.hivemind.Location;
021 import org.apache.tapestry.enhance.EnhancementOperation;
022 import org.apache.tapestry.spec.BindingSpecification;
023 import org.apache.tapestry.spec.BindingType;
024 import org.apache.tapestry.spec.ContainedComponent;
025 import org.apache.tapestry.spec.IBindingSpecification;
026 import org.apache.tapestry.spec.IComponentSpecification;
027 import 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 */
038 public 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 String type = component.type();
048 if (type.equals(""))
049 {
050 Class retTypeClazz = method.getReturnType();
051 type = retTypeClazz.getSimpleName();
052 }
053
054 IContainedComponent cc = new ContainedComponent();
055
056 cc.setInheritInformalParameters(component.inheritInformalParameters());
057 cc.setType(type);
058 cc.setPropertyName(propertyName);
059 cc.setLocation(location);
060
061 for (String binding : component.bindings())
062 {
063 addBinding(cc, binding, location);
064 }
065
066 String id = component.id();
067
068 if (id.equals(""))
069 id = propertyName;
070
071 spec.addComponent(id, cc);
072 }
073
074 void addBinding(IContainedComponent component, String binding, Location location)
075 {
076 int equalsx = binding.indexOf('=');
077
078 if (equalsx < 1)
079 invalidBinding(binding);
080
081 if (equalsx + 1 >= binding.length())
082 invalidBinding(binding);
083
084 String name = binding.substring(0, equalsx).trim();
085 String value = binding.substring(equalsx + 1).trim();
086
087 IBindingSpecification bs = new BindingSpecification();
088 bs.setType(BindingType.PREFIXED);
089 bs.setValue(value);
090 bs.setLocation(location);
091
092 component.setBinding(name, bs);
093 }
094
095 private void invalidBinding(String binding)
096 {
097 throw new ApplicationRuntimeException(AnnotationMessages.bindingWrongFormat(binding));
098 }
099 }