1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
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.util.Defense; |
21 |
| import org.apache.tapestry.IComponent; |
22 |
| import org.apache.tapestry.spec.IAssetSpecification; |
23 |
| import org.apache.tapestry.spec.IComponentSpecification; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public class InjectAssetWorker implements EnhancementWorker |
32 |
| { |
33 |
| private ErrorLog _errorLog; |
34 |
| |
35 |
414
| public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
|
36 |
| { |
37 |
414
| Iterator i = spec.getAssetNames().iterator();
|
38 |
414
| while (i.hasNext())
|
39 |
| { |
40 |
39
| String name = (String) i.next();
|
41 |
| |
42 |
39
| IAssetSpecification as = spec.getAsset(name);
|
43 |
| |
44 |
39
| String propertyName = as.getPropertyName();
|
45 |
| |
46 |
39
| if (propertyName != null)
|
47 |
| { |
48 |
2
| try
|
49 |
| { |
50 |
2
| injectAsset(op, name, propertyName);
|
51 |
| } |
52 |
| catch (Exception ex) |
53 |
| { |
54 |
1
| _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op
|
55 |
| .getBaseClass(), ex), as.getLocation(), ex); |
56 |
| } |
57 |
| } |
58 |
| } |
59 |
| } |
60 |
| |
61 |
2
| public void injectAsset(EnhancementOperation op, String assetName, String propertyName)
|
62 |
| { |
63 |
2
| Defense.notNull(op, "op");
|
64 |
2
| Defense.notNull(assetName, "assetName");
|
65 |
2
| Defense.notNull(propertyName, "propertyName");
|
66 |
| |
67 |
2
| Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
|
68 |
| |
69 |
| |
70 |
| |
71 |
2
| op.claimProperty(propertyName);
|
72 |
| |
73 |
1
| String fieldName = "_$" + propertyName;
|
74 |
| |
75 |
1
| op.addField(fieldName, propertyType);
|
76 |
| |
77 |
1
| EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName, propertyType);
|
78 |
| |
79 |
| |
80 |
| |
81 |
1
| String code = fieldName + " = getAsset(\"" + assetName + "\");";
|
82 |
| |
83 |
1
| op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code);
|
84 |
| } |
85 |
| |
86 |
41
| public void setErrorLog(ErrorLog errorLog)
|
87 |
| { |
88 |
41
| _errorLog = errorLog;
|
89 |
| } |
90 |
| } |