| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.internal.services; |
| 16 | |
|
| 17 | |
import org.apache.tapestry5.annotations.Path; |
| 18 | |
import org.apache.tapestry5.ioc.ObjectLocator; |
| 19 | |
import org.apache.tapestry5.ioc.Resource; |
| 20 | |
import org.apache.tapestry5.ioc.services.SymbolSource; |
| 21 | |
import org.apache.tapestry5.model.MutableComponentModel; |
| 22 | |
import org.apache.tapestry5.services.AssetSource; |
| 23 | |
import org.apache.tapestry5.services.ClassTransformation; |
| 24 | |
import org.apache.tapestry5.services.InjectionProvider; |
| 25 | |
|
| 26 | |
import static java.lang.String.format; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
public class AssetInjectionProvider implements InjectionProvider |
| 33 | |
{ |
| 34 | |
private final SymbolSource symbolSource; |
| 35 | |
|
| 36 | |
private final AssetSource assetSource; |
| 37 | |
|
| 38 | |
public AssetInjectionProvider(SymbolSource symbolSource, AssetSource assetSource) |
| 39 | 44 | { |
| 40 | 44 | this.symbolSource = symbolSource; |
| 41 | 44 | this.assetSource = assetSource; |
| 42 | 44 | } |
| 43 | |
|
| 44 | |
public boolean provideInjection(String fieldName, Class fieldType, ObjectLocator locator, |
| 45 | |
ClassTransformation transformation, MutableComponentModel componentModel) |
| 46 | |
{ |
| 47 | 836 | Path path = transformation.getFieldAnnotation(fieldName, Path.class); |
| 48 | |
|
| 49 | 836 | if (path == null) return false; |
| 50 | |
|
| 51 | 30 | String expanded = symbolSource.expandSymbols(path.value()); |
| 52 | |
|
| 53 | 30 | String sourceFieldName = transformation.addInjectedField(AssetSource.class, "assetSource", assetSource); |
| 54 | |
|
| 55 | 30 | String baseResourceFieldName = transformation.addInjectedField(Resource.class, "baseResource", |
| 56 | |
componentModel.getBaseResource()); |
| 57 | |
|
| 58 | 30 | String resourcesFieldName = transformation.getResourcesFieldName(); |
| 59 | |
|
| 60 | 30 | String statement = format("%s = (%s) %s.getAsset(%s, \"%s\", %s.getLocale());", fieldName, fieldType.getName(), |
| 61 | |
sourceFieldName, baseResourceFieldName, expanded, resourcesFieldName); |
| 62 | |
|
| 63 | 30 | transformation.extendConstructor(statement); |
| 64 | |
|
| 65 | 30 | transformation.makeReadOnly(fieldName); |
| 66 | |
|
| 67 | 30 | return true; |
| 68 | |
} |
| 69 | |
|
| 70 | |
} |