|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover 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.tapestry.IComponent;
|
|
22 |
import org.apache.tapestry.spec.IComponentSpecification;
|
|
23 |
import org.apache.tapestry.spec.IContainedComponent;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* Injects components for which the property attribute of the <component> element was
|
|
27 |
* specified. This makes it easier to reference a particular component from Java code.
|
|
28 |
*
|
|
29 |
* @author Howard M. Lewis Ship
|
|
30 |
* @since 4.0
|
|
31 |
*/
|
|
32 |
public class InjectComponentWorker implements EnhancementWorker |
|
33 |
{ |
|
34 |
private ErrorLog _errorLog;
|
|
35 |
|
|
36 | 465 |
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
37 |
{ |
|
38 | 465 |
Iterator i = spec.getComponentIds().iterator(); |
39 |
|
|
40 | 465 |
while (i.hasNext())
|
41 |
{ |
|
42 | 296 |
String id = (String) i.next(); |
43 |
|
|
44 | 296 |
IContainedComponent cc = spec.getComponent(id); |
45 |
|
|
46 | 296 |
String propertyName = cc.getPropertyName(); |
47 |
|
|
48 | 296 |
if (propertyName != null) |
49 |
{ |
|
50 | 2 |
try
|
51 |
{ |
|
52 | 2 |
injectComponent(op, id, propertyName); |
53 |
} |
|
54 |
catch (Exception ex)
|
|
55 |
{ |
|
56 | 1 |
_errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op |
57 |
.getBaseClass(), ex), cc.getLocation(), ex); |
|
58 |
} |
|
59 |
} |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 | 2 |
private void injectComponent(EnhancementOperation op, String componentId, String propertyName) |
64 |
{ |
|
65 | 2 |
Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
|
66 |
|
|
67 | 2 |
op.claimProperty(propertyName); |
68 |
|
|
69 | 1 |
String fieldName = "_$" + propertyName;
|
70 |
|
|
71 | 1 |
op.addField(fieldName, propertyType); |
72 |
|
|
73 | 1 |
EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName, propertyType); |
74 |
|
|
75 |
// I.e. _$fred = (IComponent) getComponent("fred");
|
|
76 |
|
|
77 | 1 |
String code = fieldName + " = (" + ClassFabUtils.getJavaClassName(propertyType)
|
78 |
+ ") getComponent(\"" + componentId + "\");"; |
|
79 |
|
|
80 | 1 |
op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code);
|
81 |
} |
|
82 |
|
|
83 | 46 |
public void setErrorLog(ErrorLog errorLog) |
84 |
{ |
|
85 | 46 |
_errorLog = errorLog; |
86 |
} |
|
87 |
} |
|