Coverage Report - org.apache.tapestry.asset.AssetSourceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AssetSourceImpl
73% 
73% 
2.444
 
 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.asset;
 16  
 
 17  
 import org.apache.hivemind.Location;
 18  
 import org.apache.hivemind.Resource;
 19  
 import org.apache.hivemind.util.Defense;
 20  
 import org.apache.tapestry.IAsset;
 21  
 import org.apache.tapestry.spec.IComponentSpecification;
 22  
 
 23  
 import java.util.*;
 24  
 
 25  
 /**
 26  
  * Implementation of the {@link org.apache.tapestry.asset.AssetSource} service interface.
 27  
  * 
 28  
  */
 29  4
 public class AssetSourceImpl implements AssetSource
 30  
 {
 31  4
     private Map _assetFactoryByPrefix = new HashMap();
 32  
 
 33  
     private List _contributions;
 34  
 
 35  
     private AssetFactory _defaultAssetFactory;
 36  
 
 37  
     private AssetFactory _lookupAssetFactory;
 38  
 
 39  
     private AssetFactory _classpathAssetFactory;
 40  
 
 41  
     private AssetFactory _contextAssetFactory;
 42  
 
 43  
     public void initializeService()
 44  
     {
 45  2
         Iterator i = _contributions.iterator();
 46  4
         while (i.hasNext())
 47  
         {
 48  2
             AssetFactoryContribution c = (AssetFactoryContribution) i.next();
 49  
 
 50  2
             _assetFactoryByPrefix.put(c.getPrefix(), c.getFactory());
 51  2
         }
 52  2
     }
 53  
 
 54  
     public IAsset findAsset(Resource base, String path, Locale locale, Location location)
 55  
     {
 56  4
         return findAsset(base, null, path, locale, location);
 57  
     }
 58  
 
 59  
     public IAsset findAsset(Resource base, IComponentSpecification spec, String path, Locale locale, Location location)
 60  
     {
 61  4
         Defense.notNull(path, "path");
 62  4
         Defense.notNull(location, "location");
 63  
 
 64  4
         int colonx = path.indexOf(':');
 65  
         
 66  4
         String prefix = colonx > -1 ? path.substring(0, colonx) : null;
 67  4
         String truePath = colonx > -1 ? path.substring(colonx + 1) : path;
 68  
 
 69  4
         AssetFactory factory = null;
 70  
 
 71  4
         if (prefix != null) {
 72  
 
 73  3
             factory = (AssetFactory) _assetFactoryByPrefix.get(prefix);
 74  
         }
 75  
 
 76  
         // now we have to search
 77  
         
 78  4
         if (factory == null && prefix == null) {
 79  
             
 80  1
             factory = findAssetFactory(spec, base, truePath, locale);
 81  
         }
 82  
         
 83  
         // Unknown prefix is expected to happen when an external asset (using an established
 84  
         // prefix such as http:) is referenced.
 85  
 
 86  4
         if (factory == null)
 87  
         {
 88  1
             factory = _defaultAssetFactory;
 89  
 
 90  
             // Path is the full path, including the prefix (which is really the scheme
 91  
             // of the URL).
 92  
 
 93  1
             truePath = path;
 94  
         }
 95  
         
 96  4
         if (truePath.startsWith("/"))
 97  0
             return factory.createAbsoluteAsset(truePath, locale, location);
 98  
 
 99  
         // This can happen when a 3.0 DTD is read in
 100  
 
 101  4
         return factory.createAsset(base, spec, truePath, locale, location);
 102  
     }
 103  
 
 104  
     AssetFactory findAssetFactory(IComponentSpecification spec, Resource baseResource, String path, Locale locale)
 105  
     {
 106  
         // need to check these two core factories in order first
 107  
 
 108  1
         if (_classpathAssetFactory.assetExists(spec, baseResource, path, locale))
 109  1
             return _classpathAssetFactory;
 110  
 
 111  0
         if (_contextAssetFactory.assetExists(spec, baseResource, path, locale))
 112  0
             return _contextAssetFactory;
 113  
         
 114  0
         for (int i=0; i < _contributions.size(); i++) {
 115  
 
 116  0
             AssetFactoryContribution c = (AssetFactoryContribution)_contributions.get(i);
 117  
 
 118  0
             if (c.getFactory().assetExists(spec, baseResource, path, locale))
 119  0
                 return c.getFactory();
 120  
         }
 121  
 
 122  0
         return null;
 123  
     }
 124  
 
 125  
     /**
 126  
      * Factory used when the path has no prefix, and the type of asset must be inferred from the
 127  
      * type of resource.
 128  
      */
 129  
 
 130  
     public void setLookupAssetFactory(AssetFactory lookupAssetFactory)
 131  
     {
 132  0
         _lookupAssetFactory = lookupAssetFactory;
 133  0
     }
 134  
 
 135  
     /**
 136  
      * List of {@link org.apache.tapestry.asset.AssetFactoryContribution}.
 137  
      */
 138  
 
 139  
     public void setContributions(List contributions)
 140  
     {
 141  2
         _contributions = contributions;
 142  2
     }
 143  
 
 144  
     /**
 145  
      * Factory used when an unrecognized prefix (typically, an arbitrary URL's scheme) is provided.
 146  
      */
 147  
 
 148  
     public void setDefaultAssetFactory(AssetFactory defaultAssetFactory)
 149  
     {
 150  1
         _defaultAssetFactory = defaultAssetFactory;
 151  1
     }
 152  
 
 153  
     public void setClasspathAssetFactory(AssetFactory classpathAssetFactory)
 154  
     {
 155  1
         _classpathAssetFactory = classpathAssetFactory;
 156  1
     }
 157  
 
 158  
     public void setContextAssetFactory(AssetFactory contextAssetFactory)
 159  
     {
 160  0
         _contextAssetFactory = contextAssetFactory;
 161  0
     }
 162  
 }