2 * Copyright (c) 2010-2020 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.lutron.internal.xml;
16 import java.io.Reader;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.lutron.internal.discovery.project.Area;
22 import org.openhab.binding.lutron.internal.discovery.project.Component;
23 import org.openhab.binding.lutron.internal.discovery.project.Device;
24 import org.openhab.binding.lutron.internal.discovery.project.DeviceGroup;
25 import org.openhab.binding.lutron.internal.discovery.project.GreenMode;
26 import org.openhab.binding.lutron.internal.discovery.project.Output;
27 import org.openhab.binding.lutron.internal.discovery.project.Project;
28 import org.openhab.binding.lutron.internal.discovery.project.Timeclock;
30 import com.thoughtworks.xstream.XStream;
31 import com.thoughtworks.xstream.converters.ConversionException;
32 import com.thoughtworks.xstream.io.xml.StaxDriver;
35 * The {@link DbXmlInfoReader} reads Lutron XML project files and converts them to {@link Project}
36 * objects describing the device things contained within the Lutron system.
38 * @author Allan Tong - Initial contribution
39 * @author Bob Adair - Added support for reading XML from a file. Requires using XStream directly
40 * instead of XmlDocumentReader.
43 public class DbXmlInfoReader {
45 private final XStream xstream;
47 public DbXmlInfoReader() {
48 StaxDriver driver = new StaxDriver();
50 xstream = new XStream(driver);
52 configureSecurity(xstream);
53 setClassLoader(Project.class.getClassLoader());
54 registerAliases(xstream);
57 private void configureSecurity(XStream xstream) {
58 XStream.setupDefaultSecurity(xstream);
59 xstream.allowTypesByWildcard(new String[] { Project.class.getPackageName() + ".**" });
62 private void setClassLoader(ClassLoader classLoader) {
63 xstream.setClassLoader(classLoader);
66 private void registerAliases(XStream xstream) {
67 xstream.alias("Project", Project.class);
68 xstream.aliasField("AppVer", Project.class, "appVersion");
69 xstream.aliasField("XMLVer", Project.class, "xmlVersion");
70 xstream.aliasField("Areas", Project.class, "areas");
71 xstream.aliasField("Timeclocks", Project.class, "timeclocks");
72 xstream.aliasField("GreenModes", Project.class, "greenmodes");
74 xstream.alias("Area", Area.class);
75 xstream.aliasField("Name", Area.class, "name");
76 xstream.useAttributeFor(Area.class, "name");
77 xstream.aliasField("DeviceGroups", Area.class, "deviceNodes");
78 xstream.aliasField("Outputs", Area.class, "outputs");
79 xstream.aliasField("Areas", Area.class, "areas");
81 xstream.alias("DeviceGroup", DeviceGroup.class);
82 xstream.aliasField("Name", DeviceGroup.class, "name");
83 xstream.useAttributeFor(DeviceGroup.class, "name");
84 xstream.aliasField("Devices", DeviceGroup.class, "devices");
86 xstream.alias("Device", Device.class);
87 xstream.aliasField("Name", Device.class, "name");
88 xstream.useAttributeFor(Device.class, "name");
89 xstream.aliasField("IntegrationID", Device.class, "integrationId");
90 xstream.useAttributeFor(Device.class, "integrationId");
91 xstream.aliasField("DeviceType", Device.class, "type");
92 xstream.useAttributeFor(Device.class, "type");
93 xstream.aliasField("Components", Device.class, "components");
95 xstream.alias("Component", Component.class);
96 xstream.aliasField("ComponentNumber", Component.class, "componentNumber");
97 xstream.useAttributeFor(Component.class, "componentNumber");
98 xstream.aliasField("ComponentType", Component.class, "type");
99 xstream.useAttributeFor(Component.class, "type");
101 xstream.alias("Output", Output.class);
102 xstream.aliasField("Name", Output.class, "name");
103 xstream.useAttributeFor(Output.class, "name");
104 xstream.aliasField("IntegrationID", Output.class, "integrationId");
105 xstream.useAttributeFor(Output.class, "integrationId");
106 xstream.aliasField("OutputType", Output.class, "type");
107 xstream.useAttributeFor(Output.class, "type");
109 xstream.alias("Timeclock", Timeclock.class);
110 xstream.aliasField("Name", Timeclock.class, "name");
111 xstream.useAttributeFor(Timeclock.class, "name");
112 xstream.aliasField("IntegrationID", Timeclock.class, "integrationId");
113 xstream.useAttributeFor(Timeclock.class, "integrationId");
115 xstream.alias("GreenMode", GreenMode.class);
116 xstream.aliasField("Name", GreenMode.class, "name");
117 xstream.useAttributeFor(GreenMode.class, "name");
118 xstream.aliasField("IntegrationID", GreenMode.class, "integrationId");
119 xstream.useAttributeFor(GreenMode.class, "integrationId");
121 // This reader is only interested in device thing information and does not read
122 // everything contained in DbXmlInfo. Ignoring unknown elements also makes the
123 // binding more tolerant of potential future changes to the XML schema.
124 xstream.ignoreUnknownElements();
127 public @Nullable Project readFromXML(@Nullable URL xmlURL) throws ConversionException {
128 if (xmlURL != null) {
129 return (Project) xstream.fromXML(xmlURL);
135 public @Nullable Project readFromXML(@Nullable File xmlFile) throws ConversionException {
136 if (xmlFile != null) {
137 return (Project) xstream.fromXML(xmlFile);
143 public @Nullable Project readFromXML(@Nullable Reader xmlReader) throws ConversionException {
144 if (xmlReader != null) {
145 return (Project) xstream.fromXML(xmlReader);
151 public @Nullable Project readFromXML(@Nullable String xmlString) throws ConversionException {
152 if (xmlString != null) {
153 return (Project) xstream.fromXML(xmlString);