]> git.basschouten.com Git - openhab-addons.git/blob
0a3659ef0ca80398b74ad8c12646477943c7cd2c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.lutron.internal.xml;
14
15 import java.io.File;
16 import java.io.Reader;
17 import java.net.URL;
18
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;
29
30 import com.thoughtworks.xstream.XStream;
31 import com.thoughtworks.xstream.converters.ConversionException;
32 import com.thoughtworks.xstream.io.xml.StaxDriver;
33
34 /**
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.
37  *
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.
41  */
42 @NonNullByDefault
43 public class DbXmlInfoReader {
44
45     private final XStream xstream;
46
47     public DbXmlInfoReader() {
48         StaxDriver driver = new StaxDriver();
49
50         xstream = new XStream(driver);
51
52         setClassLoader(Project.class.getClassLoader());
53         registerAliases(this.xstream);
54     }
55
56     public void setClassLoader(ClassLoader classLoader) {
57         xstream.setClassLoader(classLoader);
58     }
59
60     public void registerAliases(XStream xstream) {
61         xstream.alias("Project", Project.class);
62         xstream.aliasField("AppVer", Project.class, "appVersion");
63         xstream.aliasField("XMLVer", Project.class, "xmlVersion");
64         xstream.aliasField("Areas", Project.class, "areas");
65         xstream.aliasField("Timeclocks", Project.class, "timeclocks");
66         xstream.aliasField("GreenModes", Project.class, "greenmodes");
67
68         xstream.alias("Area", Area.class);
69         xstream.aliasField("Name", Area.class, "name");
70         xstream.useAttributeFor(Area.class, "name");
71         xstream.aliasField("DeviceGroups", Area.class, "deviceNodes");
72         xstream.aliasField("Outputs", Area.class, "outputs");
73         xstream.aliasField("Areas", Area.class, "areas");
74
75         xstream.alias("DeviceGroup", DeviceGroup.class);
76         xstream.aliasField("Name", DeviceGroup.class, "name");
77         xstream.useAttributeFor(DeviceGroup.class, "name");
78         xstream.aliasField("Devices", DeviceGroup.class, "devices");
79
80         xstream.alias("Device", Device.class);
81         xstream.aliasField("Name", Device.class, "name");
82         xstream.useAttributeFor(Device.class, "name");
83         xstream.aliasField("IntegrationID", Device.class, "integrationId");
84         xstream.useAttributeFor(Device.class, "integrationId");
85         xstream.aliasField("DeviceType", Device.class, "type");
86         xstream.useAttributeFor(Device.class, "type");
87         xstream.aliasField("Components", Device.class, "components");
88
89         xstream.alias("Component", Component.class);
90         xstream.aliasField("ComponentNumber", Component.class, "componentNumber");
91         xstream.useAttributeFor(Component.class, "componentNumber");
92         xstream.aliasField("ComponentType", Component.class, "type");
93         xstream.useAttributeFor(Component.class, "type");
94
95         xstream.alias("Output", Output.class);
96         xstream.aliasField("Name", Output.class, "name");
97         xstream.useAttributeFor(Output.class, "name");
98         xstream.aliasField("IntegrationID", Output.class, "integrationId");
99         xstream.useAttributeFor(Output.class, "integrationId");
100         xstream.aliasField("OutputType", Output.class, "type");
101         xstream.useAttributeFor(Output.class, "type");
102
103         xstream.alias("Timeclock", Timeclock.class);
104         xstream.aliasField("Name", Timeclock.class, "name");
105         xstream.useAttributeFor(Timeclock.class, "name");
106         xstream.aliasField("IntegrationID", Timeclock.class, "integrationId");
107         xstream.useAttributeFor(Timeclock.class, "integrationId");
108
109         xstream.alias("GreenMode", GreenMode.class);
110         xstream.aliasField("Name", GreenMode.class, "name");
111         xstream.useAttributeFor(GreenMode.class, "name");
112         xstream.aliasField("IntegrationID", GreenMode.class, "integrationId");
113         xstream.useAttributeFor(GreenMode.class, "integrationId");
114
115         // This reader is only interested in device thing information and does not read
116         // everything contained in DbXmlInfo. Ignoring unknown elements also makes the
117         // binding more tolerant of potential future changes to the XML schema.
118         xstream.ignoreUnknownElements();
119     }
120
121     public @Nullable Project readFromXML(@Nullable URL xmlURL) throws ConversionException {
122         if (xmlURL != null) {
123             return (Project) xstream.fromXML(xmlURL);
124         }
125
126         return null;
127     }
128
129     public @Nullable Project readFromXML(@Nullable File xmlFile) throws ConversionException {
130         if (xmlFile != null) {
131             return (Project) xstream.fromXML(xmlFile);
132         }
133
134         return null;
135     }
136
137     public @Nullable Project readFromXML(@Nullable Reader xmlReader) throws ConversionException {
138         if (xmlReader != null) {
139             return (Project) xstream.fromXML(xmlReader);
140         }
141
142         return null;
143     }
144
145     public @Nullable Project readFromXML(@Nullable String xmlString) throws ConversionException {
146         if (xmlString != null) {
147             return (Project) xstream.fromXML(xmlString);
148         }
149
150         return null;
151     }
152 }