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                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 final Jid userJID;
075        private final Jid userID;
076        private final String sessionID;
077        private final 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(null));
111            }
112            if (userID != null) {
113                buf.append(new UserID(userID).toXML(null));
114            }
115            return buf;
116        }
117    }
118}