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 is a pretty simple mapper which returns template pathes for
37   * a supplied template name. If the path does not exist, it looks for
38   * a templated called "Default" in the same package.
39   * This path can be used by the TemplateEngine to access
40   * a certain resource to actually render the template.
41   *
42   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
43   * @version $Id: ScreenDefaultTemplateMapper.java 534527 2007-05-02 16:10:59Z tv $
44   */
45  
46  public class ScreenDefaultTemplateMapper
47      extends BaseTemplateMapper
48      implements Mapper
49  {
50      /*** Logging */
51      private static Log log = LogFactory.getLog(ScreenDefaultTemplateMapper.class);
52  
53      /***
54       * Default C'tor. If you use this C'tor, you must use
55       * the bean setter to set the various properties needed for
56       * this mapper before first usage.
57       */
58      public ScreenDefaultTemplateMapper()
59      {
60      }
61  
62      /***
63       * Look for a given Template, then try the
64       * default.
65       *
66       * @param template The template name.
67       * @return The parsed module name.
68       */
69      public String doMapping(String template)
70      {
71          log.debug("doMapping(" + template + ")");
72          // Copy our elements into an array
73          List components
74              = new ArrayList(Arrays.asList(StringUtils.split(
75                                                template,
76                                                String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
77          int componentSize = components.size() - 1 ;
78  
79          // This method never gets an empty string passed.
80          // So this is never < 0
81          String templateName = (String) components.get(componentSize);
82          components.remove(componentSize--);
83  
84          log.debug("templateName is " + templateName);
85  
86          // Last element decides, which template Service to use...
87          TemplateEngineService tes = TurbineTemplate.getTemplateEngineService(templateName);
88  
89          if (tes == null)
90          {
91              return null;
92          }
93  
94          String defaultName = "Default.vm";
95  
96          // This is an optimization. If the name we're looking for is
97          // already the default name for the template, don't do a "first run"
98          // which looks for an exact match.
99          boolean firstRun = !templateName.equals(defaultName);
100 
101         for(;;)
102         {
103             String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator));
104 
105             log.debug("templatePackage is now: " + templatePackage);
106 
107             StringBuffer testName = new StringBuffer();
108 
109             if (!components.isEmpty())
110             {
111                 testName.append(templatePackage);
112                 testName.append(separator);
113             }
114 
115             testName.append((firstRun)
116                 ? templateName
117                 : defaultName);
118 
119             // But the Templating service must look for the name with prefix
120             StringBuffer templatePath = new StringBuffer();
121             if (StringUtils.isNotEmpty(prefix))
122             {
123                 templatePath.append(prefix);
124                 templatePath.append(separator);
125             }
126             templatePath.append(testName);
127 
128             log.debug("Looking for " + templatePath);
129 
130             if (tes.templateExists(templatePath.toString()))
131             {
132                 log.debug("Found it, returning " + testName);
133                 return testName.toString();
134             }
135 
136             if (firstRun)
137             {
138                 firstRun = false;
139             }
140             else
141             {
142                 // We run this loop only two times. The
143                 // first time with the 'real' name and the
144                 // second time with "Default". The second time
145                 // we will end up here and break the for(;;) loop.
146                 break;
147             }
148         }
149 
150         log.debug("Returning default");
151         return getDefaultName(template);
152     }
153 }