]> git.basschouten.com Git - openhab-addons.git/blob
02aaa95f29386df26d7f1655b50df51862e81bee
[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.xmltv.internal;
14
15 import static org.openhab.binding.xmltv.internal.XmlTVBindingConstants.*;
16
17 import javax.xml.bind.JAXBContext;
18 import javax.xml.bind.JAXBException;
19 import javax.xml.bind.Unmarshaller;
20 import javax.xml.stream.XMLInputFactory;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.xmltv.internal.handler.ChannelHandler;
25 import org.openhab.binding.xmltv.internal.handler.XmlTVHandler;
26 import org.openhab.binding.xmltv.internal.jaxb.Tv;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.i18n.TimeZoneProvider;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39
40 /**
41  * The {@link XmlTVHandlerFactory} is responsible for creating things and thing
42  * handlers.
43  *
44  * @author GaĆ«l L'hopital - Initial contribution
45  */
46 @NonNullByDefault
47 @Component(configurationPid = "binding.xmltv", service = ThingHandlerFactory.class)
48 public class XmlTVHandlerFactory extends BaseThingHandlerFactory {
49     private final XMLInputFactory xif = XMLInputFactory.newFactory();
50     private final TimeZoneProvider timeZoneProvider;
51     private final Unmarshaller unmarshaller;
52
53     @Activate
54     public XmlTVHandlerFactory(final @Reference TimeZoneProvider timeZoneProvider) throws JAXBException {
55         this.timeZoneProvider = timeZoneProvider;
56         this.unmarshaller = JAXBContext.newInstance(Tv.class).createUnmarshaller();
57         xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
63     }
64
65     @Override
66     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
67             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
68         if (XMLTV_FILE_BRIDGE_TYPE.equals(thingTypeUID)) {
69             return super.createThing(thingTypeUID, configuration, thingUID, null);
70         } else if (XMLTV_CHANNEL_THING_TYPE.equals(thingTypeUID)) {
71             ThingUID newThingUID;
72             if (bridgeUID != null && thingUID != null) {
73                 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
74             } else {
75                 newThingUID = thingUID;
76             }
77             return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
78         }
79         throw new IllegalArgumentException(
80                 "The thing type " + thingTypeUID + " is not supported by the XmlTV binding.");
81     }
82
83     @Override
84     protected @Nullable ThingHandler createHandler(Thing thing) {
85         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
86
87         if (XMLTV_FILE_BRIDGE_TYPE.equals(thingTypeUID)) {
88             return new XmlTVHandler((Bridge) thing, xif, unmarshaller);
89         } else if (XMLTV_CHANNEL_THING_TYPE.equals(thingTypeUID)) {
90             return new ChannelHandler(thing, timeZoneProvider.getTimeZone());
91         }
92
93         return null;
94     }
95 }