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 Creator getCreator() {
 34        return Creator.of(getAttribute("creator"));
 35    }
 36
 37    public Senders getSenders() {
 38        return Senders.of(getAttribute("senders"));
 39    }
 40
 41    public void setSenders(Senders senders) {
 42        this.setAttribute("senders", senders.toString());
 43    }
 44
 45    public GenericDescription getDescription() {
 46        final Element description = this.findChild("description");
 47        if (description == null) {
 48            return null;
 49        }
 50        final String xmlns = description.getNamespace();
 51        if (FileTransferDescription.NAMESPACES.contains(xmlns)) {
 52            return FileTransferDescription.upgrade(description);
 53        } else {
 54            return GenericDescription.upgrade(description);
 55        }
 56    }
 57
 58    public void setDescription(final GenericDescription description) {
 59        Preconditions.checkNotNull(description);
 60        this.addChild(description);
 61    }
 62
 63    public String getDescriptionNamespace() {
 64        final Element description = this.findChild("description");
 65        return description == null ? null : description.getNamespace();
 66    }
 67
 68    public GenericTransportInfo getTransport() {
 69        final Element transport = this.findChild("transport");
 70        final String namespace = transport == null ? null : transport.getNamespace();
 71        if (Namespace.JINGLE_TRANSPORTS_IBB.equals(namespace)) {
 72            return IbbTransportInfo.upgrade(transport);
 73        } else if (Namespace.JINGLE_TRANSPORTS_S5B.equals(namespace)) {
 74            return S5BTransportInfo.upgrade(transport);
 75        } else if (Namespace.JINGLE_TRANSPORT_ICE_UDP.equals(namespace)) {
 76            return IceUdpTransportInfo.upgrade(transport);
 77        } else if (transport != null) {
 78            return GenericTransportInfo.upgrade(transport);
 79        } else {
 80            return null;
 81        }
 82    }
 83
 84    public void setTransport(GenericTransportInfo transportInfo) {
 85        this.addChild(transportInfo);
 86    }
 87
 88    public enum Creator {
 89        INITIATOR, RESPONDER;
 90
 91        public static Creator of(final String value) {
 92            return Creator.valueOf(value.toUpperCase(Locale.ROOT));
 93        }
 94
 95        @Override
 96        @NonNull
 97        public String toString() {
 98            return super.toString().toLowerCase(Locale.ROOT);
 99        }
100    }
101
102    public enum Senders {
103        BOTH, INITIATOR, NONE, RESPONDER;
104
105        public static Senders of(final String value) {
106            return Senders.valueOf(value.toUpperCase(Locale.ROOT));
107        }
108
109        @Override
110        @NonNull
111        public String toString() {
112            return super.toString().toLowerCase(Locale.ROOT);
113        }
114    }
115}