Skip to content

Method: XmlCalendarDao()

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