View Javadoc

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