Skip to content

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 :: Components :: HTML Template</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.ui.component.htmltemplate</a> &gt; <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 &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.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.util.Map.Entry;
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="L62">@Configurable @Slf4j @ToString(exclude=&quot;siteProvider&quot;)</span>
public class TextHolder
  {
<span class="fc" id="L65">    private final Map&lt;String, String&gt; attributeMap = new HashMap&lt;&gt;();</span>

<span class="fc" id="L67">    private final List&lt;TextHolder&gt; contents = new ArrayList&lt;&gt;();</span>

<span class="nc" id="L69">    @Getter @Setter</span>
    private String template;

    @Inject
    private Provider&lt;SiteProvider&gt; siteProvider;

<span class="pc" id="L75">    @Getter @Setter</span>
    private String mimeType = &quot;text/plain&quot;;

    /*******************************************************************************************************************
     *
     * Creates an instance with the given name.
     *
     * @param  name  the component name
     *
     ******************************************************************************************************************/
    public TextHolder (@Nonnull final Id id)
<span class="pc bpc" id="L86" title="9 of 18 branches missed.">      {</span>
<span class="fc" id="L87">        attributeMap.put(&quot;content&quot;, &quot;&quot;);</span>

        try
          {
<span class="fc" id="L91">            loadTemplate();</span>
          }
<span class="nc" id="L93">        catch (IOException e)</span>
          {
<span class="nc" id="L95">            throw new RuntimeException(e);</span>
<span class="fc" id="L96">          }</span>
<span class="pc bpc" id="L97" title="2 of 4 branches missed.">      }</span>

    public TextHolder (@Nonnull final String html)
<span class="pc bpc" id="L100" title="9 of 18 branches missed.">      {</span>
<span class="fc" id="L101">        addAttribute(&quot;content&quot;, html);</span>
<span class="fc" id="L102">        template = &quot;$content$&quot;;</span>
<span class="pc bpc" id="L103" title="2 of 4 branches missed.">      }</span>

//    @Override
    public void setContent (@Nonnull final String content)
      {
<span class="nc" id="L108">        addAttribute(&quot;content&quot;, content);</span>
//        setValue(html);
<span class="nc" id="L110">      }</span>

    public void addComponent (@Nonnull final TextHolder child)
      {
<span class="fc" id="L114">        contents.add(child);</span>
<span class="fc" id="L115">      }</span>

    public void addAttribute (@Nonnull final String name, @Nonnull final String value)
      {
<span class="fc" id="L119">        attributeMap.put(name, value);</span>
<span class="fc" id="L120">      }</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="L137">        return asString(charset).getBytes(charset);</span>
      }

    @Nonnull
    public String asString (@Nonnull final Charset charset)
      {
<span class="fc" id="L143">        ST t = new ST(template, '$', '$');</span>

<span class="fc bfc" id="L145" title="All 2 branches covered.">        for (final Entry&lt;String, String&gt; entry : attributeMap.entrySet())</span>
          {
<span class="fc" id="L147">            t = t.add(entry.getKey(), entry.getValue());</span>
<span class="fc" id="L148">          }</span>

<span class="fc" id="L150">        final StringBuilder builder = new StringBuilder();</span>

<span class="fc bfc" id="L152" title="All 2 branches covered.">        for (final TextHolder child : contents)</span>
          {
<span class="fc" id="L154">            builder.append(child.asString(charset)).append(&quot;\n&quot;);</span>
<span class="fc" id="L155">          }</span>

<span class="fc" id="L157">        t = t.add(&quot;content&quot;,     builder.toString());</span>
//        t = t.add(&quot;contextPath&quot;, siteProvider.get().getSite().getContextPath());
<span class="fc" id="L159">        t = t.add(&quot;charset&quot;,     charset.name());</span>
//        t = t.add(&quot;language&quot;,    &quot;&quot;);

<span class="fc" id="L162">        return t.render();</span>
      }

    private void loadTemplate() // FIXME: use TemplateHelper
use TemplateHelper
throws IOException { // FIXME: this should be done only once... <span class="fc" id="L169"> Resource resource = null;</span> <span class="pc bpc" id="L171" title="1 of 2 branches missed."> for (Class&lt;?&gt; clazz = getClass(); clazz.getSuperclass() != null; clazz = clazz.getSuperclass())</span> { <span class="fc" id="L173"> final String templateName = clazz.getSimpleName() + &quot;.txt&quot;;</span> <span class="fc" id="L174"> resource = new ClassPathResource(templateName, clazz);</span> <span class="fc bfc" id="L176" title="All 2 branches covered."> if (resource.exists())</span> { <span class="fc" id="L178"> break;</span> } } try { <span class="pc bpc" id="L184" title="1 of 2 branches missed."> if (resource == null)</span> { <span class="nc" id="L186"> throw new FileNotFoundException();</span> } <span class="fc" id="L189"> try (final Reader r = new InputStreamReader(resource.getInputStream()))</span> { <span class="fc" id="L191"> final CharBuffer charBuffer = CharBuffer.allocate((int)resource.contentLength());</span> <span class="fc" id="L192"> final int length = r.read(charBuffer);</span> <span class="fc" id="L193"> template = new String(charBuffer.array(), 0, length);</span> } } <span class="nc" id="L196"> catch (FileNotFoundException e) // no specific template, fallback</span> { <span class="nc" id="L198"> log.warn(&quot;No template for {}, using default&quot;, getClass().getSimpleName());</span> <span class="nc" id="L199"> template = &quot;$content$\n&quot;;</span> <span class="fc" id="L200"> }</span> <span class="fc" id="L201"> }</span> } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>