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 (JavaFX)</a> > <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-rca-ui-content-editor</a> > <a href="index.source.html" class="el_package">it.tidalwave.northernwind.rca.ui.contenteditor.impl</a> > <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 "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.rca.ui.contenteditor.impl;
import javax.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import it.tidalwave.util.annotation.VisibleForTesting;
import org.springframework.core.io.ClassPathResource;
import it.tidalwave.util.Key;
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
*
**********************************************************************************************************************/
<span class="pc bpc" id="L49" title="1 of 2 branches missed.">@DciRole(datumType = ResourceProperties.class) @RequiredArgsConstructor</span>
public class ResourcePropertiesBinder implements PropertyBinder
{
private static final String EDITOR_TEMPLATE =
"it/tidalwave/northernwind/rca/ui/contenteditor/spi/EditorTemplate.xhtml";
@Nonnull
private final ResourceProperties properties;
@VisibleForTesting static final String EDITOR_PROLOG;
@VisibleForTesting static final String EDITOR_EPILOG;
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
static
{
<span class="fc" id="L69"> try (final BufferedReader r = new BufferedReader(new InputStreamReader(</span>
<span class="fc" id="L70"> new ClassPathResource(EDITOR_TEMPLATE).getInputStream(), StandardCharsets.UTF_8)))</span>
{
<span class="fc" id="L72"> final StringBuilder prologBuilder = new StringBuilder();</span>
<span class="fc" id="L73"> final StringBuilder epilogBuilder = new StringBuilder();</span>
<span class="fc" id="L74"> boolean prolog = true;</span>
<span class="fc bfc" id="L76" title="All 2 branches covered."> for (String line = r.readLine(); line != null; line = r.readLine())</span>
{
<span class="fc bfc" id="L78" title="All 2 branches covered."> if ("<!-- split here -->".equals(line.trim()))</span>
{
<span class="fc" id="L80"> prolog = false;</span>
<span class="fc" id="L81"> continue;</span>
}
<span class="fc bfc" id="L84" title="All 2 branches covered."> (prolog ? prologBuilder : epilogBuilder).append(line).append("\n");</span>
}
<span class="fc" id="L87"> EDITOR_PROLOG = prologBuilder.toString();</span>
<span class="fc" id="L88"> EDITOR_EPILOG = epilogBuilder.toString();</span>
}
<span class="nc" id="L90"> catch (IOException e)</span>
{
<span class="nc" id="L92"> throw new ExceptionInInitializerError(e);</span>
<span class="fc" id="L93"> }</span>
<span class="fc" id="L94"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public <T> void bind (@Nonnull final Key<T> propertyName,
@Nonnull final BoundProperty<T> boundProperty,
@Nonnull final UpdateCallback callback)
{
<span class="fc" id="L106"> properties.getProperty(propertyName).ifPresent(boundProperty::set);</span>
<span class="fc" id="L107"> boundProperty.addPropertyChangeListener(</span>
<span class="fc" id="L108"> event -> callback.notify(properties.withProperty(propertyName, boundProperty.get())));</span>
<span class="fc" id="L109"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public EmbeddedServer.Document createBoundDocument (@Nonnull final Key<String> propertyName,
@Nonnull final UpdateCallback callback)
{
<span class="fc" id="L120"> final String text = properties.getProperty(propertyName).orElse("");</span>
<span class="fc" id="L121"> final HtmlDocument originalDocument = HtmlDocument.createFromText(text);</span>
<span class="fc" id="L122"> final HtmlDocument editableDocument = originalDocument.withProlog(EDITOR_PROLOG)</span>
<span class="fc" id="L123"> .withEpilog(EDITOR_EPILOG);</span>
// FIXME: mime type - XHTML?
<span class="fc" id="L125"> return new EmbeddedServer.Document().withMimeType("text/html")</span>
<span class="fc" id="L126"> .withContent(editableDocument.asString())</span>
<span class="fc" id="L127"> .withUpdateListener(</span>
<span class="fc" id="L128"> text1 -> callback.notify(properties.withProperty(propertyName,</span>
<span class="fc" id="L129"> 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.7.202105040129</span></div></body></html>