Coverage Report - org.apache.tapestry.asset.AssetSourceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AssetSourceImpl
0%
0/44
0%
0/24
2.778
 
 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  0
 public class AssetSourceImpl implements AssetSource
 30  
 {
 31  0
     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  0
         Iterator i = _contributions.iterator();
 46  0
         while (i.hasNext())
 47  
         {
 48  0
             AssetFactoryContribution c = (AssetFactoryContribution) i.next();
 49  
 
 50  0
             _assetFactoryByPrefix.put(c.getPrefix(), c.getFactory());
 51  0
         }
 52  0
     }
 53  
 
 54  
     public IAsset findAsset(Resource base, String path, Locale locale, Location location)
 55  
     {
 56  0
         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  0
         Defense.notNull(path, "path");
 62  0
         Defense.notNull(location, "location");
 63  
 
 64  0
         int colonx = path.indexOf(':');
 65  
         
 66  0
         String prefix = colonx > -1 ? path.substring(0, colonx) : null;
 67  0
         String truePath = colonx > -1 ? path.substring(colonx + 1) : path;
 68  
 
 69  0
         AssetFactory factory = null;
 70  
 
 71  0
         if (prefix != null) {
 72  
 
 73  0
             factory = (AssetFactory) _assetFactoryByPrefix.get(prefix);
 74  
         }
 75  
 
 76  
         // now we have to search
 77  
         
 78  0
         if (factory == null && prefix == null) {
 79  
             
 80  0
             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  0
         if (factory == null)
 87  
         {
 88  0
             factory = _defaultAssetFactory;
 89  
 
 90  
             // Path is the full path, including the prefix (which is really the scheme
 91  
             // of the URL).
 92  
 
 93  0
             truePath = path;
 94  
         }
 95  
         
 96  0
         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  0
         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  0
         if (_classpathAssetFactory.assetExists(spec, baseResource, path, locale))
 109  0
             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  0
         _contributions = contributions;
 142  0
     }
 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  0
         _defaultAssetFactory = defaultAssetFactory;
 151  0
     }
 152  
 
 153  
     public void setClasspathAssetFactory(AssetFactory classpathAssetFactory)
 154  
     {
 155  0
         _classpathAssetFactory = classpathAssetFactory;
 156  0
     }
 157  
 
 158  
     public void setContextAssetFactory(AssetFactory contextAssetFactory)
 159  
     {
 160  0
         _contextAssetFactory = contextAssetFactory;
 161  0
     }
 162  
 }