|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
InjectComponentWorker.java | 100% | 100% | 100% | 100% |
|
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.enhance; | |
16 | ||
17 | import java.util.Iterator; | |
18 | ||
19 | import org.apache.hivemind.ErrorLog; | |
20 | import org.apache.hivemind.service.ClassFabUtils; | |
21 | import org.apache.hivemind.util.Defense; | |
22 | import org.apache.tapestry.IComponent; | |
23 | import org.apache.tapestry.spec.IComponentSpecification; | |
24 | import org.apache.tapestry.spec.IContainedComponent; | |
25 | ||
26 | /** | |
27 | * Injects components for which the property attribute of the <component> element was | |
28 | * specified. This makes it easier to reference a particular component from Java code. | |
29 | * | |
30 | * @author Howard M. Lewis Ship | |
31 | * @since 4.0 | |
32 | */ | |
33 | public class InjectComponentWorker implements EnhancementWorker | |
34 | { | |
35 | private ErrorLog _errorLog; | |
36 | ||
37 | 421 | public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
38 | { | |
39 | 421 | Iterator i = spec.getComponentIds().iterator(); |
40 | ||
41 | 421 | while (i.hasNext()) |
42 | { | |
43 | 259 | String id = (String) i.next(); |
44 | ||
45 | 259 | IContainedComponent cc = spec.getComponent(id); |
46 | ||
47 | 259 | String propertyName = cc.getPropertyName(); |
48 | ||
49 | 259 | if (propertyName != null) |
50 | { | |
51 | 2 | try |
52 | { | |
53 | 2 | injectComponent(op, id, propertyName); |
54 | } | |
55 | catch (Exception ex) | |
56 | { | |
57 | 1 | _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op |
58 | .getBaseClass(), ex), cc.getLocation(), ex); | |
59 | } | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
64 | 2 | public void injectComponent(EnhancementOperation op, String componentId, String propertyName) |
65 | { | |
66 | 2 | Defense.notNull(op, "op"); |
67 | 2 | Defense.notNull(componentId, "componentId"); |
68 | 2 | Defense.notNull(propertyName, "propertyName"); |
69 | ||
70 | 2 | Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null); |
71 | ||
72 | 2 | op.claimProperty(propertyName); |
73 | ||
74 | 1 | String fieldName = "_$" + propertyName; |
75 | ||
76 | 1 | op.addField(fieldName, propertyType); |
77 | ||
78 | 1 | EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName, propertyType); |
79 | ||
80 | // I.e. _$fred = (IComponent) getComponent("fred"); | |
81 | ||
82 | 1 | String code = fieldName + " = (" + ClassFabUtils.getJavaClassName(propertyType) |
83 | + ") getComponent(\"" + componentId + "\");"; | |
84 | ||
85 | 1 | op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code); |
86 | } | |
87 | ||
88 | 42 | public void setErrorLog(ErrorLog errorLog) |
89 | { | |
90 | 42 | _errorLog = errorLog; |
91 | } | |
92 | } |
|