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