OfferRevokeProvider.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.provider.IQProvider;
  21. import org.jivesoftware.smack.util.ParserUtils;
  22. import org.jxmpp.jid.Jid;
  23. import org.xmlpull.v1.XmlPullParser;
  24. import org.xmlpull.v1.XmlPullParserException;

  25. /**
  26.  * An IQProvider class which has savvy about the offer-revoke tag.<br>
  27.  *
  28.  * @author loki der quaeler
  29.  */
  30. public class OfferRevokeProvider extends IQProvider<IQ> {

  31.     @Override
  32.     public OfferRevokePacket parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
  33.         // The parser will be positioned on the opening IQ tag, so get the JID attribute.
  34.         Jid userJID = ParserUtils.getJidAttribute(parser);
  35.         // Default the userID to the JID.
  36.         Jid userID = userJID;
  37.         String reason = null;
  38.         String sessionID = null;
  39.         boolean done = false;

  40.         while (!done) {
  41.             int eventType = parser.next();

  42.             if ((eventType == XmlPullParser.START_TAG) && parser.getName().equals("reason")) {
  43.                 reason = parser.nextText();
  44.             }
  45.             else if ((eventType == XmlPullParser.START_TAG)
  46.                          && parser.getName().equals(SessionID.ELEMENT_NAME)) {
  47.                 sessionID = parser.getAttributeValue("", "id");
  48.             }
  49.             else if ((eventType == XmlPullParser.START_TAG)
  50.                          && parser.getName().equals(UserID.ELEMENT_NAME)) {
  51.                 userID = ParserUtils.getJidAttribute(parser, "id");
  52.             }
  53.             else if ((eventType == XmlPullParser.END_TAG) && parser.getName().equals(
  54.                     "offer-revoke"))
  55.             {
  56.                 done = true;
  57.             }
  58.         }

  59.         return new OfferRevokePacket(userJID, userID, reason, sessionID);
  60.     }

  61.     public class OfferRevokePacket extends IQ {

  62.         private Jid userJID;
  63.         private Jid userID;
  64.         private String sessionID;
  65.         private String reason;

  66.         public OfferRevokePacket (Jid userJID, Jid userID, String cause, String sessionID) {
  67.             super("offer-revoke", "http://jabber.org/protocol/workgroup");
  68.             this.userJID = userJID;
  69.             this.userID = userID;
  70.             this.reason = cause;
  71.             this.sessionID = sessionID;
  72.         }

  73.         public Jid getUserJID() {
  74.             return userJID;
  75.         }

  76.         public Jid getUserID() {
  77.             return this.userID;
  78.         }

  79.         public String getReason() {
  80.             return this.reason;
  81.         }

  82.         public String getSessionID() {
  83.             return this.sessionID;
  84.         }

  85.         @Override
  86.         protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  87.             buf.append(" jid=\"").append(userID).append("\">");
  88.             if (reason != null) {
  89.                 buf.append("<reason>").append(reason).append("</reason>");
  90.             }
  91.             if (sessionID != null) {
  92.                 buf.append(new SessionID(sessionID).toXML());
  93.             }
  94.             if (userID != null) {
  95.                 buf.append(new UserID(userID).toXML());
  96.             }
  97.             return buf;
  98.         }
  99.     }
  100. }