View Javadoc

1   /*
2    *  Copyright 2007 Tacos.
3    * 
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    * 
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *  under the License.
16   */
17  package net.sf.tacos.resolvers;
18  
19  import org.apache.hivemind.Resource;
20  import org.apache.hivemind.impl.DefaultClassResolver;
21  import org.apache.hivemind.impl.LocationImpl;
22  import org.apache.hivemind.util.ClasspathResource;
23  import org.apache.tapestry.INamespace;
24  import org.apache.tapestry.IRequestCycle;
25  import org.apache.tapestry.resolver.ISpecificationResolverDelegate;
26  import org.apache.tapestry.services.ClassFinder;
27  import org.apache.tapestry.spec.IComponentSpecification;
28  import org.apache.tapestry.spec.ComponentSpecification;
29  
30  /**
31   * Loads tapestry templates from the classpath - near their corresponding java file.
32   * 
33   * @author Andreas Andreou, amplafi.com
34   */
35  public class ClasspathSpecResolverDelegate implements ISpecificationResolverDelegate {
36      private ClassFinder classFinder;
37      
38      public IComponentSpecification findComponentSpecification(
39              IRequestCycle cycle, INamespace namespace, String name) {
40          return null;
41      }
42      
43      public IComponentSpecification findPageSpecification(IRequestCycle cycle,
44              INamespace namespace, String name) {
45          if (namespace.isApplicationNamespace()) {
46              String packages = namespace
47                      .getPropertyValue("org.apache.tapestry.page-class-packages");
48              
49              String className = name.replace('/', '.');
50              
51              Class pageClass = classFinder.findClass(packages, className);
52              
53              if (pageClass == null) {
54                  return null;
55              }
56              String fullPath = pageClass.getName().replace('.', '/');
57              ClasspathResource html = new ClasspathResource(new DefaultClassResolver(), fullPath + ".html");
58              
59              if (html.getResourceURL() != null) {
60                  return setupImplicitPage(namespace.getSpecificationLocation(),
61                          new ClasspathResource(new DefaultClassResolver(), fullPath + ".page"));
62              }
63          }
64          return null;
65      }
66      
67      private IComponentSpecification setupImplicitPage(Resource resource, Resource pageResource) {
68          IComponentSpecification specification = new ComponentSpecification();
69          specification.setPageSpecification(true);
70          specification.setSpecificationLocation(pageResource);
71          specification.setLocation(new LocationImpl(resource));
72          
73          return specification;
74      }
75      
76      public void setClassFinder(ClassFinder classFinder) {
77          this.classFinder = classFinder;
78      }
79      
80  }
81