Coverage Report - org.apache.tapestry5.internal.services.AssetObjectProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
AssetObjectProvider
100%
10/10
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.Asset;
 18  
 import org.apache.tapestry5.annotations.Path;
 19  
 import org.apache.tapestry5.ioc.AnnotationProvider;
 20  
 import org.apache.tapestry5.ioc.ObjectLocator;
 21  
 import org.apache.tapestry5.ioc.ObjectProvider;
 22  
 import org.apache.tapestry5.ioc.services.Builtin;
 23  
 import org.apache.tapestry5.ioc.services.SymbolSource;
 24  
 import org.apache.tapestry5.ioc.services.TypeCoercer;
 25  
 import org.apache.tapestry5.services.AssetSource;
 26  
 import org.apache.tapestry5.services.Core;
 27  
 
 28  
 /**
 29  
  * Exposes assets (in the current locale). The Inject annotation must be supplemented by a {@link Path} annotation, to
 30  
  * identify what asset to be injected.
 31  
  */
 32  
 public class AssetObjectProvider implements ObjectProvider
 33  
 {
 34  
     private final AssetSource source;
 35  
 
 36  
     private final TypeCoercer typeCoercer;
 37  
 
 38  
     private final SymbolSource symbolSource;
 39  
 
 40  
     public AssetObjectProvider(@Core AssetSource source,
 41  
 
 42  
                                @Builtin TypeCoercer typeCoercer,
 43  
 
 44  
                                @Builtin SymbolSource symbolSource)
 45  64
     {
 46  64
         this.source = source;
 47  64
         this.typeCoercer = typeCoercer;
 48  64
         this.symbolSource = symbolSource;
 49  64
     }
 50  
 
 51  
     /**
 52  
      * Provides the asset. If the expression does not identify an asset domain, with a prefix, it is assumed to be a
 53  
      * path on the classpath, relative to the root of the classpath.
 54  
      *
 55  
      * @param objectType the type of object (which must be Object or Asset)
 56  
      * @param locator    not used
 57  
      */
 58  
     public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator)
 59  
     {
 60  23165
         Path path = annotationProvider.getAnnotation(Path.class);
 61  
 
 62  23165
         if (path == null) return null;
 63  
 
 64  60
         String expanded = symbolSource.expandSymbols(path.value());
 65  
 
 66  60
         Asset asset = source.getAsset(null, expanded, null);
 67  
 
 68  60
         return typeCoercer.coerce(asset, objectType);
 69  
     }
 70  
 }