Content of file St4TemplateFactory.java.html

<?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="en"><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>St4TemplateFactory.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">NorthernWind :: Filesystems :: SCM :: Git</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-core-default</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.core.impl.text</a> &gt; <span class="el_source">St4TemplateFactory.java</span></div><h1>St4TemplateFactory.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 *
 * NorthernWind - lightweight CMS
 * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
 * %%
 * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
 * %%
 * *********************************************************************************************************************
 *
 * Licensed 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.
 *
 * *********************************************************************************************************************
 *
 *
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.northernwind.core.impl.text;

import javax.annotation.Nonnull;
import java.util.Optional;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.stringtemplate.v4.ST;
import it.tidalwave.northernwind.core.model.ResourcePath;
import it.tidalwave.northernwind.core.model.Site;
import it.tidalwave.northernwind.core.model.Template;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.northernwind.core.model.Content.*;

/***********************************************************************************************************************
 *
 * A factory of {@link Template} implementations based on StringTemplate ({@link ST}.
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L51">@RequiredArgsConstructor @Slf4j</span>
public class St4TemplateFactory
  {
    @Nonnull
    private final Class&lt;?&gt; clazz;

    @Nonnull
    private final Site site;

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Template getTemplate (@Nonnull final Optional&lt;? extends ResourcePath&gt; templatePath,
                                 @Nonnull final String embeddedResourceName)
      {
<span class="fc" id="L69">        log.debug(&quot;getTemplate({}, {})&quot;, templatePath, embeddedResourceName);</span>
<span class="fc" id="L70">        final var text = templatePath.flatMap(this::getTemplate)</span>
<span class="fc" id="L71">                                     .orElseGet(() -&gt; getEmbeddedTemplate(embeddedResourceName));</span>
<span class="pc bpc" id="L72" title="1 of 2 branches missed.">        final var delimiter = embeddedResourceName.endsWith(&quot;.xslt&quot;) ? '%' : '$';</span>
        // TODO: use a cache. Implement it on Site (as a generic cache), so it gets resetted when the Site is reset.
use a cache. Implement it on Site (as a generic cache), so it gets resetted when the Site is reset.
<span class="fc" id="L74"> return new St4Template(text, delimiter);</span> } /******************************************************************************************************************* * * * ******************************************************************************************************************/ @Nonnull public Optional&lt;String&gt; getTemplate (@Nonnull final ResourcePath templatePath) { <span class="fc" id="L85"> log.debug(&quot;getTemplate({})&quot;, templatePath);</span> <span class="fc" id="L86"> return site.find(_Content_).withRelativePath(templatePath).optionalResult().flatMap(c -&gt; c.getProperty(P_TEMPLATE));</span> } /******************************************************************************************************************* * * Gets an embedded, default template with the given name. The template file should be in the classpath, in the same * package as the owner class. * * @param fileName the name of the file * @return the template contents * @throws RuntimeException if the template can't be loaded * ******************************************************************************************************************/ @Nonnull /* visible for testing */ String getEmbeddedTemplate (@Nonnull final String fileName) { <span class="fc" id="L102"> final var packagePath = clazz.getPackage().getName().replace('.', '/');</span> <span class="fc" id="L103"> final Resource resource = new ClassPathResource(&quot;/&quot; + packagePath + &quot;/&quot; + fileName);</span> <span class="fc" id="L105"> try (final Reader r = new InputStreamReader(resource.getInputStream()))</span> { <span class="fc" id="L107"> final var buffer = new char[(int)resource.contentLength()];</span> <span class="fc" id="L108"> r.read(buffer);</span> <span class="fc" id="L109"> return new String(buffer);</span> } <span class="fc" id="L111"> catch (IOException e)</span> { <span class="fc" id="L113"> throw new RuntimeException(&quot;Missing resource: &quot; + fileName, e);</span> } } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>