Content.java

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