1package eu.siacs.conversations.xmpp.jingle.stanzas;
2
3import com.google.common.base.Preconditions;
4import com.google.common.collect.ImmutableList;
5
6import java.util.Collection;
7import java.util.List;
8
9import eu.siacs.conversations.xml.Element;
10import eu.siacs.conversations.xml.Namespace;
11
12public class Group extends Element {
13
14 private Group() {
15 super("group", Namespace.JINGLE_APPS_GROUPING);
16 }
17
18 public Group(final String semantics, final Collection<String> identificationTags) {
19 super("group", Namespace.JINGLE_APPS_GROUPING);
20 this.setAttribute("semantics", semantics);
21 for (String tag : identificationTags) {
22 this.addChild(new Element("content").setAttribute("name", tag));
23 }
24 }
25
26 public String getSemantics() {
27 return this.getAttribute("semantics");
28 }
29
30 public List<String> getIdentificationTags() {
31 final ImmutableList.Builder<String> builder = new ImmutableList.Builder<>();
32 for (final Element child : getChildren()) {
33 if ("content".equals(child.getName())) {
34 final String name = child.getAttribute("name");
35 if (name != null) {
36 builder.add(name);
37 }
38 }
39 }
40 return builder.build();
41 }
42
43 public static Group ofSdpString(final String input) {
44 final ImmutableList.Builder<String> tagBuilder = new ImmutableList.Builder<>();
45 final String[] parts = input.split(" ");
46 if (parts.length >= 2) {
47 final String semantics = parts[0];
48 for(int i = 1; i < parts.length; ++i) {
49 tagBuilder.add(parts[i]);
50 }
51 return new Group(semantics,tagBuilder.build());
52 }
53 return null;
54 }
55
56 public static Group upgrade(final Element element) {
57 Preconditions.checkArgument("group".equals(element.getName()));
58 Preconditions.checkArgument(Namespace.JINGLE_APPS_GROUPING.equals(element.getNamespace()));
59 final Group group = new Group();
60 group.setAttributes(element.getAttributes());
61 group.setChildren(element.getChildren());
62 return group;
63 }
64}