Coverage Report - org.apache.tapestry.asset.ContextAssetFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ContextAssetFactory
0%
0/36
0%
0/28
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.ApplicationRuntimeException;
 18  
 import org.apache.hivemind.Location;
 19  
 import org.apache.hivemind.Resource;
 20  
 import org.apache.tapestry.IAsset;
 21  
 import org.apache.tapestry.IRequestCycle;
 22  
 import org.apache.tapestry.l10n.ResourceLocalizer;
 23  
 import org.apache.tapestry.spec.IComponentSpecification;
 24  
 import org.apache.tapestry.web.WebContext;
 25  
 import org.apache.tapestry.web.WebContextResource;
 26  
 
 27  
 import java.util.Locale;
 28  
 
 29  
 /**
 30  
  * All "context:" prefixed asset paths are interpreted relative to the web context (the web
 31  
  * application's root folder).
 32  
  * 
 33  
  * @author Howard M. Lewis Ship
 34  
  * @since 4.0
 35  
  */
 36  0
 public class ContextAssetFactory implements AssetFactory
 37  
 {
 38  
     private String _contextPath;
 39  
 
 40  
     private WebContext _webContext;
 41  
 
 42  
     private ResourceLocalizer _localizer;
 43  
 
 44  
     private IRequestCycle _requestCycle;
 45  
 
 46  
     public void setWebContext(WebContext webContext)
 47  
     {
 48  0
         _webContext = webContext;
 49  0
     }
 50  
 
 51  
     public boolean assetExists(IComponentSpecification spec, Resource baseResource, String path, Locale locale)
 52  
     {
 53  0
         return findAsset(spec, baseResource, path, locale) != null;
 54  
     }
 55  
 
 56  
     Resource findAsset(IComponentSpecification spec, Resource baseResource, String path, Locale locale)
 57  
     {
 58  0
         Resource assetResource = baseResource.getRelativeResource("/").getRelativeResource(path);
 59  0
         Resource localized = _localizer.findLocalization(assetResource, locale);
 60  
 
 61  0
         if (localized == null) {
 62  
 
 63  0
             assetResource = baseResource.getRelativeResource(path);
 64  0
             localized = _localizer.findLocalization(assetResource, locale);
 65  
         }
 66  
 
 67  0
         if (localized == null && spec != null && spec.getLocation().getResource() != null) {
 68  
             // try relative to specification
 69  
 
 70  0
             assetResource = spec.getLocation().getResource().getRelativeResource("/").getRelativeResource(path);
 71  
             
 72  0
             localized = _localizer.findLocalization(assetResource, locale);
 73  
         }
 74  
 
 75  0
         if (localized == null || localized.getResourceURL() == null) {
 76  
             
 77  
             // try relative to context root
 78  
 
 79  
             // paths must begin with "/" for context resolution
 80  
             
 81  0
             if (path != null && !path.startsWith("/"))
 82  0
                 path = "/" + path;
 83  
             
 84  0
             Resource base = new WebContextResource(_webContext, path);
 85  0
             localized = _localizer.findLocalization(base, locale);
 86  
         }
 87  
 
 88  0
         return localized;
 89  
     }
 90  
 
 91  
     public IAsset createAsset(Resource baseResource, IComponentSpecification spec, String path, Locale locale, Location location)
 92  
     {
 93  0
         Resource localized = findAsset(spec, baseResource, path, locale);
 94  
         
 95  
         // We always create a new asset relative to an existing resource; the type of resource
 96  
         // will jive with the type of asset returned. Path may start with a leading slash, which
 97  
         // yields an absolute, not relative, path to the resource.
 98  
 
 99  0
         if ( (localized == null || localized.getResourceURL() == null)
 100  
              && path.startsWith("/")) {
 101  
 
 102  0
             return createAbsoluteAsset(path, locale, location);
 103  
         }
 104  
         
 105  0
         if (localized == null)
 106  0
             throw new ApplicationRuntimeException(AssetMessages.missingAsset(path, baseResource), location, null);
 107  
 
 108  0
         return createAsset(localized, location);
 109  
     }
 110  
 
 111  
     public IAsset createAbsoluteAsset(String path, Locale locale, Location location)
 112  
     {
 113  0
         Resource base = new WebContextResource(_webContext, path);
 114  0
         Resource localized = _localizer.findLocalization(base, locale);
 115  
 
 116  0
         if (localized == null)
 117  0
             throw new ApplicationRuntimeException(AssetMessages.missingContextResource(path), location, null);
 118  
 
 119  0
         return createAsset(localized, location);
 120  
     }
 121  
 
 122  
     public IAsset createAsset(Resource resource, Location location)
 123  
     {
 124  0
         return new ContextAsset(_contextPath, resource, location, _requestCycle);
 125  
     }
 126  
 
 127  
     public void setContextPath(String contextPath)
 128  
     {
 129  0
         _contextPath = contextPath;
 130  0
     }
 131  
     
 132  
     public void setLocalizer(ResourceLocalizer localizer)
 133  
     {
 134  0
         _localizer = localizer;
 135  0
     }
 136  
 
 137  
     public void setRequestCycle(IRequestCycle cycle)
 138  
     {
 139  0
         _requestCycle = cycle;
 140  0
     }
 141  
 }