Checksum.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.jingle_filetransfer.element;

  18. import javax.xml.namespace.QName;

  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.util.Objects;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;

  22. import org.jivesoftware.smackx.jingle.element.JingleContent;

  23. /**
  24.  * Checksum element.
  25.  */
  26. public class Checksum implements ExtensionElement {
  27.     public static final String ELEMENT = "checksum";
  28.     public static final QName QNAME = new QName(JingleFileTransfer.NAMESPACE_V5, ELEMENT);

  29.     public static final String ATTR_CREATOR = "creator";
  30.     public static final String ATTR_NAME = "name";

  31.     private final JingleContent.Creator creator;
  32.     private final String name;
  33.     private final JingleFileTransferChild file;

  34.     public Checksum(JingleContent.Creator creator, String name, JingleFileTransferChild file) {
  35.         this.creator = creator;
  36.         this.name = name;
  37.         this.file = Objects.requireNonNull(file, "file MUST NOT be null.");
  38.         Objects.requireNonNull(file.getHash(), "file MUST contain at least one hash element.");
  39.     }

  40.     @Override
  41.     public String getElementName() {
  42.         return QNAME.getLocalPart();
  43.     }

  44.     @Override
  45.     public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  46.         XmlStringBuilder sb = new XmlStringBuilder(this);
  47.         sb.optAttribute(ATTR_CREATOR, creator);
  48.         sb.optAttribute(ATTR_NAME, name);
  49.         sb.rightAngleBracket();
  50.         sb.append(file);
  51.         sb.closeElement(this);
  52.         return sb;
  53.     }

  54.     @Override
  55.     public String getNamespace() {
  56.         return QNAME.getNamespaceURI();
  57.     }
  58. }