1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.annotations; |
16 |
|
|
17 |
|
import java.lang.reflect.Method; |
18 |
|
|
19 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
20 |
|
import org.apache.hivemind.HiveMind; |
21 |
|
import org.apache.hivemind.Location; |
22 |
|
import org.apache.tapestry.enhance.EnhancementOperation; |
23 |
|
import org.apache.tapestry.spec.BindingSpecification; |
24 |
|
import org.apache.tapestry.spec.BindingType; |
25 |
|
import org.apache.tapestry.spec.ContainedComponent; |
26 |
|
import org.apache.tapestry.spec.IBindingSpecification; |
27 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
28 |
|
import org.apache.tapestry.spec.IContainedComponent; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
12 |
public class ComponentAnnotationWorker implements MethodAnnotationEnhancementWorker |
39 |
|
{ |
40 |
|
|
41 |
|
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec, |
42 |
|
Method method, Location location) |
43 |
|
{ |
44 |
9 |
Component component = method.getAnnotation(Component.class); |
45 |
|
|
46 |
9 |
String propertyName = AnnotationUtils.getPropertyName(method); |
47 |
9 |
String type = component.type(); |
48 |
9 |
String copyOf = component.copyOf(); |
49 |
9 |
boolean hasCopyOf = HiveMind.isNonBlank(copyOf); |
50 |
|
|
51 |
9 |
if (hasCopyOf) |
52 |
|
{ |
53 |
2 |
if (HiveMind.isNonBlank(type)) |
54 |
1 |
throw new ApplicationRuntimeException(AnnotationMessages.bothTypeAndCopyOf(propertyName)); |
55 |
1 |
type = null; |
56 |
|
} |
57 |
|
else |
58 |
|
{ |
59 |
7 |
if (type.equals("")) |
60 |
|
{ |
61 |
1 |
Class retTypeClazz = method.getReturnType(); |
62 |
1 |
type = resolveComponentType(retTypeClazz); |
63 |
|
} |
64 |
7 |
copyOf = null; |
65 |
|
} |
66 |
|
|
67 |
8 |
IContainedComponent cc = new ContainedComponent(); |
68 |
|
|
69 |
8 |
cc.setInheritInformalParameters(component.inheritInformalParameters()); |
70 |
8 |
cc.setType(type); |
71 |
8 |
cc.setCopyOf(copyOf); |
72 |
8 |
cc.setPropertyName(propertyName); |
73 |
8 |
cc.setLocation(location); |
74 |
|
|
75 |
14 |
for (String binding : component.bindings()) |
76 |
|
{ |
77 |
6 |
addBinding(cc, binding, location); |
78 |
|
} |
79 |
|
|
80 |
8 |
String id = component.id(); |
81 |
|
|
82 |
8 |
if (id.equals("")) |
83 |
6 |
id = propertyName; |
84 |
|
|
85 |
8 |
spec.addComponent(id, cc); |
86 |
|
|
87 |
8 |
if (hasCopyOf) |
88 |
|
{ |
89 |
1 |
IContainedComponent source = spec.getComponent(copyOf); |
90 |
1 |
if (source != null) |
91 |
1 |
AnnotationUtils.copyBindings(source, cc); |
92 |
|
} |
93 |
8 |
} |
94 |
|
|
95 |
|
protected String resolveComponentType(Class retTypeClass) |
96 |
|
{ |
97 |
1 |
return retTypeClass.getSimpleName(); |
98 |
|
} |
99 |
|
|
100 |
|
void addBinding(IContainedComponent component, String binding, Location location) |
101 |
|
{ |
102 |
9 |
int equalsx = binding.indexOf('='); |
103 |
|
|
104 |
9 |
if (equalsx < 1) |
105 |
2 |
invalidBinding(binding); |
106 |
|
|
107 |
7 |
if (equalsx + 1 >= binding.length()) |
108 |
1 |
invalidBinding(binding); |
109 |
|
|
110 |
6 |
String name = binding.substring(0, equalsx).trim(); |
111 |
6 |
String value = binding.substring(equalsx + 1).trim(); |
112 |
|
|
113 |
6 |
IBindingSpecification bs = new BindingSpecification(); |
114 |
6 |
bs.setType(BindingType.PREFIXED); |
115 |
6 |
bs.setValue(value); |
116 |
6 |
bs.setLocation(location); |
117 |
|
|
118 |
6 |
component.setBinding(name, bs); |
119 |
6 |
} |
120 |
|
|
121 |
|
protected void invalidBinding(String binding) |
122 |
|
{ |
123 |
3 |
throw new ApplicationRuntimeException(AnnotationMessages.bindingWrongFormat(binding)); |
124 |
|
} |
125 |
|
} |