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