OfflineMessageInfo.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.offline.packet;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  21. import org.xmlpull.v1.XmlPullParser;
  22. import org.xmlpull.v1.XmlPullParserException;

  23. /**
  24.  * OfflineMessageInfo is an extension included in the retrieved offline messages requested by
  25.  * the {@link org.jivesoftware.smackx.offline.OfflineMessageManager}. This extension includes a stamp
  26.  * that uniquely identifies the offline message. This stamp may be used for deleting the offline
  27.  * message. The stamp may be of the form UTC timestamps but it is not required to have that format.
  28.  *
  29.  * @author Gaston Dombiak
  30.  */
  31. public class OfflineMessageInfo implements ExtensionElement {

  32.     private String node = null;

  33.     /**
  34.     * Returns the XML element name of the extension sub-packet root element.
  35.     * Always returns "offline"
  36.     *
  37.     * @return the XML element name of the packet extension.
  38.     */
  39.     public String getElementName() {
  40.         return "offline";
  41.     }

  42.     /**
  43.      * Returns the XML namespace of the extension sub-packet root element.
  44.      * According the specification the namespace is always "http://jabber.org/protocol/offline"
  45.      *
  46.      * @return the XML namespace of the packet extension.
  47.      */
  48.     public String getNamespace() {
  49.         return "http://jabber.org/protocol/offline";
  50.     }

  51.     /**
  52.      * Returns the stamp that uniquely identifies the offline message. This stamp may
  53.      * be used for deleting the offline message. The stamp may be of the form UTC timestamps
  54.      * but it is not required to have that format.
  55.      *
  56.      * @return the stamp that uniquely identifies the offline message.
  57.      */
  58.     public String getNode() {
  59.         return node;
  60.     }

  61.     /**
  62.      * Sets the stamp that uniquely identifies the offline message. This stamp may
  63.      * be used for deleting the offline message. The stamp may be of the form UTC timestamps
  64.      * but it is not required to have that format.
  65.      *
  66.      * @param node the stamp that uniquely identifies the offline message.
  67.      */
  68.     public void setNode(String node) {
  69.         this.node = node;
  70.     }

  71.     public String toXML() {
  72.         StringBuilder buf = new StringBuilder();
  73.         buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
  74.             "\">");
  75.         if (getNode() != null)
  76.             buf.append("<item node=\"").append(getNode()).append("\"/>");
  77.         buf.append("</").append(getElementName()).append(">");
  78.         return buf.toString();
  79.     }

  80.     public static class Provider extends ExtensionElementProvider<OfflineMessageInfo> {

  81.         /**
  82.          * Parses a OfflineMessageInfo packet (extension sub-packet).
  83.          *
  84.          * @param parser the XML parser, positioned at the starting element of the extension.
  85.          * @return a PacketExtension.
  86.          * @throws IOException
  87.          * @throws XmlPullParserException
  88.          */
  89.         @Override
  90.         public OfflineMessageInfo parse(XmlPullParser parser,
  91.                         int initialDepth) throws XmlPullParserException,
  92.                         IOException {
  93.             OfflineMessageInfo info = new OfflineMessageInfo();
  94.             boolean done = false;
  95.             while (!done) {
  96.                 int eventType = parser.next();
  97.                 if (eventType == XmlPullParser.START_TAG) {
  98.                     if (parser.getName().equals("item"))
  99.                         info.setNode(parser.getAttributeValue("", "node"));
  100.                 } else if (eventType == XmlPullParser.END_TAG) {
  101.                     if (parser.getName().equals("offline")) {
  102.                         done = true;
  103.                     }
  104.                 }
  105.             }

  106.             return info;
  107.         }

  108.     }
  109. }