001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.asset;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.Location;
019    import org.apache.hivemind.Resource;
020    import org.apache.tapestry.IAsset;
021    import org.apache.tapestry.IRequestCycle;
022    import org.apache.tapestry.l10n.ResourceLocalizer;
023    import org.apache.tapestry.spec.IComponentSpecification;
024    import org.apache.tapestry.web.WebContext;
025    import org.apache.tapestry.web.WebContextResource;
026    
027    import java.util.Locale;
028    
029    /**
030     * All "context:" prefixed asset paths are interpreted relative to the web context (the web
031     * application's root folder).
032     * 
033     * @author Howard M. Lewis Ship
034     * @since 4.0
035     */
036    public class ContextAssetFactory implements AssetFactory
037    {
038        private String _contextPath;
039    
040        private WebContext _webContext;
041    
042        private ResourceLocalizer _localizer;
043    
044        private IRequestCycle _requestCycle;
045    
046        public void setWebContext(WebContext webContext)
047        {
048            _webContext = webContext;
049        }
050    
051        public boolean assetExists(IComponentSpecification spec, Resource baseResource, String path, Locale locale)
052        {
053            return findAsset(spec, baseResource, path, locale) != null;
054        }
055    
056        Resource findAsset(IComponentSpecification spec, Resource baseResource, String path, Locale locale)
057        {
058            Resource assetResource = baseResource.getRelativeResource("/").getRelativeResource(path);
059            Resource localized = _localizer.findLocalization(assetResource, locale);
060    
061            if (localized == null) {
062    
063                assetResource = baseResource.getRelativeResource(path);
064                localized = _localizer.findLocalization(assetResource, locale);
065            }
066    
067            if (localized == null && spec != null && spec.getLocation().getResource() != null) {
068                // try relative to specification
069    
070                assetResource = spec.getLocation().getResource().getRelativeResource("/").getRelativeResource(path);
071                
072                localized = _localizer.findLocalization(assetResource, locale);
073            }
074    
075            if (localized == null || localized.getResourceURL() == null) {
076                
077                // try relative to context root
078    
079                // paths must begin with "/" for context resolution
080                
081                if (path != null && !path.startsWith("/"))
082                    path = "/" + path;
083                
084                Resource base = new WebContextResource(_webContext, path);
085                localized = _localizer.findLocalization(base, locale);
086            }
087    
088            return localized;
089        }
090    
091        public IAsset createAsset(Resource baseResource, IComponentSpecification spec, String path, Locale locale, Location location)
092        {
093            Resource localized = findAsset(spec, baseResource, path, locale);
094            
095            // We always create a new asset relative to an existing resource; the type of resource
096            // will jive with the type of asset returned. Path may start with a leading slash, which
097            // yields an absolute, not relative, path to the resource.
098    
099            if ( (localized == null || localized.getResourceURL() == null)
100                 && path.startsWith("/")) {
101    
102                return createAbsoluteAsset(path, locale, location);
103            }
104            
105            if (localized == null)
106                throw new ApplicationRuntimeException(AssetMessages.missingAsset(path, baseResource), location, null);
107    
108            return createAsset(localized, location);
109        }
110    
111        public IAsset createAbsoluteAsset(String path, Locale locale, Location location)
112        {
113            Resource base = new WebContextResource(_webContext, path);
114            Resource localized = _localizer.findLocalization(base, locale);
115    
116            if (localized == null)
117                throw new ApplicationRuntimeException(AssetMessages.missingContextResource(path), location, null);
118    
119            return createAsset(localized, location);
120        }
121    
122        public IAsset createAsset(Resource resource, Location location)
123        {
124            return new ContextAsset(_contextPath, resource, location, _requestCycle);
125        }
126    
127        public void setContextPath(String contextPath)
128        {
129            _contextPath = contextPath;
130        }
131        
132        public void setLocalizer(ResourceLocalizer localizer)
133        {
134            _localizer = localizer;
135        }
136    
137        public void setRequestCycle(IRequestCycle cycle)
138        {
139            _requestCycle = cycle;
140        }
141    }