|
|||||||||||||||||||
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 | |||||||||||||||
InjectAssetWorker.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.ApplicationRuntimeException; | |
20 | import org.apache.hivemind.ErrorLog; | |
21 | import org.apache.hivemind.util.Defense; | |
22 | import org.apache.tapestry.IAsset; | |
23 | import org.apache.tapestry.IComponent; | |
24 | import org.apache.tapestry.spec.IAssetSpecification; | |
25 | import org.apache.tapestry.spec.IComponentSpecification; | |
26 | ||
27 | /** | |
28 | * Injects assets as component properties. | |
29 | * | |
30 | * @author Howard M. Lewis Ship | |
31 | * @since 4.0 | |
32 | */ | |
33 | public class InjectAssetWorker implements EnhancementWorker | |
34 | { | |
35 | private ErrorLog _errorLog; | |
36 | ||
37 | 421 | public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
38 | { | |
39 | 421 | Iterator i = spec.getAssetNames().iterator(); |
40 | 421 | while (i.hasNext()) |
41 | { | |
42 | 39 | String name = (String) i.next(); |
43 | ||
44 | 39 | IAssetSpecification as = spec.getAsset(name); |
45 | ||
46 | 39 | String propertyName = as.getPropertyName(); |
47 | ||
48 | 39 | if (propertyName != null) |
49 | { | |
50 | 2 | try |
51 | { | |
52 | 2 | injectAsset(op, name, propertyName); |
53 | } | |
54 | catch (Exception ex) | |
55 | { | |
56 | 1 | _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op |
57 | .getBaseClass(), ex), as.getLocation(), ex); | |
58 | } | |
59 | } | |
60 | } | |
61 | } | |
62 | ||
63 | 3 | public void injectAsset(EnhancementOperation op, String assetName, String propertyName) |
64 | { | |
65 | 3 | Defense.notNull(op, "op"); |
66 | 3 | Defense.notNull(assetName, "assetName"); |
67 | 3 | Defense.notNull(propertyName, "propertyName"); |
68 | ||
69 | 3 | Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null); |
70 | ||
71 | 3 | op.claimProperty(propertyName); |
72 | ||
73 | 2 | if (!propertyType.isAssignableFrom(IAsset.class)) |
74 | 1 | throw new ApplicationRuntimeException(EnhanceMessages.incompatiblePropertyType( |
75 | propertyName, | |
76 | propertyType, | |
77 | IAsset.class)); | |
78 | ||
79 | 1 | String fieldName = "_$" + propertyName; |
80 | ||
81 | 1 | op.addField(fieldName, propertyType); |
82 | ||
83 | 1 | EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName, propertyType); |
84 | ||
85 | // i.e. _$fred = getAsset("barney"); | |
86 | ||
87 | 1 | String code = fieldName + " = getAsset(\"" + assetName + "\");"; |
88 | ||
89 | 1 | op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code); |
90 | } | |
91 | ||
92 | 42 | public void setErrorLog(ErrorLog errorLog) |
93 | { | |
94 | 42 | _errorLog = errorLog; |
95 | } | |
96 | } |
|