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 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="L47" title="1 of 2 branches missed.">@DciRole(datumType = ResourceProperties.class) @RequiredArgsConstructor</span>
public class ResourcePropertiesBinder implements PropertyBinder
{
private final static String EDITOR_TEMPLATE =
"it/tidalwave/northernwind/rca/ui/contenteditor/spi/EditorTemplate.xhtml";
@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="L67"> try (final BufferedReader r = new BufferedReader(new InputStreamReader(</span>
<span class="fc" id="L68"> new ClassPathResource(EDITOR_TEMPLATE).getInputStream(), "UTF-8")))</span>
{
<span class="fc" id="L70"> final StringBuilder prologBuilder = new StringBuilder();</span>
<span class="fc" id="L71"> final StringBuilder epilogBuilder = new StringBuilder();</span>
<span class="fc" id="L72"> boolean prolog = true;</span>
<span class="fc bfc" id="L74" title="All 2 branches covered."> for (String line = r.readLine(); line != null; line = r.readLine())</span>
{
<span class="fc bfc" id="L76" title="All 2 branches covered."> if (line.trim().equals("<!-- split here -->"))</span>
{
<span class="fc" id="L78"> prolog = false;</span>
<span class="fc" id="L79"> continue;</span>
}
<span class="fc bfc" id="L82" title="All 2 branches covered."> (prolog ? prologBuilder : epilogBuilder).append(line).append("\n");</span>
}
<span class="fc" id="L85"> EDITOR_PROLOG = prologBuilder.toString();</span>
<span class="fc" id="L86"> EDITOR_EPILOG = epilogBuilder.toString();</span>
}
<span class="nc" id="L88"> catch (IOException e)</span>
{
<span class="nc" id="L90"> throw new ExceptionInInitializerError(e);</span>
<span class="fc" id="L91"> }</span>
<span class="fc" id="L92"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public <T> void bind (final @Nonnull Key<T> propertyName,
final @Nonnull BoundProperty<T> boundProperty,
final @Nonnull UpdateCallback callback)
{
<span class="fc" id="L104"> properties.getProperty(propertyName).ifPresent(boundProperty::set);</span>
<span class="fc" id="L105"> boundProperty.addPropertyChangeListener(</span>
<span class="fc" id="L106"> event -> callback.notify(properties.withProperty(propertyName, boundProperty.get())));</span>
<span class="fc" id="L107"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public EmbeddedServer.Document createBoundDocument (final @Nonnull Key<String> propertyName,
final @Nonnull UpdateCallback callback)
{
<span class="fc" id="L118"> final String text = properties.getProperty(propertyName).orElse("");</span>
<span class="fc" id="L119"> final HtmlDocument originalDocument = HtmlDocument.createFromText(text);</span>
<span class="fc" id="L120"> final HtmlDocument editableDocument = originalDocument.withProlog(EDITOR_PROLOG)</span>
<span class="fc" id="L121"> .withEpilog(EDITOR_EPILOG);</span>
// FIXME: mime type - XHTML?
<span class="fc" id="L123"> return new EmbeddedServer.Document().withMimeType("text/html")</span>
<span class="fc" id="L124"> .withContent(editableDocument.asString())</span>
<span class="fc" id="L125"> .withUpdateListener(</span>
<span class="fc" id="L126"> text1 -> callback.notify(properties.withProperty(propertyName,</span>
<span class="fc" id="L127"> 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>