Content.java

  1package eu.siacs.conversations.xmpp.jingle.stanzas;
  2
  3import androidx.annotation.NonNull;
  4
  5import com.google.common.base.Preconditions;
  6
  7import java.util.Locale;
  8
  9import eu.siacs.conversations.xml.Element;
 10import eu.siacs.conversations.xml.Namespace;
 11
 12public class Content extends Element {
 13
 14    public Content(final Creator creator, final String name) {
 15        super("content", Namespace.JINGLE);
 16        this.setAttribute("creator", creator.toString());
 17        this.setAttribute("name", name);
 18    }
 19
 20    private Content() {
 21        super("content", Namespace.JINGLE);
 22    }
 23
 24    public static Content upgrade(final Element element) {
 25        Preconditions.checkArgument("content".equals(element.getName()));
 26        final Content content = new Content();
 27        content.setAttributes(element.getAttributes());
 28        content.setChildren(element.getChildren());
 29        return content;
 30    }
 31
 32    public String getContentName() {
 33        return this.getAttribute("name");
 34    }
 35
 36    public Creator getCreator() {
 37        return Creator.of(getAttribute("creator"));
 38    }
 39
 40    public Senders getSenders() {
 41        return Senders.of(getAttribute("senders"));
 42    }
 43
 44    public void setSenders(Senders senders) {
 45        this.setAttribute("senders", senders.toString());
 46    }
 47
 48    public GenericDescription getDescription() {
 49        final Element description = this.findChild("description");
 50        if (description == null) {
 51            return null;
 52        }
 53        final String namespace = description.getNamespace();
 54        if (FileTransferDescription.NAMESPACES.contains(namespace)) {
 55            return FileTransferDescription.upgrade(description);
 56        } else if (Namespace.JINGLE_APPS_RTP.equals(namespace)) {
 57            return RtpDescription.upgrade(description);
 58        } else {
 59            return GenericDescription.upgrade(description);
 60        }
 61    }
 62
 63    public void setDescription(final GenericDescription description) {
 64        Preconditions.checkNotNull(description);
 65        this.addChild(description);
 66    }
 67
 68    public String getDescriptionNamespace() {
 69        final Element description = this.findChild("description");
 70        return description == null ? null : description.getNamespace();
 71    }
 72
 73    public GenericTransportInfo getTransport() {
 74        final Element transport = this.findChild("transport");
 75        final String namespace = transport == null ? null : transport.getNamespace();
 76        if (Namespace.JINGLE_TRANSPORTS_IBB.equals(namespace)) {
 77            return IbbTransportInfo.upgrade(transport);
 78        } else if (Namespace.JINGLE_TRANSPORTS_S5B.equals(namespace)) {
 79            return S5BTransportInfo.upgrade(transport);
 80        } else if (Namespace.JINGLE_TRANSPORT_ICE_UDP.equals(namespace)) {
 81            return IceUdpTransportInfo.upgrade(transport);
 82        } else if (transport != null) {
 83            return GenericTransportInfo.upgrade(transport);
 84        } else {
 85            return null;
 86        }
 87    }
 88
 89    public void setTransport(GenericTransportInfo transportInfo) {
 90        this.addChild(transportInfo);
 91    }
 92
 93    public enum Creator {
 94        INITIATOR, RESPONDER;
 95
 96        public static Creator of(final String value) {
 97            return Creator.valueOf(value.toUpperCase(Locale.ROOT));
 98        }
 99
100        @Override
101        @NonNull
102        public String toString() {
103            return super.toString().toLowerCase(Locale.ROOT);
104        }
105    }
106
107    public enum Senders {
108        BOTH, INITIATOR, NONE, RESPONDER;
109
110        public static Senders of(final String value) {
111            return Senders.valueOf(value.toUpperCase(Locale.ROOT));
112        }
113
114        @Override
115        @NonNull
116        public String toString() {
117            return super.toString().toLowerCase(Locale.ROOT);
118        }
119    }
120}