Coverage Report - org.apache.tapestry5.internal.services.AssetInjectionProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
AssetInjectionProvider
100%
14/14
100%
2/2
0
 
 1  
 // Copyright 2007, 2008 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.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  
  * Performs injection of assets, based on the presence of the {@link Path} annotation. This is more useful than the
 30  
  * general {@link AssetObjectProvider}, becase relative assets are supported.
 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  
 }