]> git.basschouten.com Git - openhab-addons.git/blob
57596177a6856ee2d6b6083da2a5ea0d753d1413
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Implementation for a static use of JAXBContext as singleton instance.
30  *
31  * @author Michael Lobstein - Initial contribution
32  */
33 @NonNullByDefault
34 public class JAXBUtils {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(JAXBUtils.class);
37
38     public static final @Nullable JAXBContext JAXBCONTEXT_ACTIVE_APP = initJAXBContextActiveApp();
39     public static final @Nullable JAXBContext JAXBCONTEXT_APPS = initJAXBContextApps();
40     public static final @Nullable JAXBContext JAXBCONTEXT_DEVICE_INFO = initJAXBContextDeviceInfo();
41     public static final @Nullable JAXBContext JAXBCONTEXT_PLAYER = initJAXBContextPlayer();
42     public static final XMLInputFactory XMLINPUTFACTORY = initXMLInputFactory();
43
44     private static @Nullable JAXBContext initJAXBContextActiveApp() {
45         try {
46             return JAXBContext.newInstance(ActiveApp.class);
47         } catch (JAXBException e) {
48             LOGGER.error("Exception creating JAXBContext for active app: {}", e.getLocalizedMessage(), e);
49             return null;
50         }
51     }
52
53     private static @Nullable JAXBContext initJAXBContextApps() {
54         try {
55             return JAXBContext.newInstance(Apps.class);
56         } catch (JAXBException e) {
57             LOGGER.error("Exception creating JAXBContext for app list: {}", e.getLocalizedMessage(), e);
58             return null;
59         }
60     }
61
62     private static @Nullable JAXBContext initJAXBContextDeviceInfo() {
63         try {
64             return JAXBContext.newInstance(DeviceInfo.class);
65         } catch (JAXBException e) {
66             LOGGER.error("Exception creating JAXBContext for device info: {}", e.getLocalizedMessage(), e);
67             return null;
68         }
69     }
70
71     private static @Nullable JAXBContext initJAXBContextPlayer() {
72         try {
73             return JAXBContext.newInstance(Player.class);
74         } catch (JAXBException e) {
75             LOGGER.error("Exception creating JAXBContext for player info: {}", e.getLocalizedMessage(), e);
76             return null;
77         }
78     }
79
80     private static XMLInputFactory initXMLInputFactory() {
81         XMLInputFactory xif = XMLInputFactory.newInstance();
82         xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
83         xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
84         return xif;
85     }
86 }