ChatMarkersElements.java

  1. /**
  2.  *
  3.  * Copyright © 2016 Fernando Ramirez, 2018-2020 Florian Schmaus
  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.chat_markers.element;

  18. import javax.xml.namespace.QName;

  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.packet.Message;
  21. import org.jivesoftware.smack.util.StringUtils;
  22. import org.jivesoftware.smack.util.XmlStringBuilder;

  23. import org.jivesoftware.smackx.chat_markers.ChatMarkersState;

  24. /**
  25.  * Chat Markers elements (XEP-0333).
  26.  *
  27.  * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
  28.  *      Markers</a>
  29.  * @author Fernando Ramirez
  30.  *
  31.  */
  32. public class ChatMarkersElements {

  33.     public static final String NAMESPACE = "urn:xmpp:chat-markers:0";

  34.     /**
  35.      * Markable extension class.
  36.      *
  37.      * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
  38.      *      Markers</a>
  39.      * @author Fernando Ramirez
  40.      *
  41.      */
  42.     public static final class MarkableExtension implements ExtensionElement {

  43.         public static final MarkableExtension INSTANCE = new MarkableExtension();
  44.         /**
  45.          * markable element.
  46.          */
  47.         public static final String ELEMENT = ChatMarkersState.markable.toString();
  48.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  49.         private MarkableExtension() {
  50.         }

  51.         @Override
  52.         public String getElementName() {
  53.             return ELEMENT;
  54.         }

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

  59.         @Override
  60.         public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  61.             XmlStringBuilder xml = new XmlStringBuilder(this);
  62.             xml.closeEmptyElement();
  63.             return xml;
  64.         }

  65.         public static MarkableExtension from(Message message) {
  66.             return message.getExtension(MarkableExtension.class);
  67.         }
  68.     }

  69.     protected abstract static class ChatMarkerExtensionWithId implements ExtensionElement {
  70.         protected final String id;

  71.         protected ChatMarkerExtensionWithId(String id) {
  72.             this.id = StringUtils.requireNotNullNorEmpty(id, "Message ID must not be null");
  73.         }

  74.         /**
  75.          * Get the id.
  76.          *
  77.          * @return the id
  78.          */
  79.         public final String getId() {
  80.             return id;
  81.         }

  82.         @Override
  83.         public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  84.             XmlStringBuilder xml = new XmlStringBuilder(this);
  85.             xml.attribute("id", id);
  86.             xml.closeEmptyElement();
  87.             return xml;
  88.         }
  89.     }

  90.     /**
  91.      * Received extension class.
  92.      *
  93.      * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
  94.      *      Markers</a>
  95.      * @author Fernando Ramirez
  96.      *
  97.      */
  98.     public static class ReceivedExtension extends ChatMarkerExtensionWithId {

  99.         /**
  100.          * received element.
  101.          */
  102.         public static final String ELEMENT = ChatMarkersState.received.toString();
  103.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  104.         public ReceivedExtension(String id) {
  105.             super(id);
  106.         }

  107.         @Override
  108.         public String getElementName() {
  109.             return ELEMENT;
  110.         }

  111.         @Override
  112.         public String getNamespace() {
  113.             return NAMESPACE;
  114.         }

  115.         public static ReceivedExtension from(Message message) {
  116.             return message.getExtension(ReceivedExtension.class);
  117.         }
  118.     }

  119.     /**
  120.      * Displayed extension class.
  121.      *
  122.      * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
  123.      *      Markers</a>
  124.      * @author Fernando Ramirez
  125.      *
  126.      */
  127.     public static class DisplayedExtension extends ChatMarkerExtensionWithId {

  128.         /**
  129.          * displayed element.
  130.          */
  131.         public static final String ELEMENT = ChatMarkersState.displayed.toString();
  132.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  133.         public DisplayedExtension(String id) {
  134.             super(id);
  135.         }

  136.         @Override
  137.         public String getElementName() {
  138.             return ELEMENT;
  139.         }

  140.         @Override
  141.         public String getNamespace() {
  142.             return NAMESPACE;
  143.         }

  144.         public static DisplayedExtension from(Message message) {
  145.             return message.getExtension(DisplayedExtension.class);
  146.         }
  147.     }

  148.     /**
  149.      * Acknowledged extension class.
  150.      *
  151.      * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
  152.      *      Markers</a>
  153.      * @author Fernando Ramirez
  154.      *
  155.      */
  156.     public static class AcknowledgedExtension extends ChatMarkerExtensionWithId {

  157.         /**
  158.          * acknowledged element.
  159.          */
  160.         public static final String ELEMENT = ChatMarkersState.acknowledged.toString();
  161.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  162.         public AcknowledgedExtension(String id) {
  163.             super(id);
  164.         }

  165.         @Override
  166.         public String getElementName() {
  167.             return ELEMENT;
  168.         }

  169.         @Override
  170.         public String getNamespace() {
  171.             return NAMESPACE;
  172.         }

  173.         public static AcknowledgedExtension from(Message message) {
  174.             return message.getExtension(AcknowledgedExtension.class);
  175.         }
  176.     }

  177. }