01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.internal.services;
16:
17: import static java.lang.String.format;
18:
19: import org.apache.tapestry.annotations.Path;
20: import org.apache.tapestry.ioc.ObjectLocator;
21: import org.apache.tapestry.ioc.services.SymbolSource;
22: import org.apache.tapestry.model.MutableComponentModel;
23: import org.apache.tapestry.services.AssetSource;
24: import org.apache.tapestry.services.ClassTransformation;
25: import org.apache.tapestry.services.InjectionProvider;
26:
27: /**
28: * Performs injection of assets, based on the presence of the {@link Path} annotation. This is more
29: * useful than the general {@link AssetObjectProvider}, becase relative assets are supported.
30: */
31: public class AssetInjectionProvider implements InjectionProvider {
32: private final SymbolSource _symbolSource;
33:
34: private final AssetSource _assetSource;
35:
36: public AssetInjectionProvider(SymbolSource symbolSource,
37: AssetSource assetSource) {
38: _symbolSource = symbolSource;
39: _assetSource = assetSource;
40: }
41:
42: public boolean provideInjection(String fieldName, String fieldType,
43: ObjectLocator locator, ClassTransformation transformation,
44: MutableComponentModel componentModel) {
45: Path path = transformation.getFieldAnnotation(fieldName,
46: Path.class);
47:
48: if (path == null)
49: return false;
50:
51: String expanded = _symbolSource.expandSymbols(path.value());
52:
53: String sourceFieldName = transformation.addInjectedField(
54: AssetSource.class, "assetSource", _assetSource);
55: String resourcesFieldName = transformation
56: .getResourcesFieldName();
57:
58: String statement = format(
59: "%s = (%s) %s.findAsset(%s.getBaseResource(), \"%s\", %s.getLocale());",
60: fieldName, fieldType, sourceFieldName,
61: resourcesFieldName, expanded, resourcesFieldName);
62:
63: transformation.extendConstructor(statement);
64:
65: transformation.makeReadOnly(fieldName);
66:
67: return true;
68:
69: }
70:
71: }
|