Content of file DefaultMetadata.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>DefaultMetadata.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 :: Frontend :: Media</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.media.impl</a> &gt; <span class="el_source">DefaultMetadata.java</span></div><h1>DefaultMetadata.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.frontend.media.impl;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.springframework.beans.factory.annotation.Configurable;
import it.tidalwave.util.Key;
import it.tidalwave.image.EditableImage;
import it.tidalwave.image.metadata.Directory;
import it.tidalwave.image.metadata.EXIF;
import it.tidalwave.image.metadata.IPTC;
import it.tidalwave.image.metadata.TIFF;
import it.tidalwave.image.metadata.XMP;
import it.tidalwave.northernwind.core.model.ResourceProperties;
import it.tidalwave.northernwind.frontend.media.impl.interpolator.MetadataInterpolator.Context;
import it.tidalwave.northernwind.frontend.media.impl.interpolator.MetadataInterpolatorFactory;
import lombok.AllArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import static java.util.Collections.emptyList;
import static it.tidalwave.util.FunctionalCheckedExceptionWrappers.*;
import static it.tidalwave.northernwind.frontend.media.impl.EmbeddedMediaMetadataProvider.*;

/***********************************************************************************************************************
 *
 * A default implementation of {@link Metadata}.
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="pc bnc" id="L60" title="All 22 branches missed.">@Configurable @AllArgsConstructor @ToString @Slf4j</span>
class DefaultMetadata implements Metadata
  {
    @Nonnull
    private final String mediaName;

    @Nonnull
    private final EditableImage image;

    @Inject
    private MetadataInterpolatorFactory interpolatorFactory;

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public DefaultMetadata (@Nonnull final String mediaName, @Nonnull final EditableImage image)
<span class="pc bpc" id="L78" title="9 of 18 branches missed.">      {</span>
<span class="fc" id="L79">        this.mediaName = mediaName;</span>
<span class="fc" id="L80">        this.image = image;</span>
<span class="pc bpc" id="L81" title="2 of 4 branches missed.">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public &lt;T extends Directory&gt; T getDirectory (@Nonnull final Class&lt;T&gt; metadataClass)
      {
<span class="pc" id="L91">        return image.getMetadata(metadataClass).orElseGet(_s(() -&gt; metadataClass.getConstructor().newInstance()));</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public String interpolateString (@Nonnull final String template,
                                     @Nonnull final ResourceProperties properties)
      {
<span class="pc bpc" id="L103" title="1 of 2 branches missed.">        if (log.isDebugEnabled())</span>
          {
<span class="fc" id="L105">            log();</span>
          }

        // FIXME: use format as an interpolated string to get properties both from EXIF and IPTC
use format as an interpolated string to get properties both from EXIF and IPTC
// final String string = formatted(iptc.getObject(517, String.class)); <span class="fc" id="L110"> final var context = new Context(this, getMap(properties, P_CAMERA_IDS), getMap(properties, P_LENS_IDS));</span> <span class="fc" id="L112"> var result = template;</span> <span class="fc bfc" id="L114" title="All 2 branches covered."> for (final var interpolator : interpolatorFactory.getInterpolators())</span> { <span class="fc bfc" id="L116" title="All 2 branches covered."> if (result.contains(&quot;$&quot; + interpolator.getMacro() + &quot;$&quot;))</span> { <span class="fc" id="L118"> result = interpolator.interpolate(result, context);</span> } <span class="fc" id="L120"> }</span> <span class="fc" id="L122"> return result;</span> } /******************************************************************************************************************* * ******************************************************************************************************************/ private void log() { <span class="fc" id="L130"> final var tiff = image.getMetadata(TIFF.class).orElseGet(TIFF::new);</span> <span class="fc" id="L131"> final var exif = image.getMetadata(EXIF.class).orElseGet(EXIF::new);</span> <span class="fc" id="L132"> final var iptc = image.getMetadata(IPTC.class).orElseGet(IPTC::new);</span> <span class="fc" id="L133"> final var xmp = image.getMetadata(XMP.class).orElseGet(XMP::new);</span> <span class="fc" id="L134"> final Map&lt;String, String&gt; xmpProperties = new TreeMap&lt;&gt;(xmp.getXmpProperties());</span> <span class="fc" id="L136"> tiff.forEachTag(t -&gt; log.debug(&quot;{}: TIFF[{}]: {}&quot;, mediaName, t.getName(), tiff.getRaw(t.getCode())));</span> <span class="fc" id="L137"> exif.forEachTag(t -&gt; log.debug(&quot;{}: EXIF[{}]: {}&quot;, mediaName, t.getName(), exif.getRaw(t.getCode())));</span> <span class="fc" id="L138"> iptc.forEachTag(t -&gt; log.debug(&quot;{}: IPTC[{}]: {}&quot;, mediaName, t.getName(), iptc.getRaw(t.getCode())));</span> <span class="fc" id="L139"> xmp.forEachTag(t -&gt; log.debug(&quot;{}: XMP[{}]: {}&quot;, mediaName, t.getName(), xmp.getRaw(t.getCode())));</span> <span class="fc" id="L140"> xmpProperties.forEach((k, v) -&gt; log.debug(&quot;XMPprop({}).{}: {}&quot;, mediaName, k, v));</span> <span class="fc" id="L141"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private static Map&lt;String, String&gt; getMap (@Nonnull final ResourceProperties siteNodeProperties, @Nonnull final Key&lt;List&lt;String&gt;&gt; key) { <span class="fc" id="L150"> final var properties = siteNodeProperties.getGroup(P_GROUP_ID);</span> <span class="fc" id="L151"> final Map&lt;String, String&gt; lensMap = new HashMap&lt;&gt;();</span> <span class="fc bfc" id="L153" title="All 2 branches covered."> for (final var s : properties.getProperty(key).orElse(emptyList()))</span> { <span class="fc" id="L155"> final var split = s.split(&quot;:&quot;);</span> <span class="fc" id="L156"> lensMap.put(split[0].trim(), split[1].trim());</span> <span class="fc" id="L157"> }</span> <span class="fc" id="L159"> return lensMap;</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>