001/**
002 *
003 * Copyright 2023 Paul Schaub
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.thumbnails.element;
018
019import org.jivesoftware.smack.packet.ExtensionElement;
020import org.jivesoftware.smack.packet.XmlEnvironment;
021import org.jivesoftware.smack.util.Objects;
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024public class ThumbnailElement implements ExtensionElement {
025
026    public static final String ELEMENT = "thumbnail";
027    public static final String NAMESPACE = "urn:xmpp:thumbs:1";
028    public static final String ELEM_URI = "uri";
029    public static final String ELEM_MEDIA_TYPE = "media-type";
030    public static final String ELEM_WIDTH = "width";
031    public static final String ELEM_HEIGHT = "height";
032
033    private final String uri;
034    private final String mediaType;
035    private final Integer width;
036    private final Integer height;
037
038    public ThumbnailElement(String uri) {
039        this(uri, null, null, null);
040    }
041
042    public ThumbnailElement(String uri, String mediaType, Integer width, Integer height) {
043        this.uri = Objects.requireNonNull(uri);
044        this.mediaType = mediaType;
045
046        if (width != null && width < 0) {
047            throw new IllegalArgumentException("Width cannot be negative.");
048        }
049        this.width = width;
050
051        if (height != null && height < 0) {
052            throw new IllegalArgumentException("Height cannot be negative.");
053        }
054        this.height = height;
055    }
056
057    public String getUri() {
058        return uri;
059    }
060
061    public String getMediaType() {
062        return mediaType;
063    }
064
065    public Integer getWidth() {
066        return width;
067    }
068
069    public Integer getHeight() {
070        return height;
071    }
072
073    @Override
074    public CharSequence toXML(XmlEnvironment xmlEnvironment) {
075        XmlStringBuilder sb = new XmlStringBuilder(this, xmlEnvironment);
076        return sb.attribute(ELEM_URI, uri)
077                .optAttribute(ELEM_MEDIA_TYPE, mediaType)
078                .optAttribute(ELEM_WIDTH, width)
079                .optAttribute(ELEM_HEIGHT, height)
080                .closeEmptyElement();
081    }
082
083    @Override
084    public String getElementName() {
085        return ELEMENT;
086    }
087
088    @Override
089    public String getNamespace() {
090        return NAMESPACE;
091    }
092}