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