PEPEvent.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  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.pep.packet;

  18. import org.jivesoftware.smack.packet.ExtensionElement;

  19. /**
  20.  * Represents XMPP Personal Event Protocol packets.<p>
  21.  *
  22.  * The 'http://jabber.org/protocol/pubsub#event' namespace  is used to publish personal events items from one client
  23.  * to subscribed clients (See XEP-163).
  24.  *
  25.  * @author Jeff Williams
  26.  */
  27. public class PEPEvent implements ExtensionElement {

  28.     PEPItem item;

  29.     /**
  30.      * Creates a new empty roster exchange package.
  31.      *
  32.      */
  33.     public PEPEvent() {
  34.         super();
  35.     }

  36.     /**
  37.      * Creates a new empty roster exchange package.
  38.      *
  39.      */
  40.     public PEPEvent(PEPItem item) {
  41.         super();

  42.         this.item = item;
  43.     }
  44.    
  45.     public void addPEPItem(PEPItem item) {
  46.         this.item = item;
  47.     }

  48.     /**
  49.     * Returns the XML element name of the extension sub-packet root element.
  50.     * Always returns "x"
  51.     *
  52.     * @return the XML element name of the packet extension.
  53.     */
  54.     public String getElementName() {
  55.         return "event";
  56.     }

  57.     /**
  58.      * Returns the XML namespace of the extension sub-packet root element.
  59.      * According the specification the namespace is always "jabber:x:roster"
  60.      * (which is not to be confused with the 'jabber:iq:roster' namespace
  61.      *
  62.      * @return the XML namespace of the packet extension.
  63.      */
  64.     public String getNamespace() {
  65.         return "http://jabber.org/protocol/pubsub";
  66.     }

  67.     /**
  68.      * Returns the XML representation of a Personal Event Publish according the specification.
  69.      *
  70.      * Usually the XML representation will be inside of a Message XML representation like
  71.      * in the following example:
  72.      * <pre>
  73.      * &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
  74.      *     &lt;subject&gt;Any subject you want&lt;/subject&gt;
  75.      *     &lt;body&gt;This message contains roster items.&lt;/body&gt;
  76.      *     &lt;x xmlns="jabber:x:roster"&gt;
  77.      *         &lt;item jid="gato1@gato.home"/&gt;
  78.      *         &lt;item jid="gato2@gato.home"/&gt;
  79.      *     &lt;/x&gt;
  80.      * &lt;/message&gt;
  81.      * </pre>
  82.      *
  83.      */
  84.     public String toXML() {
  85.         StringBuilder buf = new StringBuilder();
  86.         buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">");
  87.         buf.append(item.toXML());
  88.         buf.append("</").append(getElementName()).append(">");
  89.         return buf.toString();
  90.     }

  91. }