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