Content of file TextHolder.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>TextHolder.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 :: Spring MVC</a> > <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-frontend-components-htmltemplate</a> > <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.ui.component.htmltemplate</a> > <span class="el_source">TextHolder.java</span></div><h1>TextHolder.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.frontend.ui.component.htmltemplate;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Provider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.stringtemplate.v4.ST;
import it.tidalwave.util.Id;
import it.tidalwave.northernwind.core.model.SiteProvider;
import it.tidalwave.northernwind.frontend.ui.SiteViewController;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
/***********************************************************************************************************************
*
* The HtmlTemplate specialization of {@link SiteViewController}.
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="pc" id="L61">@Configurable @Slf4j @ToString(exclude="siteProvider")</span>
public class TextHolder
{
<span class="fc" id="L64"> private final Map<String, String> attributeMap = new HashMap<>();</span>
<span class="fc" id="L66"> private final List<TextHolder> contents = new ArrayList<>();</span>
<span class="nc" id="L68"> @Getter @Setter</span>
private String template;
@Inject
private Provider<SiteProvider> siteProvider;
<span class="pc" id="L74"> @Getter @Setter</span>
private String mimeType = "text/plain";
/*******************************************************************************************************************
*
* Creates an instance with the given name.
*
* @param name the component name
*
******************************************************************************************************************/
public TextHolder (@Nonnull final Id id)
<span class="pc bpc" id="L85" title="9 of 18 branches missed."> {</span>
<span class="fc" id="L86"> attributeMap.put("content", "");</span>
try
{
<span class="fc" id="L90"> loadTemplate();</span>
}
<span class="nc" id="L92"> catch (IOException e)</span>
{
<span class="nc" id="L94"> throw new RuntimeException(e);</span>
<span class="fc" id="L95"> }</span>
<span class="pc bpc" id="L96" title="2 of 4 branches missed."> }</span>
public TextHolder (@Nonnull final String html)
<span class="pc bpc" id="L99" title="9 of 18 branches missed."> {</span>
<span class="fc" id="L100"> addAttribute("content", html);</span>
<span class="fc" id="L101"> template = "$content$";</span>
<span class="pc bpc" id="L102" title="2 of 4 branches missed."> }</span>
// @Override
public void setContent (@Nonnull final String content)
{
<span class="nc" id="L107"> addAttribute("content", content);</span>
// setValue(html);
<span class="nc" id="L109"> }</span>
public void addComponent (@Nonnull final TextHolder child)
{
<span class="fc" id="L113"> contents.add(child);</span>
<span class="fc" id="L114"> }</span>
public void addAttribute (@Nonnull final String name, @Nonnull final String value)
{
<span class="fc" id="L118"> attributeMap.put(name, value);</span>
<span class="fc" id="L119"> }</span>
// @Nonnull
// public byte[] asBytes2 (final @Nonnull Charset charset)
// {
// return asString2(charset).getBytes(charset);
// }
//
// @Nonnull
// public String asString2 (final @Nonnull Charset charset)
// {
// return template;
// }
@Nonnull
public byte[] asBytes (@Nonnull final Charset charset)
{
<span class="fc" id="L136"> return asString(charset).getBytes(charset);</span>
}
@Nonnull
public String asString (@Nonnull final Charset charset)
{
<span class="fc" id="L142"> var t = new ST(template, '$', '$');</span>
<span class="fc bfc" id="L144" title="All 2 branches covered."> for (final var entry : attributeMap.entrySet())</span>
{
<span class="fc" id="L146"> t = t.add(entry.getKey(), entry.getValue());</span>
<span class="fc" id="L147"> }</span>
<span class="fc" id="L149"> final var builder = new StringBuilder();</span>
<span class="fc bfc" id="L151" title="All 2 branches covered."> for (final var child : contents)</span>
{
<span class="fc" id="L153"> builder.append(child.asString(charset)).append("\n");</span>
<span class="fc" id="L154"> }</span>
<span class="fc" id="L156"> t = t.add("content", builder.toString());</span>
// t = t.add("contextPath", siteProvider.get().getSite().getContextPath());
<span class="fc" id="L158"> t = t.add("charset", charset.name());</span>
// t = t.add("language", "");
<span class="fc" id="L161"> return t.render();</span>
}
private void loadTemplate() // FIXME: use TemplateHelper
throws IOException
{
// FIXME: this should be done only once...
<span class="fc" id="L168"> Resource resource = null;</span>
<span class="pc bpc" id="L170" title="1 of 2 branches missed."> for (Class<?> clazz = getClass(); clazz.getSuperclass() != null; clazz = clazz.getSuperclass())</span>
{
<span class="fc" id="L172"> final var templateName = clazz.getSimpleName() + ".txt";</span>
<span class="fc" id="L173"> resource = new ClassPathResource(templateName, clazz);</span>
<span class="fc bfc" id="L175" title="All 2 branches covered."> if (resource.exists())</span>
{
<span class="fc" id="L177"> break;</span>
}
}
try
{
<span class="pc bpc" id="L183" title="1 of 2 branches missed."> if (resource == null)</span>
{
<span class="nc" id="L185"> throw new FileNotFoundException();</span>
}
<span class="fc" id="L188"> try (final Reader r = new InputStreamReader(resource.getInputStream()))</span>
{
<span class="fc" id="L190"> final var charBuffer = CharBuffer.allocate((int)resource.contentLength());</span>
<span class="fc" id="L191"> final var length = r.read(charBuffer);</span>
<span class="fc" id="L192"> template = new String(charBuffer.array(), 0, length);</span>
}
}
<span class="nc" id="L195"> catch (FileNotFoundException e) // no specific template, fallback</span>
{
<span class="nc" id="L197"> log.warn("No template for {}, using default", getClass().getSimpleName());</span>
<span class="nc" id="L198"> template = "$content$\n";</span>
<span class="fc" id="L199"> }</span>
<span class="fc" id="L200"> }</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>