Coverage Report - org.apache.turbine.services.template.mapper.LayoutTemplateMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
LayoutTemplateMapper
97%
37/38
81%
13/16
6
 
 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.lang.StringUtils;
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 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 1071091 2011-02-15 22:06:55Z tv $
 48  
  */
 49  
 
 50  
 public class LayoutTemplateMapper
 51  
     extends BaseTemplateMapper
 52  
     implements Mapper
 53  
 {
 54  
     /** Logging */
 55  58
     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  94
     {
 64  
             // empty
 65  94
     }
 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  
     public String doMapping(String template)
 75  
     {
 76  32
         log.debug("doMapping(" + template + ")");
 77  
         // Copy our elements into an array
 78  32
         List<String> components
 79  
             = new ArrayList<String>(Arrays.asList(StringUtils.split(
 80  
                                               template,
 81  
                                               String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
 82  32
         int componentSize = components.size() - 1 ;
 83  
 
 84  
         // This method never gets an empty string passed.
 85  
         // So this is never < 0
 86  32
         String templateName = components.get(componentSize);
 87  32
         components.remove(componentSize--);
 88  
 
 89  32
         log.debug("templateName is " + templateName);
 90  
 
 91  
         // Last element decides, which template Service to use...
 92  32
         TemplateEngineService tes = TurbineTemplate.getTemplateEngineService(templateName);
 93  
 
 94  32
         if (tes == null)
 95  
         {
 96  0
             return null;
 97  
         }
 98  
 
 99  32
         String defaultName =
 100  
             TurbineTemplate.getDefaultLayoutTemplateName(templateName); // We're, after all, a Layout Template Mapper...
 101  
 
 102  
         // This is an optimization. If the name we're looking for is
 103  
         // already the default name for the template, don't do a "first run"
 104  
         // which looks for an exact match.
 105  32
         boolean firstRun = !templateName.equals(defaultName);
 106  
 
 107  
         for(;;)
 108  
         {
 109  64
             String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator));
 110  
 
 111  64
             log.debug("templatePackage is now: " + templatePackage);
 112  
 
 113  64
             StringBuffer testName = new StringBuffer();
 114  
 
 115  64
             if (!components.isEmpty())
 116  
             {
 117  16
                 testName.append(templatePackage);
 118  16
                 testName.append(separator);
 119  
             }
 120  
 
 121  64
             testName.append((firstRun)
 122  
                 ? templateName
 123  
                 : defaultName);
 124  
 
 125  
             // But the Templating service must look for the name with prefix
 126  64
             StringBuffer templatePath = new StringBuffer();
 127  64
             if (StringUtils.isNotEmpty(prefix))
 128  
             {
 129  64
                 templatePath.append(prefix);
 130  64
                 templatePath.append(separator);
 131  
             }
 132  64
             templatePath.append(testName);
 133  
 
 134  64
             log.debug("Looking for " + templatePath);
 135  
 
 136  64
             if (tes.templateExists(templatePath.toString()))
 137  
             {
 138  8
                 log.debug("Found it, returning " + testName);
 139  8
                 return testName.toString();
 140  
             }
 141  
 
 142  56
             if (firstRun)
 143  
             {
 144  24
                 firstRun = false;
 145  
             }
 146  
             else
 147  
             {
 148  
                 // We're no longer on the first Run (so we
 149  
                 // already tested the "Default" template)
 150  
                 // and the package is empty (we've hit the
 151  
                 // root. So we now break the endless loop.
 152  32
                 if (components.isEmpty())
 153  
                 {
 154  24
                     break; // for(;;)
 155  
                 }
 156  
                 // We still have components. Remove the
 157  
                 // last one and go through the loop again.
 158  8
                 components.remove(componentSize--);
 159  
             }
 160  32
         }
 161  
 
 162  24
         log.debug("Returning default");
 163  24
         return getDefaultName(template);
 164  
     }
 165  
 }