Content of file DefaultResource.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>DefaultResource.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 :: Filesystems :: SCM :: Mercurial</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-core-default</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.core.impl.model</a> &gt; <span class="el_source">DefaultResource.java</span></div><h1>DefaultResource.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.core.impl.model;

import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Configurable;
import it.tidalwave.northernwind.core.model.RequestLocaleManager;
import it.tidalwave.northernwind.core.model.Resource;
import it.tidalwave.northernwind.core.model.ResourceProperties;
import it.tidalwave.northernwind.core.model.spi.ResourceSupport;
import lombok.Cleanup;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.role.io.Unmarshallable._Unmarshallable_;

/***********************************************************************************************************************
 *
 * The default implementation for {@link Resource}.
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L54">@Configurable @Slf4j @ToString(callSuper = true, of = &quot;placeHolder&quot;)</span>
/* package */ class DefaultResource extends ResourceSupport
  {
    @Inject
    private InheritanceHelper inheritanceHelper;

    @Inject
    private RequestLocaleManager localeRequestManager;

<span class="nc" id="L63">    private final Map&lt;Locale, ResourceProperties&gt; propertyMapByLocale = new HashMap&lt;&gt;();</span>

<span class="nc" id="L65">    @Getter</span>
    private boolean placeHolder;

    private final ResourceProperties.PropertyResolver propertyResolver;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public DefaultResource (@Nonnull final Resource.Builder builder)
      {
<span class="nc bnc" id="L76" title="All 18 branches missed.">        super(builder);</span>
<span class="nc" id="L77">        propertyResolver = new TextResourcePropertyResolver(getFile());</span>
<span class="nc bnc" id="L78" title="All 4 branches missed.">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public ResourceProperties getProperties()
      {
<span class="nc" id="L88">        return propertyMapByLocale.get(localeRequestManager.getLocales().get(0));</span>
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @PostConstruct
    /* package */ void loadProperties()
      throws IOException
      {
<span class="nc" id="L99">        final var file = getFile();</span>
<span class="nc" id="L100">        log.debug(&quot;loadProperties() for {}&quot;, file.getPath().asString());</span>

<span class="nc" id="L102">        var tmpPlaceHolder = true;</span>

<span class="nc bnc" id="L104" title="All 2 branches missed.">        for (final var locale : localeRequestManager.getLocales())</span>
          {
<span class="nc" id="L106">            var properties = modelFactory.createProperties().withPropertyResolver(propertyResolver).build();</span>

<span class="nc bnc" id="L108" title="All 2 branches missed.">            if (file.isData())</span>
              {
<span class="nc" id="L110">                tmpPlaceHolder = false;</span>
              }
            else
              {
<span class="nc bnc" id="L114" title="All 2 branches missed.">                for (final var propertyFile : inheritanceHelper.getInheritedPropertyFiles(file, locale, &quot;Properties&quot;))</span>
                  {
<span class="nc" id="L116">                    log.trace(&quot;&gt;&gt;&gt;&gt; reading properties from {} ({})...&quot;, propertyFile.getPath().asString(), locale);</span>
<span class="nc bnc" id="L117" title="All 2 branches missed.">                    @Cleanup final var is = propertyFile.getInputStream();</span>
<span class="nc" id="L118">                    final ResourceProperties tempProperties =</span>
<span class="nc" id="L119">                        modelFactory.createProperties().build().as(_Unmarshallable_).unmarshal(is);</span>
        //                modelFactory.createProperties().withPropertyResolver(propertyResolver).build().as(_Unmarshallable_).unmarshal(is);
<span class="nc" id="L121">                    log.trace(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; read properties: {} ({})&quot;, tempProperties, locale);</span>
<span class="nc" id="L122">                    properties = properties.merged(tempProperties);</span>
<span class="nc bnc" id="L123" title="All 2 branches missed.">                    tmpPlaceHolder &amp;= !propertyFile.getParent().equals(file);</span>
<span class="nc" id="L124">                  }</span>
              }

<span class="nc" id="L127">            placeHolder = properties.getProperty(P_PLACE_HOLDER).orElse(tmpPlaceHolder);</span>

<span class="nc bnc" id="L129" title="All 2 branches missed.">            if (log.isDebugEnabled())</span>
              {
<span class="nc" id="L131">                log.debug(&quot;&gt;&gt;&gt;&gt; properties for {} ({}):&quot;, file.getPath().asString(), locale);</span>
<span class="nc" id="L132">                logProperties(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;, properties);</span>
              }

<span class="nc" id="L135">            propertyMapByLocale.put(locale, properties);</span>
<span class="nc" id="L136">          }</span>
<span class="nc" id="L137">      }</span>

    /*******************************************************************************************************************
     *
     * FIXME: move to ResourceProperties!
move to ResourceProperties!
* ******************************************************************************************************************/ private static void logProperties (@Nonnull final String indent, @Nonnull final ResourceProperties properties) { <span class="nc" id="L147"> log.debug(&quot;{} property items:&quot;, indent);</span> <span class="nc bnc" id="L149" title="All 2 branches missed."> for (final var key : properties.getKeys())</span> { <span class="nc" id="L151"> log.debug(&quot;{}&gt;&gt;&gt;&gt; {} = {}&quot;, indent, key, properties.getProperty(key));</span> <span class="nc" id="L152"> }</span> <span class="nc" id="L154"> log.debug(&quot;{} property groups: {}&quot;, indent, properties.getGroupIds());</span> <span class="nc bnc" id="L156" title="All 2 branches missed."> for (final var groupId : properties.getGroupIds())</span> { <span class="nc" id="L158"> log.debug(&quot;{}&gt;&gt;&gt;&gt; group: {}&quot;, indent, groupId);</span> <span class="nc" id="L159"> logProperties(indent + &quot;&gt;&gt;&gt;&gt;&quot;, properties.getGroup(groupId));</span> <span class="nc" id="L160"> }</span> <span class="nc" id="L161"> }</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>