MonitorPacket.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.IQ;
  20. import org.jivesoftware.smack.packet.IqData;
  21. import org.jivesoftware.smack.packet.XmlEnvironment;
  22. import org.jivesoftware.smack.provider.IqProvider;
  23. import org.jivesoftware.smack.xml.XmlPullParser;
  24. import org.jivesoftware.smack.xml.XmlPullParserException;

  25. public class MonitorPacket extends IQ {

  26.     private String sessionID;

  27.     private boolean isMonitor;

  28.     public boolean isMonitor() {
  29.         return isMonitor;
  30.     }

  31.     public void setMonitor(boolean monitor) {
  32.         isMonitor = monitor;
  33.     }

  34.     public String getSessionID() {
  35.         return sessionID;
  36.     }

  37.     public void setSessionID(String sessionID) {
  38.         this.sessionID = sessionID;
  39.     }

  40.     /**
  41.      * Element name of the stanza extension.
  42.      */
  43.     public static final String ELEMENT_NAME = "monitor";

  44.     /**
  45.      * Namespace of the stanza extension.
  46.      */
  47.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

  48.     public MonitorPacket() {
  49.         super(ELEMENT_NAME, NAMESPACE);
  50.     }

  51.     @Override
  52.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  53.         buf.rightAngleBracket();

  54.         if (sessionID != null) {
  55.             buf.append("<makeOwner sessionID=\"" + sessionID + "\"></makeOwner>");
  56.         }

  57.         return buf;
  58.     }


  59.     /**
  60.      * Stanza extension provider for Monitor Packets.
  61.      */
  62.     public static class InternalProvider extends IqProvider<MonitorPacket> {

  63.         @Override
  64.         public MonitorPacket parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  65.             MonitorPacket packet = new MonitorPacket();

  66.             boolean done = false;


  67.             while (!done) {
  68.                 XmlPullParser.Event eventType = parser.next();
  69.                 if (eventType == XmlPullParser.Event.START_ELEMENT && "isMonitor".equals(parser.getName())) {
  70.                     String value = parser.nextText();
  71.                     if ("false".equalsIgnoreCase(value)) {
  72.                         packet.setMonitor(false);
  73.                     }
  74.                     else {
  75.                         packet.setMonitor(true);
  76.                     }
  77.                 }
  78.                 else if (eventType == XmlPullParser.Event.END_ELEMENT && "monitor".equals(parser.getName())) {
  79.                     done = true;
  80.                 }
  81.             }

  82.             return packet;
  83.         }
  84.     }
  85. }