<?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 :: Frontend :: Components :: HTML Template</a> > <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-core-default</a> > <a href="index.source.html" class="el_package">it.tidalwave.northernwind.core.impl.text</a> > <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 "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.
*
* *********************************************************************************************************************
*
*
* *********************************************************************************************************************
* #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<?> clazz;
@Nonnull
private final Site site;
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
public Template getTemplate (@Nonnull final Optional<? extends ResourcePath> templatePath,
@Nonnull final String embeddedResourceName)
{
<span class="fc" id="L69"> log.debug("getTemplate({}, {})", templatePath, embeddedResourceName);</span>
<span class="fc" id="L70"> final var text = templatePath.flatMap(this::getTemplate)</span>
<span class="fc" id="L71"> .orElseGet(() -> getEmbeddedTemplate(embeddedResourceName));</span>
<span class="fc bfc" id="L72" title="All 2 branches covered."> final var delimiter = embeddedResourceName.endsWith(".xslt") ? '%' : '$';</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<String> getTemplate (@Nonnull final ResourcePath templatePath)
{
<span class="fc" id="L85"> log.debug("getTemplate({})", templatePath);</span>
<span class="fc" id="L86"> return site.find(_Content_).withRelativePath(templatePath).optionalResult().flatMap(c -> 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("/" + packagePath + "/" + 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("Missing resource: " + 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>