001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.workgroup.packet;
019
020import java.io.IOException;
021
022import org.jivesoftware.smack.packet.IQ;
023import org.jivesoftware.smack.provider.IQProvider;
024import org.jivesoftware.smack.util.ParserUtils;
025import org.jxmpp.jid.Jid;
026import org.xmlpull.v1.XmlPullParser;
027import org.xmlpull.v1.XmlPullParserException;
028
029/**
030 * An IQProvider class which has savvy about the offer-revoke tag.<br>
031 *
032 * @author loki der quaeler
033 */
034public class OfferRevokeProvider extends IQProvider<IQ> {
035
036    @Override
037    public OfferRevokePacket parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
038        // The parser will be positioned on the opening IQ tag, so get the JID attribute.
039        Jid userJID = ParserUtils.getJidAttribute(parser);
040        // Default the userID to the JID.
041        Jid userID = userJID;
042        String reason = null;
043        String sessionID = null;
044        boolean done = false;
045
046        while (!done) {
047            int eventType = parser.next();
048
049            if ((eventType == XmlPullParser.START_TAG) && parser.getName().equals("reason")) {
050                reason = parser.nextText();
051            }
052            else if ((eventType == XmlPullParser.START_TAG)
053                         && parser.getName().equals(SessionID.ELEMENT_NAME)) {
054                sessionID = parser.getAttributeValue("", "id");
055            }
056            else if ((eventType == XmlPullParser.START_TAG)
057                         && parser.getName().equals(UserID.ELEMENT_NAME)) {
058                userID = ParserUtils.getJidAttribute(parser, "id");
059            }
060            else if ((eventType == XmlPullParser.END_TAG) && parser.getName().equals(
061                    "offer-revoke"))
062            {
063                done = true;
064            }
065        }
066
067        return new OfferRevokePacket(userJID, userID, reason, sessionID);
068    }
069
070    public static class OfferRevokePacket extends IQ {
071
072        public static final String ELEMENT = "offer-revoke";
073        public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
074        private Jid userJID;
075        private Jid userID;
076        private String sessionID;
077        private String reason;
078
079        public OfferRevokePacket (Jid userJID, Jid userID, String cause, String sessionID) {
080            super(ELEMENT, NAMESPACE);
081            this.userJID = userJID;
082            this.userID = userID;
083            this.reason = cause;
084            this.sessionID = sessionID;
085        }
086
087        public Jid getUserJID() {
088            return userJID;
089        }
090
091        public Jid getUserID() {
092            return this.userID;
093        }
094
095        public String getReason() {
096            return this.reason;
097        }
098
099        public String getSessionID() {
100            return this.sessionID;
101        }
102
103        @Override
104        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
105            buf.append(" jid=\"").append(userID).append("\">");
106            if (reason != null) {
107                buf.append("<reason>").append(reason).append("</reason>");
108            }
109            if (sessionID != null) {
110                buf.append(new SessionID(sessionID).toXML());
111            }
112            if (userID != null) {
113                buf.append(new UserID(userID).toXML());
114            }
115            return buf;
116        }
117    }
118}