View Javadoc

1   package org.apache.turbine.services.template.mapper;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.List;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  
23  import org.apache.commons.lang.StringUtils;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import org.apache.turbine.services.template.TemplateEngineService;
29  import org.apache.turbine.services.template.TemplateService;
30  import org.apache.turbine.services.template.TurbineTemplate;
31  
32  /***
33   * This mapper is responsible for the lookup of templates for the Layout
34   * It tries to look in various packages for a match:
35   *
36   * 1. about,directions,Driving.vm      <- exact match
37   * 2. about,directions,Default.vm      <- package match, Default name
38   * 3. about,Default.vm                 <- stepping up in the hierarchy
39   * 4. Default.vm                       <- The name configured as default.layout.template
40   *                                        in the corresponding Templating Engine
41  
42   *
43   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
44   * @version $Id: LayoutTemplateMapper.java 264148 2005-08-29 14:21:04Z henning $
45   */
46  
47  public class LayoutTemplateMapper
48      extends BaseTemplateMapper
49      implements Mapper
50  {
51      /*** Logging */
52      private static Log log = LogFactory.getLog(LayoutTemplateMapper.class);
53  
54      /***
55       * Default C'tor. If you use this C'tor, you must use
56       * the bean setter to set the various properties needed for
57       * this mapper before first usage.
58       */
59      public LayoutTemplateMapper()
60      {
61      }
62  
63      /***
64       * Look for a given Template, then try the
65       * defaults until we hit the root.
66       *
67       * @param template The template name.
68       * @return The parsed module name.
69       */
70      public String doMapping(String template)
71      {
72          log.debug("doMapping(" + template + ")");
73          // Copy our elements into an array
74          List components
75              = new ArrayList(Arrays.asList(StringUtils.split(
76                                                template,
77                                                String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
78          int componentSize = components.size() - 1 ;
79  
80          // This method never gets an empty string passed.
81          // So this is never < 0
82          String templateName = (String) components.get(componentSize);
83          components.remove(componentSize--);
84  
85          log.debug("templateName is " + templateName);
86  
87          // Last element decides, which template Service to use...
88          TemplateEngineService tes = TurbineTemplate.getTemplateEngineService(templateName);
89  
90          if (tes == null)
91          {
92              return null;
93          }
94  
95          String defaultName =
96              TurbineTemplate.getDefaultLayoutTemplateName(templateName); // We're, after all, a Layout Template Mapper...
97  
98          // This is an optimization. If the name we're looking for is
99          // already the default name for the template, don't do a "first run"
100         // which looks for an exact match.
101         boolean firstRun = !templateName.equals(defaultName);
102 
103         for(;;)
104         {
105             String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator));
106 
107             log.debug("templatePackage is now: " + templatePackage);
108 
109             StringBuffer testName = new StringBuffer();
110 
111             if (!components.isEmpty())
112             {
113                 testName.append(templatePackage);
114                 testName.append(separator);
115             }
116 
117             testName.append((firstRun)
118                 ? templateName
119                 : defaultName);
120 
121             // But the Templating service must look for the name with prefix
122             StringBuffer templatePath = new StringBuffer();
123             if (StringUtils.isNotEmpty(prefix))
124             {
125                 templatePath.append(prefix);
126                 templatePath.append(separator);
127             }
128             templatePath.append(testName);
129 
130             log.debug("Looking for " + templatePath);
131 
132             if (tes.templateExists(templatePath.toString()))
133             {
134                 log.debug("Found it, returning " + testName);
135                 return testName.toString();
136             }
137 
138             if (firstRun)
139             {
140                 firstRun = false;
141             }
142             else
143             {
144                 // We're no longer on the first Run (so we
145                 // already tested the "Default" template)
146                 // and the package is empty (we've hit the
147                 // root. So we now break the endless loop.
148                 if (components.isEmpty())
149                 {
150                     break; // for(;;)
151                 }
152                 // We still have components. Remove the
153                 // last one and go through the loop again.
154                 components.remove(componentSize--);
155             }
156         }
157 
158         log.debug("Returning default");
159         return getDefaultName(template);
160     }
161 }