]> git.basschouten.com Git - openhab-addons.git/blob
d8c16ab9857303b960c0ab4445a831a8c1b83efa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.plugwiseha.internal.api.xml;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.plugwiseha.internal.api.model.converter.DateTimeConverter;
17 import org.openhab.binding.plugwiseha.internal.api.model.dto.ActuatorFunctionalities;
18 import org.openhab.binding.plugwiseha.internal.api.model.dto.ActuatorFunctionality;
19 import org.openhab.binding.plugwiseha.internal.api.model.dto.ActuatorFunctionalityOffsetTemperature;
20 import org.openhab.binding.plugwiseha.internal.api.model.dto.ActuatorFunctionalityRelay;
21 import org.openhab.binding.plugwiseha.internal.api.model.dto.ActuatorFunctionalityThermostat;
22 import org.openhab.binding.plugwiseha.internal.api.model.dto.ActuatorFunctionalityThreshold;
23 import org.openhab.binding.plugwiseha.internal.api.model.dto.ActuatorFunctionalityTimer;
24 import org.openhab.binding.plugwiseha.internal.api.model.dto.ActuatorFunctionalityToggle;
25 import org.openhab.binding.plugwiseha.internal.api.model.dto.Appliance;
26 import org.openhab.binding.plugwiseha.internal.api.model.dto.Appliances;
27 import org.openhab.binding.plugwiseha.internal.api.model.dto.DomainObjects;
28 import org.openhab.binding.plugwiseha.internal.api.model.dto.GatewayEnvironment;
29 import org.openhab.binding.plugwiseha.internal.api.model.dto.GatewayInfo;
30 import org.openhab.binding.plugwiseha.internal.api.model.dto.Location;
31 import org.openhab.binding.plugwiseha.internal.api.model.dto.Locations;
32 import org.openhab.binding.plugwiseha.internal.api.model.dto.Log;
33 import org.openhab.binding.plugwiseha.internal.api.model.dto.Logs;
34 import org.openhab.binding.plugwiseha.internal.api.model.dto.Module;
35 import org.openhab.binding.plugwiseha.internal.api.model.dto.Modules;
36 import org.openhab.binding.plugwiseha.internal.api.model.dto.Service;
37 import org.openhab.binding.plugwiseha.internal.api.model.dto.Services;
38 import org.openhab.binding.plugwiseha.internal.api.model.dto.ZigBeeNode;
39
40 import com.thoughtworks.xstream.XStream;
41 import com.thoughtworks.xstream.io.xml.StaxDriver;
42 import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
43 import com.thoughtworks.xstream.security.NoTypePermission;
44 import com.thoughtworks.xstream.security.NullPermission;
45
46 /**
47  * The {@link PlugwiseHAXStream} class is a utility class that wraps an XStream
48  * object and provide additional functionality specific to the PlugwiseHA
49  * binding. It automatically load the correct converter classes and processes
50  * the XStream annotions used by the object classes.
51  * 
52  * @author B. van Wetten - Initial contribution
53  */
54
55 @NonNullByDefault
56 public class PlugwiseHAXStream extends XStream {
57
58     private static XmlFriendlyNameCoder customCoder = new XmlFriendlyNameCoder("_-", "_");
59
60     public PlugwiseHAXStream() {
61         super(new StaxDriver(PlugwiseHAXStream.customCoder));
62
63         initialize();
64     }
65
66     // Protected methods
67
68     @SuppressWarnings("rawtypes")
69     protected void allowClass(Class clz) {
70         this.processAnnotations(clz);
71         this.allowTypeHierarchy(clz);
72     }
73
74     protected void initialize() {
75         // Configure XStream
76         this.ignoreUnknownElements();
77         this.setClassLoader(getClass().getClassLoader());
78
79         // Clear out existing
80         this.addPermission(NoTypePermission.NONE);
81         this.addPermission(NullPermission.NULL);
82
83         // Whitelist classes
84         this.allowClass(GatewayInfo.class);
85         this.allowClass(GatewayEnvironment.class);
86         this.allowClass(Appliances.class);
87         this.allowClass(Appliance.class);
88         this.allowClass(Modules.class);
89         this.allowClass(Module.class);
90         this.allowClass(Locations.class);
91         this.allowClass(Location.class);
92         this.allowClass(Logs.class);
93         this.allowClass(Log.class);
94         this.allowClass(Services.class);
95         this.allowClass(Service.class);
96         this.allowClass(ZigBeeNode.class);
97         this.allowClass(ActuatorFunctionalities.class);
98         this.allowClass(ActuatorFunctionality.class);
99         this.allowClass(ActuatorFunctionalityThermostat.class);
100         this.allowClass(ActuatorFunctionalityOffsetTemperature.class);
101         this.allowClass(ActuatorFunctionalityRelay.class);
102         this.allowClass(ActuatorFunctionalityTimer.class);
103         this.allowClass(ActuatorFunctionalityThreshold.class);
104         this.allowClass(ActuatorFunctionalityToggle.class);
105         this.allowClass(DomainObjects.class);
106
107         // Register custom converters
108         this.registerConverter(new DateTimeConverter());
109     }
110 }