QueueUpdate.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.workgroup.packet;

  18. import java.io.IOException;

  19. import javax.xml.namespace.QName;

  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.packet.XmlEnvironment;
  22. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  23. import org.jivesoftware.smack.xml.XmlPullParser;
  24. import org.jivesoftware.smack.xml.XmlPullParserException;

  25. /**
  26.  * An IQ stanza that encapsulates both types of workgroup queue
  27.  * status notifications -- position updates, and estimated time
  28.  * left in the queue updates.
  29.  */
  30. public class QueueUpdate implements ExtensionElement {

  31.     /**
  32.      * Element name of the stanza extension.
  33.      */
  34.     public static final String ELEMENT_NAME = "queue-status";

  35.     /**
  36.      * Namespace of the stanza extension.
  37.      */
  38.     public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";

  39.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT_NAME);

  40.     private final int position;
  41.     private final int remainingTime;

  42.     public QueueUpdate(int position, int remainingTime) {
  43.         this.position = position;
  44.         this.remainingTime = remainingTime;
  45.     }

  46.     /**
  47.      * Returns the user's position in the workgroup queue, or -1 if the
  48.      * value isn't set on this packet.
  49.      *
  50.      * @return the position in the workgroup queue.
  51.      */
  52.     public int getPosition() {
  53.         return this.position;
  54.     }

  55.     /**
  56.      * Returns the user's estimated time left in the workgroup queue, or
  57.      * -1 if the value isn't set on this packet.
  58.      *
  59.      * @return the estimated time left in the workgroup queue.
  60.      */
  61.     public int getRemaingTime() {
  62.         return remainingTime;
  63.     }

  64.     @Override
  65.     public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  66.         StringBuilder buf = new StringBuilder();
  67.         buf.append("<queue-status xmlns=\"http://jabber.org/protocol/workgroup\">");
  68.         if (position != -1) {
  69.             buf.append("<position>").append(position).append("</position>");
  70.         }
  71.         if (remainingTime != -1) {
  72.             buf.append("<time>").append(remainingTime).append("</time>");
  73.         }
  74.         buf.append("</queue-status>");
  75.         return buf.toString();
  76.     }

  77.     @Override
  78.     public String getElementName() {
  79.         return ELEMENT_NAME;
  80.     }

  81.     @Override
  82.     public String getNamespace() {
  83.         return NAMESPACE;
  84.     }

  85.     public static class Provider extends ExtensionElementProvider<QueueUpdate> {

  86.         @Override
  87.         public QueueUpdate parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
  88.                         throws XmlPullParserException, IOException {
  89.             boolean done = false;
  90.             int position = -1;
  91.             int timeRemaining = -1;
  92.             while (!done) {
  93.                 parser.next();
  94.                 if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT && "position".equals(parser.getName())) {
  95.                     position = Integer.parseInt(parser.nextText());
  96.                 }
  97.                 else if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT && "time".equals(parser.getName())) {
  98.                     timeRemaining = Integer.parseInt(parser.nextText());
  99.                 }
  100.                 else if (parser.getEventType() == XmlPullParser.Event.END_ELEMENT && "queue-status".equals(parser.getName())) {
  101.                     done = true;
  102.                 }
  103.             }
  104.             return new QueueUpdate(position, timeRemaining);
  105.         }
  106.     }
  107. }