]> git.basschouten.com Git - openhab-addons.git/blob
d57c0b3f848db43b00cab0318ceb0d528b0509e5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.avmfritz.internal.util;
14
15 import javax.xml.bind.JAXBContext;
16 import javax.xml.bind.JAXBException;
17 import javax.xml.stream.XMLInputFactory;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.avmfritz.internal.dto.DeviceListModel;
22 import org.openhab.binding.avmfritz.internal.dto.templates.TemplateListModel;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Implementation for a static use of JAXBContext as singleton instance.
28  *
29  * @author Christoph Weitkamp - Initial contribution
30  */
31 @NonNullByDefault
32 public class JAXBUtils {
33
34     private static final Logger LOGGER = LoggerFactory.getLogger(JAXBUtils.class);
35
36     public static final @Nullable JAXBContext JAXBCONTEXT_DEVICES = initJAXBContextDevices();
37     public static final @Nullable JAXBContext JAXBCONTEXT_TEMPLATES = initJAXBContextTemplates();
38     public static final XMLInputFactory XMLINPUTFACTORY = initXMLInputFactory();
39
40     private static @Nullable JAXBContext initJAXBContextDevices() {
41         try {
42             return JAXBContext.newInstance(DeviceListModel.class);
43         } catch (JAXBException e) {
44             LOGGER.error("Exception creating JAXBContext for devices: {}", e.getLocalizedMessage(), e);
45             return null;
46         }
47     }
48
49     private static @Nullable JAXBContext initJAXBContextTemplates() {
50         try {
51             return JAXBContext.newInstance(TemplateListModel.class);
52         } catch (JAXBException e) {
53             LOGGER.error("Exception creating JAXBContext for templates: {}", e.getLocalizedMessage(), e);
54             return null;
55         }
56     }
57
58     private static XMLInputFactory initXMLInputFactory() {
59         XMLInputFactory xif = XMLInputFactory.newInstance();
60         xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
61         xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
62         return xif;
63     }
64 }