2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.xmltv.internal;
15 import static org.openhab.binding.xmltv.internal.XmlTVBindingConstants.*;
17 import javax.xml.bind.JAXBContext;
18 import javax.xml.bind.JAXBException;
19 import javax.xml.bind.Unmarshaller;
20 import javax.xml.stream.XMLInputFactory;
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;
41 * The {@link XmlTVHandlerFactory} is responsible for creating things and thing
44 * @author Gaƫl L'hopital - Initial contribution
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;
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.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
58 xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
62 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
63 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
67 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
68 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
69 if (XMLTV_FILE_BRIDGE_TYPE.equals(thingTypeUID)) {
70 return super.createThing(thingTypeUID, configuration, thingUID, null);
71 } else if (XMLTV_CHANNEL_THING_TYPE.equals(thingTypeUID)) {
73 if (bridgeUID != null && thingUID != null) {
74 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
76 newThingUID = thingUID;
78 return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
80 throw new IllegalArgumentException(
81 "The thing type " + thingTypeUID + " is not supported by the XmlTV binding.");
85 protected @Nullable ThingHandler createHandler(Thing thing) {
86 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
88 if (XMLTV_FILE_BRIDGE_TYPE.equals(thingTypeUID)) {
89 return new XmlTVHandler((Bridge) thing, xif, unmarshaller);
90 } else if (XMLTV_CHANNEL_THING_TYPE.equals(thingTypeUID)) {
91 return new ChannelHandler(thing, timeZoneProvider.getTimeZone());