KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > annotations > ComponentAnnotationWorker


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.annotations;
16
17 import java.lang.reflect.Method JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.Location;
21 import org.apache.tapestry.enhance.EnhancementOperation;
22 import org.apache.tapestry.spec.BindingSpecification;
23 import org.apache.tapestry.spec.BindingType;
24 import org.apache.tapestry.spec.ContainedComponent;
25 import org.apache.tapestry.spec.IBindingSpecification;
26 import org.apache.tapestry.spec.IComponentSpecification;
27 import org.apache.tapestry.spec.IContainedComponent;
28
29 /**
30  * Adds a {@link org.apache.tapestry.spec.IContainedComponent} to the
31  * {@link org.apache.tapestry.spec.IComponentSpecification}.
32  *
33  * @author Howard Lewis Ship
34  * @since 4.0
35  * @see org.apache.tapestry.annotations.Component
36  * @see org.apache.tapestry.annotations.Binding
37  */

38 public class ComponentAnnotationWorker implements MethodAnnotationEnhancementWorker
39 {
40
41     public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
42             Method JavaDoc method, Location location)
43     {
44         Component component = method.getAnnotation(Component.class);
45
46         String JavaDoc propertyName = AnnotationUtils.getPropertyName(method);
47
48         IContainedComponent cc = new ContainedComponent();
49
50         cc.setInheritInformalParameters(component.inheritInformalParameters());
51         cc.setType(component.type());
52         cc.setPropertyName(propertyName);
53         cc.setLocation(location);
54
55         for (String JavaDoc binding : component.bindings())
56         {
57             addBinding(cc, binding, location);
58         }
59
60         String JavaDoc id = component.id();
61
62         if (id.equals(""))
63             id = propertyName;
64
65         spec.addComponent(id, cc);
66     }
67
68     void addBinding(IContainedComponent component, String JavaDoc binding, Location location)
69     {
70         int equalsx = binding.indexOf('=');
71
72         if (equalsx < 1)
73             invalidBinding(binding);
74
75         if (equalsx + 1 >= binding.length())
76             invalidBinding(binding);
77
78         String JavaDoc name = binding.substring(0, equalsx).trim();
79         String JavaDoc value = binding.substring(equalsx + 1).trim();
80
81         IBindingSpecification bs = new BindingSpecification();
82         bs.setType(BindingType.PREFIXED);
83         bs.setValue(value);
84         bs.setLocation(location);
85
86         component.setBinding(name, bs);
87     }
88
89     private void invalidBinding(String JavaDoc binding)
90     {
91         throw new ApplicationRuntimeException(AnnotationMessages.bindingWrongFormat(binding));
92     }
93 }
Popular Tags