001    /*
002     *  Copyright 2007 Tacos.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     * 
008     *       http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     *  under the License.
016     */
017    package net.sf.tacos.resolvers;
018    
019    import org.apache.hivemind.Resource;
020    import org.apache.hivemind.impl.DefaultClassResolver;
021    import org.apache.hivemind.impl.LocationImpl;
022    import org.apache.hivemind.util.ClasspathResource;
023    import org.apache.tapestry.INamespace;
024    import org.apache.tapestry.IRequestCycle;
025    import org.apache.tapestry.resolver.ISpecificationResolverDelegate;
026    import org.apache.tapestry.services.ClassFinder;
027    import org.apache.tapestry.spec.IComponentSpecification;
028    import org.apache.tapestry.spec.ComponentSpecification;
029    
030    /**
031     * Loads tapestry templates from the classpath - near their corresponding java file.
032     * 
033     * @author Andreas Andreou, amplafi.com
034     */
035    public class ClasspathSpecResolverDelegate implements ISpecificationResolverDelegate {
036        private ClassFinder classFinder;
037        
038        public IComponentSpecification findComponentSpecification(
039                IRequestCycle cycle, INamespace namespace, String name) {
040            return null;
041        }
042        
043        public IComponentSpecification findPageSpecification(IRequestCycle cycle,
044                INamespace namespace, String name) {
045            if (namespace.isApplicationNamespace()) {
046                String packages = namespace
047                        .getPropertyValue("org.apache.tapestry.page-class-packages");
048                
049                String className = name.replace('/', '.');
050                
051                Class pageClass = classFinder.findClass(packages, className);
052                
053                if (pageClass == null) {
054                    return null;
055                }
056                String fullPath = pageClass.getName().replace('.', '/');
057                ClasspathResource html = new ClasspathResource(new DefaultClassResolver(), fullPath + ".html");
058                
059                if (html.getResourceURL() != null) {
060                    return setupImplicitPage(namespace.getSpecificationLocation(),
061                            new ClasspathResource(new DefaultClassResolver(), fullPath + ".page"));
062                }
063            }
064            return null;
065        }
066        
067        private IComponentSpecification setupImplicitPage(Resource resource, Resource pageResource) {
068            IComponentSpecification specification = new ComponentSpecification();
069            specification.setPageSpecification(true);
070            specification.setSpecificationLocation(pageResource);
071            specification.setLocation(new LocationImpl(resource));
072            
073            return specification;
074        }
075        
076        public void setClassFinder(ClassFinder classFinder) {
077            this.classFinder = classFinder;
078        }
079        
080    }
081