Skip to content

Commit

Permalink
[suggestion-finder] update xml schema, converters, and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
  • Loading branch information
andrewfg committed Oct 21, 2023
1 parent 99dabd0 commit d49f100
Show file tree
Hide file tree
Showing 19 changed files with 414 additions and 59 deletions.
7 changes: 0 additions & 7 deletions bundles/org.openhab.core.addon/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,5 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
16 changes: 14 additions & 2 deletions bundles/org.openhab.core.addon/schema/addon-1.0.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<xs:element name="config-description" type="config-description:configDescription"/>
<xs:element name="config-description-ref" type="config-description:configDescriptionRef"/>
</xs:choice>
<xs:element name="discovery-method" type="addon:discoveryMethodType" minOccurs="0"/>
<xs:element name="discovery-methods" type="addon:discoveryMethodsType" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" type="config-description:idRestrictionPattern" use="required">
<xs:annotation>
Expand Down Expand Up @@ -81,11 +81,23 @@
</xs:restriction>
</xs:simpleType>

<xs:complexType name="discoveryMethodsType">
<xs:sequence>
<xs:element type="addon:discoveryMethodType" name="discovery-method" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="discoveryMethodType">
<xs:sequence>
<xs:element type="xs:string" name="service-type"/>
<xs:element type="xs:string" name="mdns-service-type" minOccurs="0"/>
<xs:element type="addon:matchPropertyType" name="match-property" minOccurs="0"/>
<xs:element type="addon:matchPropertiesType" name="match-properties" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="matchPropertiesType">
<xs:sequence>
<xs:element type="addon:matchPropertyType" name="match-property" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
*/
@NonNullByDefault
public class AddonDiscoveryMethod {
private @Nullable String serviceType;
private @NonNullByDefault({}) String serviceType;
private @Nullable String mdnsServiceType;
private @Nullable List<AddonMatchProperty> matchProperties;

public AddonDiscoveryServiceType getServiceType() {
String serviceType = this.serviceType;
return AddonDiscoveryServiceType.valueOf(serviceType != null ? serviceType.toUpperCase() : "");
public String getServiceType() {
return serviceType.toLowerCase();
}

public String getMdnsServiceType() {
Expand All @@ -43,17 +42,17 @@ public List<AddonMatchProperty> getMatchProperties() {
return matchProperties != null ? matchProperties : List.of();
}

public AddonDiscoveryMethod setServiceType(AddonDiscoveryServiceType serviceType) {
this.serviceType = serviceType.name().toLowerCase();
public AddonDiscoveryMethod setServiceType(String serviceType) {
this.serviceType = serviceType.toLowerCase();
return this;
}

public AddonDiscoveryMethod setMdnsServiceType(String mdnsServiceType) {
public AddonDiscoveryMethod setMdnsServiceType(@Nullable String mdnsServiceType) {
this.mdnsServiceType = mdnsServiceType;
return this;
}

public AddonDiscoveryMethod setMatchProperties(List<AddonMatchProperty> matchProperties) {
public AddonDiscoveryMethod setMatchProperties(@Nullable List<AddonMatchProperty> matchProperties) {
this.matchProperties = matchProperties;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@
*/
package org.openhab.core.addon;

import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* Enum of different supported discovery types.
* DTO containing a list of {@code AddonInfo}
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
public enum AddonDiscoveryServiceType {
MDNS,
UPNP
public class AddonInfoList {
protected @Nullable List<AddonInfo> addons;

public List<AddonInfo> getAddons() {
List<AddonInfo> addons = this.addons;
return addons != null ? addons : List.of();
}

public AddonInfoList setAddons(@Nullable List<AddonInfo> addons) {
this.addons = addons;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package org.openhab.core.addon;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* DTO for serialization of a property match regular expression.
Expand All @@ -22,21 +21,19 @@
*/
@NonNullByDefault
public class AddonMatchProperty {
private @Nullable String name;
private @Nullable String regex;
private @NonNullByDefault({}) String name;
private @NonNullByDefault({}) String regex;

public AddonMatchProperty(String name, String regex) {
this.name = name;
this.regex = regex;
}

public String getName() {
String name = this.name;
return name != null ? name : "";
return name;
}

public String getRegex() {
String regex = this.regex;
return regex != null ? regex : "";
return regex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.addon.internal.xml;

import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.addon.AddonDiscoveryMethod;
import org.openhab.core.addon.AddonMatchProperty;
import org.openhab.core.config.core.xml.util.GenericUnmarshaller;
import org.openhab.core.config.core.xml.util.NodeIterator;

import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;

/**
* The {@link AddonDiscoveryMethodConverter} is a concrete implementation of the {@code XStream} {@link Converter}
* interface used to convert add-on discovery method information within an XML document into a
* {@link AddonDiscoveryMethod} object.
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
public class AddonDiscoveryMethodConverter extends GenericUnmarshaller<AddonDiscoveryMethod> {

public AddonDiscoveryMethodConverter() {
super(AddonDiscoveryMethod.class);
}

@Override
public @Nullable Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
List<?> nodes = (List<?>) context.convertAnother(context, List.class);
NodeIterator nodeIterator = new NodeIterator(nodes);

String serviceType = requireNonEmpty((String) nodeIterator.nextValue("service-type", true),
"Service type is null or empty");

String mdnsServiceType = (String) nodeIterator.nextValue("mdns-service-type", false);

Object object = nodeIterator.nextList("match-properties", false);
List<AddonMatchProperty> matchProperties = !(object instanceof List<?> list) ? null
: list.stream().filter(e -> (e instanceof AddonMatchProperty)).map(e -> ((AddonMatchProperty) e))
.toList();

nodeIterator.assertEndOfType();

return new AddonDiscoveryMethod().setServiceType(serviceType).setMdnsServiceType(mdnsServiceType)
.setMatchProperties(matchProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package org.openhab.core.addon.internal.xml;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -39,6 +38,7 @@
* @author Michael Grammling - Initial contribution
* @author Andre Fuechsel - Made author tag optional
* @author Jan N. Klug - Refactored to cover all add-ons
* @author Andrew Fiddian-Green - Added discovery methods
*/
@NonNullByDefault
public class AddonInfoConverter extends GenericUnmarshaller<AddonInfoXmlResult> {
Expand Down Expand Up @@ -109,21 +109,10 @@ public AddonInfoConverter() {

addonInfo.withConfigDescriptionURI(configDescriptionURI);

List<AddonDiscoveryMethod> discoveryMethods = null;
while (true) {
Object value = nodeIterator.nextValue("discovery-method", false);
if (value instanceof AddonDiscoveryMethod discoveryMethod) {
if (discoveryMethods == null) {
discoveryMethods = new ArrayList<>();
}
discoveryMethods.add(discoveryMethod);
} else {
break;
}
}
if (discoveryMethods != null) {
addonInfo.withDiscoveryMethods(discoveryMethods);
}
Object object = nodeIterator.nextList("discovery-methods", false);
addonInfo.withDiscoveryMethods(!(object instanceof List<?> list) ? null
: list.stream().filter(e -> (e instanceof AddonDiscoveryMethod)).map(e -> ((AddonDiscoveryMethod) e))
.toList());

nodeIterator.assertEndOfType();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.addon.internal.xml;

import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.addon.AddonInfo;
import org.openhab.core.addon.AddonInfoList;
import org.openhab.core.config.core.xml.util.GenericUnmarshaller;
import org.openhab.core.config.core.xml.util.NodeIterator;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;

/**
* The {@link AddonInfoListConverter} is a concrete implementation of the {@code XStream} {@link Converter}
* interface used to convert a list of add-on information within an XML document into a list of {@link AddonInfo}
* objects.
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
public class AddonInfoListConverter extends GenericUnmarshaller<AddonInfoList> {

public AddonInfoListConverter() {
super(AddonInfoList.class);
}

@Override
public @Nullable Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
List<?> nodes = (List<?>) context.convertAnother(context, List.class);
NodeIterator nodeIterator = new NodeIterator(nodes);

Object object = nodeIterator.nextList("addons", false);
List<AddonInfo> addons = (object instanceof List<?> list)
? list.stream().filter(e -> (e instanceof AddonInfoXmlResult)).map(e -> (AddonInfoXmlResult) e)
.map(r -> r.addonInfo()).toList()
: null;

nodeIterator.assertEndOfType();

return new AddonInfoList().setAddons(addons);
}
}
Loading

0 comments on commit d49f100

Please sign in to comment.