EventElement.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  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.pubsub;

  18. import java.util.Arrays;
  19. import java.util.List;

  20. import javax.xml.namespace.QName;

  21. import org.jivesoftware.smack.packet.ExtensionElement;
  22. import org.jivesoftware.smack.packet.Stanza;
  23. import org.jivesoftware.smack.packet.XmlElement;
  24. import org.jivesoftware.smack.util.XmlStringBuilder;

  25. import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;

  26. /**
  27.  * Represents the top level element of a PubSub event extension.  All types of PubSub events are
  28.  * represented by this class.  The specific type can be found by {@link #getEventType()}.  The
  29.  * embedded event information, which is specific to the event type, can be retrieved by the {@link #getEvent()}
  30.  * method.
  31.  *
  32.  * @author Robin Collier
  33.  */
  34. public class EventElement implements EmbeddedPacketExtension, ExtensionElement {
  35.     /**
  36.      * The constant String "event".
  37.      */
  38.     public static final String ELEMENT = "event";

  39.     /**
  40.      * The constant String "http://jabber.org/protocol/pubsub#event".
  41.      */
  42.     public static final String NAMESPACE = PubSubNamespace.event.getXmlns();

  43.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  44.     private final EventElementType type;
  45.     private final NodeExtension ext;

  46.     public EventElement(EventElementType eventType, NodeExtension eventExt) {
  47.         type = eventType;
  48.         ext = eventExt;
  49.     }

  50.     public EventElementType getEventType() {
  51.         return type;
  52.     }

  53.     @Override
  54.     public List<XmlElement> getExtensions() {
  55.         return Arrays.asList(new XmlElement[] {getEvent()});
  56.     }

  57.     public NodeExtension getEvent() {
  58.         return ext;
  59.     }

  60.     @Override
  61.     public String getElementName() {
  62.         return QNAME.getLocalPart();
  63.     }

  64.     @Override
  65.     public String getNamespace() {
  66.         return QNAME.getNamespaceURI();
  67.     }

  68.     @Override
  69.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  70.         XmlStringBuilder xml = new XmlStringBuilder(this);
  71.         xml.rightAngleBracket();
  72.         xml.append(ext.toXML());
  73.         xml.closeElement(this);
  74.         return xml;
  75.     }

  76.     public static EventElement from(Stanza stanza) {
  77.         return stanza.getExtension(EventElement.class);
  78.     }
  79. }