Skip to content

Package: XmlCalendarDao

XmlCalendarDao

nameinstructionbranchcomplexitylinemethod
XmlCalendarDao()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
findMonthlyEntries(Site, String, int, int)
M: 6 C: 64
91%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 10
83%
M: 0 C: 1
100%
lambda$findMonthlyEntries$0(Site, NodeList, int, int)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toEntry(Site, Node, int)
M: 0 C: 33
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.frontend.ui.component.calendar.spi;
28:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import java.text.DateFormatSymbols;
32: import java.util.List;
33: import java.util.Locale;
34: import java.util.Optional;
35: import java.util.stream.IntStream;
36: import java.io.IOException;
37: import java.io.StringReader;
38: import javax.xml.parsers.DocumentBuilderFactory;
39: import javax.xml.parsers.ParserConfigurationException;
40: import javax.xml.xpath.XPathConstants;
41: import javax.xml.xpath.XPathExpressionException;
42: import javax.xml.xpath.XPathFactory;
43: import org.w3c.dom.Node;
44: import org.w3c.dom.NodeList;
45: import org.xml.sax.InputSource;
46: import org.xml.sax.SAXException;
47: import it.tidalwave.northernwind.core.model.ResourcePath;
48: import it.tidalwave.northernwind.core.model.Site;
49: import it.tidalwave.northernwind.frontend.ui.component.calendar.DefaultCalendarViewController;
50: import it.tidalwave.northernwind.frontend.ui.component.calendar.DefaultCalendarViewController.Entry;
51: import static java.util.stream.Collectors.*;
52:
53: /***********************************************************************************************************************
54: *
55: * @author Fabrizio Giudici
56: *
57: **********************************************************************************************************************/
58: public class XmlCalendarDao implements CalendarDao
59: {
60: private static final String[] SHORT_MONTH_NAMES = DateFormatSymbols.getInstance(Locale.ENGLISH).getShortMonths();
61:
62: /*******************************************************************************************************************
63: *
64: * {@inheritDoc}
65: *
66: ******************************************************************************************************************/
67: @Override @Nonnull
68: public List<Entry> findMonthlyEntries (@Nonnull final Site site,
69: @Nonnull final String entries,
70: @Nonnegative final int month,
71: @Nonnegative final int year)
72: {
73: try
74: {
75: // TODO: have them injected
76: final var dbf = DocumentBuilderFactory.newInstance();
77: final var db = dbf.newDocumentBuilder();
78: final var document = db.parse(new InputSource(new StringReader(entries)));
79: final var xPathFactory = XPathFactory.newInstance();
80: final var xPath = xPathFactory.newXPath();
81:
82: final var queryTemplate = "/calendar/year[@id='%d']/month[@id='%s']/item";
83: final var queryString = String.format(queryTemplate, year, SHORT_MONTH_NAMES[month - 1].toLowerCase());
84: final var query = xPath.compile(queryString);
85: final var nodes = (NodeList)query.evaluate(document, XPathConstants.NODESET);
86: return IntStream.range(0, nodes.getLength()).mapToObj(i -> toEntry(site, nodes.item(i), month)).collect(toList());
87: }
88: catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException e)
89: {
90: throw new RuntimeException(e);
91: }
92: }
93:
94: /*******************************************************************************************************************
95: *
96: ******************************************************************************************************************/
97: @Nonnull
98: private static Entry toEntry (@Nonnull final Site site, @Nonnull final Node node, final int month)
99: {
100: final var uri = node.getAttributes().getNamedItem("link").getNodeValue();
101: final var type = Optional.ofNullable(node.getAttributes().getNamedItem("type")).map(Node::getNodeValue);
102: final var link = site.createLink(ResourcePath.of(uri));
103: final var name = node.getAttributes().getNamedItem("name").getNodeValue();
104:
105: return new DefaultCalendarViewController.Entry(month, name, link, type);
106: }
107: }