blob: 913134e2e74e997a27e4b304d2125c8976a84fc9 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>LayoutTemplateMapper.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Turbine</a> &gt; <a href="index.source.html" class="el_package">org.apache.turbine.services.template.mapper</a> &gt; <span class="el_source">LayoutTemplateMapper.java</span></div><h1>LayoutTemplateMapper.java</h1><pre class="source lang-java linenums">package org.apache.turbine.services.template.mapper;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.template.TemplateEngineService;
import org.apache.turbine.services.template.TemplateService;
/**
* This mapper is responsible for the lookup of templates for the Layout
* It tries to look in various packages for a match:
*
* 1. about,directions,Driving.vm &amp;lt;- exact match
* 2. about,directions,Default.vm &amp;lt;- package match, Default name
* 3. about,Default.vm &amp;lt;- stepping up in the hierarchy
* 4. Default.vm &amp;lt;- The name configured as default.layout.template
* in the corresponding Templating Engine
*
* @author &lt;a href=&quot;mailto:hps@intermeta.de&quot;&gt;Henning P. Schmiedehausen&lt;/a&gt;
* @version $Id$
*/
public class LayoutTemplateMapper
extends BaseTemplateMapper
implements Mapper
{
/** Logging */
<span class="fc" id="L55"> private static final Logger log = LogManager.getLogger(LayoutTemplateMapper.class);</span>
/**
* Default C'tor. If you use this C'tor, you must use
* the bean setter to set the various properties needed for
* this mapper before first usage.
*/
public LayoutTemplateMapper()
<span class="fc" id="L63"> {</span>
// empty
<span class="fc" id="L65"> }</span>
/**
* Look for a given Template, then try the
* defaults until we hit the root.
*
* @param template The template name.
* @return The parsed module name.
*/
@Override
public String doMapping(String template)
{
<span class="nc" id="L77"> log.debug(&quot;doMapping({})&quot;, template);</span>
// Copy our elements into an array
<span class="nc" id="L79"> List&lt;String&gt; components</span>
<span class="nc" id="L80"> = new ArrayList&lt;&gt;(Arrays.asList(StringUtils.split(</span>
template,
<span class="nc" id="L82"> String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));</span>
<span class="nc" id="L83"> int componentSize = components.size() - 1 ;</span>
// This method never gets an empty string passed.
// So this is never &lt; 0
<span class="nc" id="L87"> String templateName = components.get(componentSize);</span>
<span class="nc" id="L88"> components.remove(componentSize--);</span>
<span class="nc" id="L90"> log.debug(&quot;templateName is {}&quot;, templateName);</span>
// Last element decides, which template Service to use...
<span class="nc" id="L93"> TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);</span>
<span class="nc" id="L94"> TemplateEngineService tes = templateService.getTemplateEngineService(templateName);</span>
<span class="nc bnc" id="L96" title="All 2 branches missed."> if (tes == null)</span>
{
<span class="nc" id="L98"> return null;</span>
}
// We're, after all, a Layout Template Mapper...
<span class="nc" id="L102"> String defaultName = templateService.getDefaultLayoutTemplateName(templateName);</span>
// This is an optimization. If the name we're looking for is
// already the default name for the template, don't do a &quot;first run&quot;
// which looks for an exact match.
<span class="nc bnc" id="L107" title="All 2 branches missed."> boolean firstRun = !templateName.equals(defaultName);</span>
for(;;)
{
<span class="nc" id="L111"> String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator));</span>
<span class="nc" id="L113"> log.debug(&quot;templatePackage is now: {}&quot;, templatePackage);</span>
<span class="nc" id="L115"> StringBuilder testName = new StringBuilder();</span>
<span class="nc bnc" id="L117" title="All 2 branches missed."> if (!components.isEmpty())</span>
{
<span class="nc" id="L119"> testName.append(templatePackage);</span>
<span class="nc" id="L120"> testName.append(separator);</span>
}
<span class="nc bnc" id="L123" title="All 2 branches missed."> testName.append((firstRun)</span>
<span class="nc" id="L124"> ? templateName</span>
<span class="nc" id="L125"> : defaultName);</span>
// But the Templating service must look for the name with prefix
<span class="nc" id="L128"> StringBuilder templatePath = new StringBuilder();</span>
<span class="nc bnc" id="L129" title="All 2 branches missed."> if (StringUtils.isNotEmpty(prefix))</span>
{
<span class="nc" id="L131"> templatePath.append(prefix);</span>
<span class="nc" id="L132"> templatePath.append(separator);</span>
}
<span class="nc" id="L134"> templatePath.append(testName);</span>
<span class="nc" id="L136"> log.debug(&quot;Looking for {}&quot;, templatePath);</span>
<span class="nc bnc" id="L138" title="All 2 branches missed."> if (tes.templateExists(templatePath.toString()))</span>
{
<span class="nc" id="L140"> log.debug(&quot;Found it, returning {}&quot;, testName);</span>
<span class="nc" id="L141"> return testName.toString();</span>
}
<span class="nc bnc" id="L144" title="All 2 branches missed."> if (firstRun)</span>
{
<span class="nc" id="L146"> firstRun = false;</span>
}
else
{
// We're no longer on the first Run (so we
// already tested the &quot;Default&quot; template)
// and the package is empty (we've hit the
// root. So we now break the endless loop.
<span class="nc bnc" id="L154" title="All 2 branches missed."> if (components.isEmpty())</span>
{
<span class="nc" id="L156"> break; // for(;;)</span>
}
// We still have components. Remove the
// last one and go through the loop again.
<span class="nc" id="L160"> components.remove(componentSize--);</span>
}
<span class="nc" id="L162"> }</span>
<span class="nc" id="L164"> log.debug(&quot;Returning default&quot;);</span>
<span class="nc" id="L165"> return getDefaultName(template);</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html>