Content of file DiaryPhotoCollectionProvider.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>DiaryPhotoCollectionProvider.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">blueMarine II :: Application :: JavaFX</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-services-stoppingdown</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.service.stoppingdown.impl</a> &gt; <span class="el_source">DiaryPhotoCollectionProvider.java</span></div><h1>DiaryPhotoCollectionProvider.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * blueMarine II: Semantic Media Centre
 * http://tidalwave.it/projects/bluemarine2
 *
 * Copyright (C) 2015 - 2021 by 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.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/bluemarine2-src
 * git clone https://github.com/tidalwave-it/bluemarine2-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.bluemarine2.service.stoppingdown.impl;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.IntStream;
import java.io.IOException;
import java.nio.file.Paths;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import it.tidalwave.util.annotation.VisibleForTesting;
import it.tidalwave.bluemarine2.model.MediaFolder;
import it.tidalwave.bluemarine2.model.VirtualMediaFolder;
import it.tidalwave.bluemarine2.model.VirtualMediaFolder.EntityCollectionFactory;
import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
import it.tidalwave.bluemarine2.model.spi.PathAwareFinder;
import lombok.extern.slf4j.Slf4j;
import static java.util.Comparator.*;
import static java.util.stream.Collectors.*;
import static javax.xml.xpath.XPathConstants.NODESET;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L62">@Slf4j</span>
public class DiaryPhotoCollectionProvider extends PhotoCollectionProviderSupport
  {
    private static final String URL_DIARY_TEMPLATE = &quot;%s/diary/%d/&quot;;

    private static final String REGEXP_URL_HOST_AND_PORT = &quot;http:\\/\\/[^\\/]*&quot;;

    private static final XPathExpression XPATH_DIARY_EXPR;

    /**
     * A local cache for themes is advisable because multiple calls will be performed.
     */
<span class="fc" id="L74">    private final Map&lt;Integer, List&lt;GalleryDescription&gt;&gt; diaryCache = new ConcurrentHashMap&lt;&gt;();</span>

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    static
      {
        try
          {
<span class="fc" id="L83">            final XPath xpath = XPATH_FACTORY.newXPath();</span>
<span class="fc" id="L84">            XPATH_DIARY_EXPR = xpath.compile(&quot;//div[@class='nw-calendar']//li/a&quot;);</span>
          }
<span class="nc" id="L86">        catch (XPathExpressionException e)</span>
          {
<span class="nc" id="L88">            throw new ExceptionInInitializerError(e);</span>
<span class="fc" id="L89">          }</span>
<span class="fc" id="L90">      }</span>

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    public DiaryPhotoCollectionProvider()
      {
<span class="fc" id="L97">        this(URL_STOPPINGDOWN);</span>
<span class="fc" id="L98">      }</span>

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    public DiaryPhotoCollectionProvider (@Nonnull final String baseUrl)
      {
<span class="fc" id="L105">        super(baseUrl);</span>
<span class="fc" id="L106">      }</span>

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Override
    @Nonnull
    public PathAwareFinder findPhotos(@Nonnull final MediaFolder parent) {
<span class="fc" id="L114">        return parent.finderOf(</span>
<span class="fc" id="L115">                p1 -&gt; IntStream.range(1999, 2016 + 1) // FIXME: use current year</span>
use current year
<span class="fc" id="L116"> .boxed()</span> <span class="fc" id="L117"> .map(year -&gt; new VirtualMediaFolder(p1,</span> <span class="fc" id="L118"> Paths.get(&quot;&quot; + year),</span> &quot;&quot; + year, <span class="fc" id="L120"> (EntityCollectionFactory)(p2 -&gt; entriesFactory(p2, year))))</span> <span class="fc" id="L121"> .collect(toList()));</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override protected void clearCachesImpl() { <span class="fc" id="L132"> super.clearCachesImpl();</span> <span class="fc" id="L133"> diaryCache.clear();</span> <span class="fc" id="L134"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private Collection&lt;PathAwareEntity&gt; entriesFactory (@Nonnull final MediaFolder parent, final int year) { <span class="fc" id="L142"> return parseDiary(year).stream().map(gallery -&gt; gallery.createFolder(parent, this::findPhotos))</span> <span class="fc" id="L143"> .collect(toList());</span> } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull @VisibleForTesting List&lt;GalleryDescription&gt; parseDiary (final int year) { <span class="fc" id="L152"> final String diaryUrl = String.format(URL_DIARY_TEMPLATE, baseUrl, year);</span> <span class="fc" id="L153"> log.debug(&quot;parseDiary({})&quot;, diaryUrl);</span> <span class="fc" id="L155"> return diaryCache.computeIfAbsent(year, key -&gt;</span> { try { <span class="fc" id="L159"> final Document document = downloadXml(diaryUrl);</span> <span class="fc" id="L160"> final NodeList entryNodes = (NodeList)XPATH_DIARY_EXPR.evaluate(document, NODESET);</span> <span class="fc" id="L161"> final List&lt;GalleryDescription&gt; galleryDescriptions = new ArrayList&lt;&gt;();</span> <span class="fc bfc" id="L163" title="All 2 branches covered."> for (int i = 0; i &lt; entryNodes.getLength(); i++)</span> { <span class="fc" id="L165"> final Node entryNode = entryNodes.item(i);</span> <span class="fc" id="L166"> final String href = getAttribute(entryNode, &quot;href&quot;).replaceAll(REGEXP_URL_HOST_AND_PORT, &quot;&quot;);</span> <span class="fc" id="L167"> final String url = String.format(URL_GALLERY_TEMPLATE, baseUrl, href).replace(&quot;//&quot;, &quot;/&quot;)</span> <span class="fc" id="L168"> .replace(&quot;:/&quot;, &quot;://&quot;)</span> <span class="fc" id="L169"> .replaceAll(&quot;(^.*)\\/([0-9]{2})\\/([0-9]{2})\\/(.*)$&quot;, &quot;$1/$2-$3/$4&quot;);</span> <span class="fc" id="L170"> final String date = href.substring(href.length() - 11, href.length() - 1);</span> <span class="fc" id="L171"> final String displayName = date + &quot; - &quot; + entryNode.getTextContent();</span> <span class="fc" id="L172"> galleryDescriptions.add(new GalleryDescription(displayName, url));</span> } <span class="fc" id="L175"> galleryDescriptions.sort(comparing(GalleryDescription::getUrl));</span> <span class="fc" id="L177"> return galleryDescriptions;</span> } <span class="nc" id="L179"> catch (SAXException | IOException | XPathExpressionException | ParserConfigurationException e)</span> { <span class="nc" id="L181"> throw new RuntimeException(e);</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>