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.packet.IqData;
024import org.jivesoftware.smack.packet.XmlEnvironment;
025import org.jivesoftware.smack.provider.IqProvider;
026import org.jivesoftware.smack.util.ParserUtils;
027import org.jivesoftware.smack.xml.XmlPullParser;
028import org.jivesoftware.smack.xml.XmlPullParserException;
029
030import org.jxmpp.JxmppContext;
031import org.jxmpp.jid.Jid;
032
033/**
034 * An IQProvider class which has savvy about the offer-revoke tag.<br>
035 *
036 * @author loki der quaeler
037 */
038public class OfferRevokeProvider extends IqProvider<IQ> {
039
040    @Override
041    public OfferRevokePacket parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
042        // The parser will be positioned on the opening IQ tag, so get the JID attribute.
043        Jid userJID = ParserUtils.getJidAttribute(parser, jxmppContext);
044        // Default the userID to the JID.
045        Jid userID = userJID;
046        String reason = null;
047        String sessionID = null;
048        boolean done = false;
049
050        while (!done) {
051            XmlPullParser.Event eventType = parser.next();
052
053            if ((eventType == XmlPullParser.Event.START_ELEMENT) && parser.getName().equals("reason")) {
054                reason = parser.nextText();
055            }
056            else if ((eventType == XmlPullParser.Event.START_ELEMENT)
057                         && parser.getName().equals(SessionID.ELEMENT_NAME)) {
058                sessionID = parser.getAttributeValue("", "id");
059            }
060            else if ((eventType == XmlPullParser.Event.START_ELEMENT)
061                         && parser.getName().equals(UserID.ELEMENT_NAME)) {
062                userID = ParserUtils.getJidAttribute(parser, "id", jxmppContext);
063            }
064            else if ((eventType == XmlPullParser.Event.END_ELEMENT) && parser.getName().equals(
065                    "offer-revoke")) {
066                done = true;
067            }
068        }
069
070        return new OfferRevokePacket(userJID, userID, reason, sessionID);
071    }
072
073    public static class OfferRevokePacket extends IQ {
074
075        public static final String ELEMENT = "offer-revoke";
076        public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
077        private final Jid userJID;
078        private final Jid userID;
079        private final String sessionID;
080        private final String reason;
081
082        public OfferRevokePacket (Jid userJID, Jid userID, String cause, String sessionID) {
083            super(ELEMENT, NAMESPACE);
084            this.userJID = userJID;
085            this.userID = userID;
086            this.reason = cause;
087            this.sessionID = sessionID;
088        }
089
090        public Jid getUserJID() {
091            return userJID;
092        }
093
094        public Jid getUserID() {
095            return this.userID;
096        }
097
098        public String getReason() {
099            return this.reason;
100        }
101
102        public String getSessionID() {
103            return this.sessionID;
104        }
105
106        @Override
107        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
108            buf.append(" jid=\"").append(userID).append("\">");
109            if (reason != null) {
110                buf.append("<reason>").append(reason).append("</reason>");
111            }
112            if (sessionID != null) {
113                buf.append(new SessionID(sessionID).toXML());
114            }
115            if (userID != null) {
116                buf.append(new UserID(userID).toXML());
117            }
118            return buf;
119        }
120    }
121}