| <?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="de"><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>ScreenDefaultTemplateMapper.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> > <a href="index.source.html" class="el_package">org.apache.turbine.services.template.mapper</a> > <span class="el_source">ScreenDefaultTemplateMapper.java</span></div><h1>ScreenDefaultTemplateMapper.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 |
| * "License"); 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 |
| * "AS IS" 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 is a pretty simple mapper which returns template pathes for |
| * a supplied template name. If the path does not exist, it looks for |
| * a templated called "Default" in the same package. |
| * This path can be used by the TemplateEngine to access |
| * a certain resource to actually render the template. |
| * |
| * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
| * @version $Id$ |
| */ |
| |
| public class ScreenDefaultTemplateMapper |
| extends BaseTemplateMapper |
| implements Mapper |
| { |
| /** Logging */ |
| <span class="nc" id="L51"> private static final Logger log = LogManager.getLogger(ScreenDefaultTemplateMapper.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 ScreenDefaultTemplateMapper() |
| <span class="nc" id="L59"> {</span> |
| // empty |
| <span class="nc" id="L61"> }</span> |
| |
| /** |
| * Look for a given Template, then try the |
| * default. |
| * |
| * @param template The template name. |
| * @return The parsed module name. |
| */ |
| @Override |
| public String doMapping(String template) |
| { |
| <span class="nc" id="L73"> log.debug("doMapping({})", template);</span> |
| // Copy our elements into an array |
| <span class="nc" id="L75"> List<String> components</span> |
| <span class="nc" id="L76"> = new ArrayList<>(Arrays.asList(StringUtils.split(</span> |
| template, |
| <span class="nc" id="L78"> String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));</span> |
| <span class="nc" id="L79"> int componentSize = components.size() - 1 ;</span> |
| |
| // This method never gets an empty string passed. |
| // So this is never < 0 |
| <span class="nc" id="L83"> String templateName = components.get(componentSize);</span> |
| <span class="nc" id="L84"> components.remove(componentSize--);</span> |
| |
| <span class="nc" id="L86"> log.debug("templateName is {}", templateName);</span> |
| |
| // Last element decides, which template Service to use... |
| <span class="nc" id="L89"> TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);</span> |
| <span class="nc" id="L90"> TemplateEngineService tes = templateService.getTemplateEngineService(templateName);</span> |
| |
| <span class="nc bnc" id="L92" title="All 2 branches missed."> if (tes == null)</span> |
| { |
| <span class="nc" id="L94"> return null;</span> |
| } |
| |
| <span class="nc" id="L97"> String defaultName = "Default.vm";</span> |
| |
| // This is an optimization. If the name we're looking for is |
| // already the default name for the template, don't do a "first run" |
| // which looks for an exact match. |
| <span class="nc bnc" id="L102" title="All 2 branches missed."> boolean firstRun = !templateName.equals(defaultName);</span> |
| |
| for(;;) |
| { |
| <span class="nc" id="L106"> String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator));</span> |
| |
| <span class="nc" id="L108"> log.debug("templatePackage is now: {}", templatePackage);</span> |
| |
| <span class="nc" id="L110"> StringBuilder testName = new StringBuilder();</span> |
| |
| <span class="nc bnc" id="L112" title="All 2 branches missed."> if (!components.isEmpty())</span> |
| { |
| <span class="nc" id="L114"> testName.append(templatePackage);</span> |
| <span class="nc" id="L115"> testName.append(separator);</span> |
| } |
| |
| <span class="nc bnc" id="L118" title="All 2 branches missed."> testName.append((firstRun)</span> |
| <span class="nc" id="L119"> ? templateName</span> |
| <span class="nc" id="L120"> : defaultName);</span> |
| |
| // But the Templating service must look for the name with prefix |
| <span class="nc" id="L123"> StringBuilder templatePath = new StringBuilder();</span> |
| <span class="nc bnc" id="L124" title="All 2 branches missed."> if (StringUtils.isNotEmpty(prefix))</span> |
| { |
| <span class="nc" id="L126"> templatePath.append(prefix);</span> |
| <span class="nc" id="L127"> templatePath.append(separator);</span> |
| } |
| <span class="nc" id="L129"> templatePath.append(testName);</span> |
| |
| <span class="nc" id="L131"> log.debug("Looking for {}", templatePath);</span> |
| |
| <span class="nc bnc" id="L133" title="All 2 branches missed."> if (tes.templateExists(templatePath.toString()))</span> |
| { |
| <span class="nc" id="L135"> log.debug("Found it, returning {}", testName);</span> |
| <span class="nc" id="L136"> return testName.toString();</span> |
| } |
| |
| <span class="nc bnc" id="L139" title="All 2 branches missed."> if (firstRun)</span> |
| { |
| <span class="nc" id="L141"> firstRun = false;</span> |
| } |
| else |
| { |
| // We run this loop only two times. The |
| // first time with the 'real' name and the |
| // second time with "Default". The second time |
| // we will end up here and break the for(;;) loop. |
| break; |
| } |
| <span class="nc" id="L151"> }</span> |
| |
| <span class="nc" id="L153"> log.debug("Returning default");</span> |
| <span class="nc" id="L154"> 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> |