001/**
002 *
003 * Copyright 2017 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.jingle_filetransfer.element;
018
019import javax.xml.namespace.QName;
020
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smack.util.Objects;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025import org.jivesoftware.smackx.jingle.element.JingleContent;
026
027/**
028 * Checksum element.
029 */
030public class Checksum implements ExtensionElement {
031    public static final String ELEMENT = "checksum";
032    public static final QName QNAME = new QName(JingleFileTransfer.NAMESPACE_V5, ELEMENT);
033
034    public static final String ATTR_CREATOR = "creator";
035    public static final String ATTR_NAME = "name";
036
037    private final JingleContent.Creator creator;
038    private final String name;
039    private final JingleFileTransferChild file;
040
041    public Checksum(JingleContent.Creator creator, String name, JingleFileTransferChild file) {
042        this.creator = creator;
043        this.name = name;
044        this.file = Objects.requireNonNull(file, "file MUST NOT be null.");
045        Objects.requireNonNull(file.getHash(), "file MUST contain at least one hash element.");
046    }
047
048    @Override
049    public String getElementName() {
050        return QNAME.getLocalPart();
051    }
052
053    @Override
054    public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
055        XmlStringBuilder sb = new XmlStringBuilder(this);
056        sb.optAttribute(ATTR_CREATOR, creator);
057        sb.optAttribute(ATTR_NAME, name);
058        sb.rightAngleBracket();
059        sb.append(file);
060        sb.closeElement(this);
061        return sb;
062    }
063
064    @Override
065    public String getNamespace() {
066        return QNAME.getNamespaceURI();
067    }
068}