Content of file DefaultInheritanceHelper.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>DefaultInheritanceHelper.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</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">DefaultInheritanceHelper.java</span></div><h1>DefaultInheritanceHelper.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 java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.northernwind.core.model.ResourceFile;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * Implements the default inheritance policy for properties. The sequence of loaded file is:
 *
 * &lt;ul&gt;
 * &lt;li&gt;parent default&lt;/li&gt;
 * &lt;li&gt;parent localized&lt;/li&gt;
 * &lt;li&gt;this level default&lt;/li&gt;
 * &lt;li&gt;this level localized&lt;/li&gt;
 * &lt;li&gt;override default&lt;/li&gt;
 * &lt;li&gt;override localized&lt;/li&gt;
 * &lt;/ul&gt;
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L54">@Slf4j</span>
<span class="nc" id="L55">public class DefaultInheritanceHelper implements InheritanceHelper</span>
  {
    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public List&lt;ResourceFile&gt; getInheritedPropertyFiles (@Nonnull final ResourceFile folder,
                                                         @Nonnull final Locale locale,
                                                         @Nonnull final String propertyFileName)
      {
<span class="nc" id="L65">        log.trace(&quot;getInheritedPropertyFiles({}, {})&quot;, folder.getPath(), propertyFileName);</span>

<span class="nc" id="L67">        final List&lt;String&gt; suffixes = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L68">        suffixes.add(&quot;&quot;);</span>
<span class="nc" id="L69">        suffixes.add(&quot;_&quot; + locale.getLanguage());</span>
<span class="nc" id="L70">        final List&lt;ResourceFile&gt; files = new ArrayList&lt;&gt;();</span>

<span class="nc bnc" id="L72" title="All 2 branches missed.">        for (final var parentFolder : getHierarchyFolders(folder))</span>
          {
<span class="nc" id="L74">            files.addAll(getFiles(&quot;&quot;, propertyFileName, parentFolder, suffixes));</span>
<span class="nc" id="L75">          }</span>

<span class="nc" id="L77">        files.addAll(getFiles(&quot;Override&quot;, propertyFileName, folder, suffixes));</span>

<span class="nc" id="L79">        log.trace(&quot;&gt;&gt;&gt;&gt; property file candidates: {}&quot;, files);</span>

<span class="nc" id="L81">        return files;</span>
      }

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    private static List&lt;ResourceFile&gt; getFiles (@Nonnull final String prefix,
                                                @Nonnull final String propertyFileName,
                                                @Nonnull final ResourceFile folder,
                                                @Nonnull final List&lt;String&gt; suffixes)
      {
<span class="nc" id="L92">        final List&lt;ResourceFile&gt; files = new ArrayList&lt;&gt;();</span>

<span class="nc bnc" id="L94" title="All 2 branches missed.">        for (final var localeSuffix : suffixes)</span>
          {
<span class="nc" id="L96">            final var fileName = prefix + propertyFileName + localeSuffix + &quot;.xml&quot;;</span>
<span class="nc" id="L97">            log.trace(&quot;&gt;&gt;&gt;&gt; probing {} ...&quot;, folder.getPath().asString() + &quot;/&quot; + fileName);</span>

            try
              {
<span class="nc" id="L101">                files.add(folder.findChildren().withName(fileName).result());</span>
              }
<span class="nc" id="L103">            catch (NotFoundException e)</span>
              {
                // ok. do nothing
<span class="nc" id="L106">              }</span>
<span class="nc" id="L107">          }</span>

<span class="nc" id="L109">        return files;</span>
      }

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    private static List&lt;ResourceFile&gt; getHierarchyFolders (@Nonnull final ResourceFile folder)
      {
<span class="nc" id="L118">        final List&lt;ResourceFile&gt; folders = new ArrayList&lt;&gt;();</span>

<span class="nc bnc" id="L120" title="All 2 branches missed.">        for (var parent = folder; parent.getParent() != null; parent = parent.getParent()) // TODO: refactor with recursion?</span>
refactor with recursion?
{ <span class="nc" id="L122"> folders.add(parent);</span> } <span class="nc" id="L125"> Collections.reverse(folders);</span> <span class="nc" id="L127"> return folders;</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>