1package eu.siacs.conversations.entities;
2
3import java.util.List;
4import java.util.ArrayList;
5
6import eu.siacs.conversations.xml.Element;
7import eu.siacs.conversations.xmpp.stanzas.IqPacket;
8
9public class ServiceDiscoveryResult {
10 public static class Identity {
11 protected final String category;
12 protected final String type;
13 protected final String name;
14
15 public Identity(final String category, final String type, final String name) {
16 this.category = category;
17 this.type = type;
18 this.name = name;
19 }
20
21 public Identity(final Element el) {
22 this.category = el.getAttribute("category");
23 this.type = el.getAttribute("type");
24 this.name = el.getAttribute("name");
25 }
26
27 public String getCategory() {
28 return this.category;
29 }
30
31 public String getType() {
32 return this.type;
33 }
34
35 public String getName() {
36 return this.name;
37 }
38 }
39
40 protected final List<Identity> identities;
41 protected final List<String> features;
42
43 public ServiceDiscoveryResult(final List<Identity> identities, final List<String> features) {
44 this.identities = identities;
45 this.features = features;
46 }
47
48 public ServiceDiscoveryResult(final IqPacket packet) {
49 this.identities = new ArrayList<>();
50 this.features = new ArrayList<>();
51
52 final List<Element> elements = packet.query().getChildren();
53
54 for (final Element element : elements) {
55 if (element.getName().equals("identity")) {
56 Identity id = new Identity(element);
57 if (id.getType() != null && id.getCategory() != null) {
58 identities.add(id);
59 }
60 } else if (element.getName().equals("feature")) {
61 features.add(element.getAttribute("var"));
62 }
63 }
64 }
65
66 public List<Identity> getIdentities() {
67 return this.identities;
68 }
69
70 public List<String> getFeatures() {
71 return this.features;
72 }
73
74 public boolean hasIdentity(String category, String type) {
75 for(Identity id : this.getIdentities()) {
76 if((category == null || id.getCategory().equals(category)) &&
77 (type == null || id.getType().equals(type))) {
78 return true;
79 }
80 }
81
82 return false;
83 }
84}