001/**
002 *
003 * Copyright 2019-2020 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.mediaelement.element;
018
019import java.net.URI;
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.List;
023
024import javax.xml.namespace.QName;
025
026import org.jivesoftware.smack.datatypes.UInt16;
027import org.jivesoftware.smack.packet.XmlElement;
028import org.jivesoftware.smack.packet.XmlEnvironment;
029import org.jivesoftware.smack.util.Objects;
030import org.jivesoftware.smack.util.StringUtils;
031import org.jivesoftware.smack.util.XmlStringBuilder;
032
033import org.jivesoftware.smackx.xdata.FormField;
034import org.jivesoftware.smackx.xdata.FormFieldChildElement;
035
036public class MediaElement implements FormFieldChildElement {
037
038    public static final String ELEMENT = "media";
039
040    public static final String NAMESPACE = "urn:xmpp:media-element";
041
042    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
043
044    private final UInt16 height;
045
046    private final UInt16 width;
047
048    private final List<Uri> uris;
049
050    public MediaElement(Builder builder) {
051        this.height = builder.height;
052        this.width = builder.width;
053        this.uris = Collections.unmodifiableList(builder.uris);
054    }
055
056    public UInt16 getHeight() {
057        return height;
058    }
059
060    public UInt16 getWidth() {
061        return width;
062    }
063
064    public List<Uri> getUris() {
065        return uris;
066    }
067
068    @Override
069    public String getElementName() {
070        return ELEMENT;
071    }
072
073    @Override
074    public String getNamespace() {
075        return NAMESPACE;
076    }
077
078    @Override
079    public QName getQName() {
080        return QNAME;
081    }
082
083    @Override
084    public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
085        XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);
086        xml.optAttributeCs("height", height)
087            .optAttributeCs("width", width)
088            .rightAngleBracket();
089
090        xml.append(uris);
091
092        xml.closeElement(this);
093        return xml;
094    }
095
096    public MediaElement from(FormField formField) {
097        return (MediaElement) formField.getFormFieldChildElement(QNAME);
098    }
099
100    public static Builder builder() {
101        return new Builder();
102    }
103
104    public static final class Builder {
105        private UInt16 height, width;
106
107        private List<Uri> uris = new ArrayList<>();
108
109        public Builder setHeightAndWidth(int height, int width) {
110            return setHeightAndWidth(UInt16.from(height), UInt16.from(width));
111        }
112
113        public Builder setHeightAndWidth(UInt16 height, UInt16 width) {
114            this.height = height;
115            this.width = width;
116            return this;
117        }
118
119        public Builder addUri(URI uri, String type) {
120            return addUri(new Uri(uri, type));
121        }
122
123        public Builder addUri(Uri uri) {
124            uris.add(uri);
125            return this;
126        }
127
128        public MediaElement build() {
129            return new MediaElement(this);
130        }
131    }
132
133    public static final class Uri implements XmlElement {
134        public static final String ELEMENT = "uri";
135
136        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
137
138        private final URI uri;
139        private final String type;
140
141        public Uri(URI uri, String type) {
142            this.uri = Objects.requireNonNull(uri);
143            this.type = StringUtils.requireNotNullNorEmpty(type, "The 'type' argument must not be null or empty");
144        }
145
146        public URI getUri() {
147            return uri;
148        }
149
150        public String getType() {
151            return type;
152        }
153
154        @Override
155        public String getElementName() {
156            return ELEMENT;
157        }
158
159        @Override
160        public String getNamespace() {
161            return NAMESPACE;
162        }
163
164        @Override
165        public QName getQName() {
166            return QNAME;
167        }
168
169        @Override
170        public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
171            XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);
172            xml.attribute("type", type)
173               .rightAngleBracket();
174            xml.escape(uri.toString());
175            xml.closeElement(this);
176            return xml;
177        }
178
179    }
180}