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