Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
file stats: LOC: 113   Methods: 8
NCLOC: 61   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ContextAssetFactory.java 100% 100% 100% 100%
coverage
 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 java.util.Locale;
 18   
 19    import org.apache.hivemind.ApplicationRuntimeException;
 20    import org.apache.hivemind.Location;
 21    import org.apache.hivemind.Resource;
 22    import org.apache.tapestry.IAsset;
 23    import org.apache.tapestry.IRequestCycle;
 24    import org.apache.tapestry.l10n.ResourceLocalizer;
 25    import org.apache.tapestry.web.WebContext;
 26    import org.apache.tapestry.web.WebContextResource;
 27   
 28    /**
 29    * All "context:" prefixed asset paths are interpreted relative to the web context (the web
 30    * application's root folder).
 31    *
 32    * @author Howard M. Lewis Ship
 33    * @since 4.0
 34    */
 35    public class ContextAssetFactory implements AssetFactory
 36    {
 37    private String _contextPath;
 38   
 39    private AssetFactory _classpathAssetFactory;
 40   
 41    private WebContext _webContext;
 42   
 43    private ResourceLocalizer _localizer;
 44   
 45    private IRequestCycle _requestCycle;
 46   
 47  18 public void setWebContext(WebContext webContext)
 48    {
 49  18 _webContext = webContext;
 50    }
 51   
 52  8 public IAsset createAsset(Resource baseResource, String path, Locale locale, Location location)
 53    {
 54    // We always create a new asset relative to an existing resource; the type of resource
 55    // will jive with the type of asset returned. Path may start with a leading slash, which
 56    // yields an absolute, not relative, path to the resource.
 57   
 58  8 Resource assetResource = baseResource.getRelativeResource(path);
 59   
 60    // Here's the thing; In Tapestry 3.0 and earlier, you could specify
 61    // library path like /org/apache/tapestry/contrib/Contrib.library. In the new scheme
 62    // of things, that should be "classpath:/org/apache/tapestry/contrib/Contrib.library".
 63    // But to keep a lot of things from breaking, we'll kludgely allow that here.
 64   
 65  8 if (assetResource.getResourceURL() == null && path.startsWith("/"))
 66  4 return _classpathAssetFactory.createAbsoluteAsset(path, locale, location);
 67   
 68  4 Resource localized = _localizer.findLocalization(assetResource, locale);
 69   
 70  4 if (localized == null)
 71  2 throw new ApplicationRuntimeException(AssetMessages.missingAsset(path, baseResource),
 72    location, null);
 73   
 74  2 return createAsset(localized, location);
 75    }
 76   
 77  18 public IAsset createAbsoluteAsset(String path, Locale locale, Location location)
 78    {
 79  18 Resource base = new WebContextResource(_webContext, path);
 80  18 Resource localized = _localizer.findLocalization(base, locale);
 81   
 82  18 if (localized == null)
 83  2 throw new ApplicationRuntimeException(AssetMessages.missingContextResource(path),
 84    location, null);
 85   
 86  16 return createAsset(localized, location);
 87    }
 88   
 89  18 public IAsset createAsset(Resource resource, Location location)
 90    {
 91  18 return new ContextAsset(_contextPath, resource, location, _requestCycle);
 92    }
 93   
 94  24 public void setContextPath(String contextPath)
 95    {
 96  24 _contextPath = contextPath;
 97    }
 98   
 99  14 public void setClasspathAssetFactory(AssetFactory classpathAssetFactory)
 100    {
 101  14 _classpathAssetFactory = classpathAssetFactory;
 102    }
 103   
 104  22 public void setLocalizer(ResourceLocalizer localizer)
 105    {
 106  22 _localizer = localizer;
 107    }
 108   
 109  14 public void setRequestCycle(IRequestCycle cycle)
 110    {
 111  14 _requestCycle = cycle;
 112    }
 113    }