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 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.  * An IQ packet that encapsulates both types of workgroup queue
  25.  * status notifications -- position updates, and estimated time
  26.  * left in the queue updates.
  27.  */
  28. public class QueueUpdate implements ExtensionElement {

  29.     /**
  30.      * Element name of the packet extension.
  31.      */
  32.     public static final String ELEMENT_NAME = "queue-status";

  33.     /**
  34.      * Namespace of the packet extension.
  35.      */
  36.     public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";

  37.     private int position;
  38.     private int remainingTime;

  39.     public QueueUpdate(int position, int remainingTime) {
  40.         this.position = position;
  41.         this.remainingTime = remainingTime;
  42.     }

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

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

  61.     public String toXML() {
  62.         StringBuilder buf = new StringBuilder();
  63.         buf.append("<queue-status xmlns=\"http://jabber.org/protocol/workgroup\">");
  64.         if (position != -1) {
  65.             buf.append("<position>").append(position).append("</position>");
  66.         }
  67.         if (remainingTime != -1) {
  68.             buf.append("<time>").append(remainingTime).append("</time>");
  69.         }
  70.         buf.append("</queue-status>");
  71.         return buf.toString();
  72.     }

  73.     public String getElementName() {
  74.         return ELEMENT_NAME;
  75.     }

  76.     public String getNamespace() {
  77.         return NAMESPACE;
  78.     }

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

  80.         @Override
  81.         public QueueUpdate parse(XmlPullParser parser, int initialDepth)
  82.                         throws XmlPullParserException, IOException {
  83.             boolean done = false;
  84.             int position = -1;
  85.             int timeRemaining = -1;
  86.             while (!done) {
  87.                 parser.next();
  88.                 String elementName = parser.getName();
  89.                 if (parser.getEventType() == XmlPullParser.START_TAG && "position".equals(elementName)) {
  90.                     try {
  91.                         position = Integer.parseInt(parser.nextText());
  92.                     }
  93.                     catch (NumberFormatException nfe) {
  94.                     }
  95.                 }
  96.                 else if (parser.getEventType() == XmlPullParser.START_TAG && "time".equals(elementName)) {
  97.                     try {
  98.                         timeRemaining = Integer.parseInt(parser.nextText());
  99.                     }
  100.                     catch (NumberFormatException nfe) {
  101.                     }
  102.                 }
  103.                 else if (parser.getEventType() == XmlPullParser.END_TAG && "queue-status".equals(elementName)) {
  104.                     done = true;
  105.                 }
  106.             }
  107.             return new QueueUpdate(position, timeRemaining);
  108.         }
  109.     }
  110. }