Content of file XmlCalendarDao.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>XmlCalendarDao.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.html" class="el_bundle">it-tidalwave-northernwind-frontend-components</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.ui.component.calendar.spi</a> &gt; <span class="el_source">XmlCalendarDao.java</span></div><h1>XmlCalendarDao.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.ui.component.calendar.spi;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.text.DateFormatSymbols;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.IntStream;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import it.tidalwave.northernwind.core.model.ResourcePath;
import it.tidalwave.northernwind.core.model.Site;
import it.tidalwave.northernwind.frontend.ui.component.calendar.DefaultCalendarViewController;
import it.tidalwave.northernwind.frontend.ui.component.calendar.DefaultCalendarViewController.Entry;
import static java.util.stream.Collectors.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L58">public class XmlCalendarDao implements CalendarDao</span>
  {
<span class="fc" id="L60">    private static final String[] SHORT_MONTH_NAMES = DateFormatSymbols.getInstance(Locale.ENGLISH).getShortMonths();</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public List&lt;Entry&gt; findMonthlyEntries (@Nonnull final Site site,
                                           @Nonnull final String entries,
                                           @Nonnegative final int month,
                                           @Nonnegative final int year)
      {
        try
          {
            // TODO: have them injected
have them injected
<span class="fc" id="L76"> final var dbf = DocumentBuilderFactory.newInstance();</span> <span class="fc" id="L77"> final var db = dbf.newDocumentBuilder();</span> <span class="fc" id="L78"> final var document = db.parse(new InputSource(new StringReader(entries)));</span> <span class="fc" id="L79"> final var xPathFactory = XPathFactory.newInstance();</span> <span class="fc" id="L80"> final var xPath = xPathFactory.newXPath();</span> <span class="fc" id="L82"> final var queryTemplate = &quot;/calendar/year[@id='%d']/month[@id='%s']/item&quot;;</span> <span class="fc" id="L83"> final var queryString = String.format(queryTemplate, year, SHORT_MONTH_NAMES[month - 1].toLowerCase());</span> <span class="fc" id="L84"> final var query = xPath.compile(queryString);</span> <span class="fc" id="L85"> final var nodes = (NodeList)query.evaluate(document, XPathConstants.NODESET);</span> <span class="fc" id="L86"> return IntStream.range(0, nodes.getLength()).mapToObj(i -&gt; toEntry(site, nodes.item(i), month)).collect(toList());</span> } <span class="nc" id="L88"> catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException e)</span> { <span class="nc" id="L90"> throw new RuntimeException(e);</span> } } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private static Entry toEntry (@Nonnull final Site site, @Nonnull final Node node, final int month) { <span class="fc" id="L100"> final var uri = node.getAttributes().getNamedItem(&quot;link&quot;).getNodeValue();</span> <span class="fc" id="L101"> final var type = Optional.ofNullable(node.getAttributes().getNamedItem(&quot;type&quot;)).map(Node::getNodeValue);</span> <span class="fc" id="L102"> final var link = site.createLink(ResourcePath.of(uri));</span> <span class="fc" id="L103"> final var name = node.getAttributes().getNamedItem(&quot;name&quot;).getNodeValue();</span> <span class="fc" id="L105"> return new DefaultCalendarViewController.Entry(month, name, link, type);</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>