View Javadoc
1   package org.apache.turbine.services.template.mapper;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.logging.log4j.LogManager;
30  import org.apache.logging.log4j.Logger;
31  import org.apache.turbine.services.TurbineServices;
32  import org.apache.turbine.services.template.TemplateEngineService;
33  import org.apache.turbine.services.template.TemplateService;
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 1854688 2019-03-03 10:36:42Z tv $
48   */
49  
50  public class LayoutTemplateMapper
51      extends BaseTemplateMapper
52      implements Mapper
53  {
54      /** Logging */
55      private static final Logger log = LogManager.getLogger(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      	// empty
65      }
66  
67      /**
68       * Look for a given Template, then try the
69       * defaults until we hit the root.
70       *
71       * @param template The template name.
72       * @return The parsed module name.
73       */
74      @Override
75      public String doMapping(String template)
76      {
77          log.debug("doMapping({})", template);
78          // Copy our elements into an array
79          List<String> components
80              = new ArrayList<String>(Arrays.asList(StringUtils.split(
81                                                template,
82                                                String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
83          int componentSize = components.size() - 1 ;
84  
85          // This method never gets an empty string passed.
86          // So this is never < 0
87          String templateName = components.get(componentSize);
88          components.remove(componentSize--);
89  
90          log.debug("templateName is {}", templateName);
91  
92          // Last element decides, which template Service to use...
93          TemplateServiceorg/apache/turbine/services/template/TemplateService.html#TemplateService">TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
94          TemplateEngineService tes = templateService.getTemplateEngineService(templateName);
95  
96          if (tes == null)
97          {
98              return null;
99          }
100 
101         // We're, after all, a Layout Template Mapper...
102         String defaultName = templateService.getDefaultLayoutTemplateName(templateName);
103 
104         // This is an optimization. If the name we're looking for is
105         // already the default name for the template, don't do a "first run"
106         // which looks for an exact match.
107         boolean firstRun = !templateName.equals(defaultName);
108 
109         for(;;)
110         {
111             String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator));
112 
113             log.debug("templatePackage is now: {}", templatePackage);
114 
115             StringBuilder testName = new StringBuilder();
116 
117             if (!components.isEmpty())
118             {
119                 testName.append(templatePackage);
120                 testName.append(separator);
121             }
122 
123             testName.append((firstRun)
124                 ? templateName
125                 : defaultName);
126 
127             // But the Templating service must look for the name with prefix
128             StringBuilder templatePath = new StringBuilder();
129             if (StringUtils.isNotEmpty(prefix))
130             {
131                 templatePath.append(prefix);
132                 templatePath.append(separator);
133             }
134             templatePath.append(testName);
135 
136             log.debug("Looking for {}", templatePath);
137 
138             if (tes.templateExists(templatePath.toString()))
139             {
140                 log.debug("Found it, returning {}", testName);
141                 return testName.toString();
142             }
143 
144             if (firstRun)
145             {
146                 firstRun = false;
147             }
148             else
149             {
150                 // We're no longer on the first Run (so we
151                 // already tested the "Default" template)
152                 // and the package is empty (we've hit the
153                 // root. So we now break the endless loop.
154                 if (components.isEmpty())
155                 {
156                     break; // for(;;)
157                 }
158                 // We still have components. Remove the
159                 // last one and go through the loop again.
160                 components.remove(componentSize--);
161             }
162         }
163 
164         log.debug("Returning default");
165         return getDefaultName(template);
166     }
167 }