]> git.basschouten.com Git - openhab-addons.git/blob
210e481cebf3bb8718ab5b8fd1c6775f36a5b0c8
[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.roku.internal.communication;
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.roku.internal.dto.ActiveApp;
22 import org.openhab.binding.roku.internal.dto.Apps;
23 import org.openhab.binding.roku.internal.dto.DeviceInfo;
24 import org.openhab.binding.roku.internal.dto.Player;
25 import org.openhab.binding.roku.internal.dto.TvChannel;
26 import org.openhab.binding.roku.internal.dto.TvChannels;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Implementation for a static use of JAXBContext as singleton instance.
32  *
33  * @author Michael Lobstein - Initial contribution
34  */
35 @NonNullByDefault
36 public class JAXBUtils {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(JAXBUtils.class);
39
40     public static final @Nullable JAXBContext JAXBCONTEXT_ACTIVE_APP = initJAXBContextActiveApp();
41     public static final @Nullable JAXBContext JAXBCONTEXT_APPS = initJAXBContextApps();
42     public static final @Nullable JAXBContext JAXBCONTEXT_DEVICE_INFO = initJAXBContextDeviceInfo();
43     public static final @Nullable JAXBContext JAXBCONTEXT_PLAYER = initJAXBContextPlayer();
44     public static final @Nullable JAXBContext JAXBCONTEXT_TVCHANNEL = initJAXBContextTvChannel();
45     public static final @Nullable JAXBContext JAXBCONTEXT_TVCHANNELS = initJAXBContextTvChannels();
46     public static final XMLInputFactory XMLINPUTFACTORY = initXMLInputFactory();
47
48     private static @Nullable JAXBContext initJAXBContextActiveApp() {
49         try {
50             return JAXBContext.newInstance(ActiveApp.class);
51         } catch (JAXBException e) {
52             LOGGER.error("Exception creating JAXBContext for active app: {}", e.getLocalizedMessage(), e);
53             return null;
54         }
55     }
56
57     private static @Nullable JAXBContext initJAXBContextApps() {
58         try {
59             return JAXBContext.newInstance(Apps.class);
60         } catch (JAXBException e) {
61             LOGGER.error("Exception creating JAXBContext for app list: {}", e.getLocalizedMessage(), e);
62             return null;
63         }
64     }
65
66     private static @Nullable JAXBContext initJAXBContextDeviceInfo() {
67         try {
68             return JAXBContext.newInstance(DeviceInfo.class);
69         } catch (JAXBException e) {
70             LOGGER.error("Exception creating JAXBContext for device info: {}", e.getLocalizedMessage(), e);
71             return null;
72         }
73     }
74
75     private static @Nullable JAXBContext initJAXBContextPlayer() {
76         try {
77             return JAXBContext.newInstance(Player.class);
78         } catch (JAXBException e) {
79             LOGGER.error("Exception creating JAXBContext for player info: {}", e.getLocalizedMessage(), e);
80             return null;
81         }
82     }
83
84     private static @Nullable JAXBContext initJAXBContextTvChannel() {
85         try {
86             return JAXBContext.newInstance(TvChannel.class);
87         } catch (JAXBException e) {
88             LOGGER.error("Exception creating JAXBContext for TvChannel info: {}", e.getLocalizedMessage(), e);
89             return null;
90         }
91     }
92
93     private static @Nullable JAXBContext initJAXBContextTvChannels() {
94         try {
95             return JAXBContext.newInstance(TvChannels.class);
96         } catch (JAXBException e) {
97             LOGGER.error("Exception creating JAXBContext for TvChannels info: {}", e.getLocalizedMessage(), e);
98             return null;
99         }
100     }
101
102     private static XMLInputFactory initXMLInputFactory() {
103         XMLInputFactory xif = XMLInputFactory.newInstance();
104         xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
105         xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
106         return xif;
107     }
108 }