Skip to content

Content of file ResourcePropertiesBinder.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>ResourcePropertiesBinder.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">Zephyr - UI Content Editor</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.rca.ui.contenteditor.impl</a> &gt; <span class="el_source">ResourcePropertiesBinder.java</span></div><h1>ResourcePropertiesBinder.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 *
 * NorthernWind - lightweight CMS
 * http://northernwind.tidalwave.it - git clone git@bitbucket.org:tidalwave/northernwind-rca-src.git
 * %%
 * Copyright (C) 2013 - 2021 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.rca.ui.contenteditor.impl;

import javax.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.core.io.ClassPathResource;
import it.tidalwave.util.Key;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.role.ui.BoundProperty;
import it.tidalwave.dci.annotation.DciRole;
import it.tidalwave.northernwind.core.model.ResourceProperties;
import it.tidalwave.northernwind.rca.embeddedserver.EmbeddedServer;
import it.tidalwave.northernwind.rca.ui.contenteditor.spi.PropertyBinder;
import lombok.RequiredArgsConstructor;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
@DciRole(datumType = ResourceProperties.class)
<span class="pc bpc" id="L50" title="1 of 2 branches missed.">@Configurable @RequiredArgsConstructor</span>
public class ResourcePropertiesBinder implements PropertyBinder
  {
    private final static String EDITOR_TEMPLATE =
            &quot;it/tidalwave/northernwind/rca/ui/contenteditor/spi/EditorTemplate.xhtml&quot;;

    @Nonnull
    private final ResourceProperties properties;

    /* visible for testing */ final static String EDITOR_PROLOG;

    /* visible for testing */ final static String EDITOR_EPILOG;

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    static
      {
<span class="fc" id="L70">        try (final BufferedReader r = new BufferedReader(new InputStreamReader(</span>
<span class="fc" id="L71">                    new ClassPathResource(EDITOR_TEMPLATE).getInputStream(), &quot;UTF-8&quot;)))</span>
          {
<span class="fc" id="L73">            final StringBuilder prologBuilder = new StringBuilder();</span>
<span class="fc" id="L74">            final StringBuilder epilogBuilder = new StringBuilder();</span>
<span class="fc" id="L75">            boolean prolog = true;</span>

<span class="fc bfc" id="L77" title="All 2 branches covered.">            for (String line = r.readLine(); line != null; line = r.readLine())</span>
              {
<span class="fc bfc" id="L79" title="All 2 branches covered.">                if (line.trim().equals(&quot;&lt;!-- split here --&gt;&quot;))</span>
                  {
<span class="fc" id="L81">                    prolog = false;</span>
<span class="fc" id="L82">                    continue;</span>
                  }

<span class="fc bfc" id="L85" title="All 2 branches covered.">                (prolog ? prologBuilder : epilogBuilder).append(line).append(&quot;\n&quot;);</span>
              }

<span class="fc" id="L88">            EDITOR_PROLOG = prologBuilder.toString();</span>
<span class="fc" id="L89">            EDITOR_EPILOG = epilogBuilder.toString();</span>
          }
<span class="nc" id="L91">        catch (Exception e)</span>
          {
<span class="nc" id="L93">            throw new ExceptionInInitializerError(e);</span>
<span class="fc" id="L94">          }</span>
<span class="fc" id="L95">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public &lt;T&gt; void bind (final @Nonnull Key&lt;T&gt; propertyName,
                          final @Nonnull BoundProperty&lt;T&gt; boundProperty,
                          final @Nonnull UpdateCallback callback)
      {
<span class="fc" id="L107">        properties.getProperty(propertyName).ifPresent(boundProperty::set);</span>
<span class="fc" id="L108">        boundProperty.addPropertyChangeListener(</span>
<span class="fc" id="L109">                event -&gt; callback.notify(properties.withProperty(propertyName, boundProperty.get())));</span>
<span class="fc" id="L110">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public EmbeddedServer.Document createBoundDocument (final @Nonnull Key&lt;String&gt; propertyName,
                                                        final @Nonnull UpdateCallback callback)
      {
<span class="fc" id="L121">        final String text = properties.getProperty(propertyName).orElse(&quot;&quot;);</span>
<span class="fc" id="L122">        final HtmlDocument originalDocument = HtmlDocument.createFromText(text);</span>
<span class="fc" id="L123">        final HtmlDocument editableDocument = originalDocument.withProlog(EDITOR_PROLOG)</span>
<span class="fc" id="L124">                                                              .withEpilog(EDITOR_EPILOG);</span>
        // FIXME: mime type - XHTML?
mime type - XHTML?
<span class="fc" id="L126"> return new EmbeddedServer.Document().withMimeType(&quot;text/html&quot;)</span> <span class="fc" id="L127"> .withContent(editableDocument.asString())</span> <span class="fc" id="L128"> .withUpdateListener(</span> <span class="fc" id="L129"> text1 -&gt; callback.notify(properties.withProperty(propertyName,</span> <span class="fc" id="L130"> originalDocument.withBody(text1).asString())));</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.6.202009150832</span></div></body></html>