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.XmlEnvironment;
024import org.jivesoftware.smack.provider.IQProvider;
025import org.jivesoftware.smack.util.ParserUtils;
026import org.jivesoftware.smack.xml.XmlPullParser;
027import org.jivesoftware.smack.xml.XmlPullParserException;
028
029import org.jxmpp.jid.Jid;
030
031/**
032 * An IQProvider class which has savvy about the offer-revoke tag.<br>
033 *
034 * @author loki der quaeler
035 */
036public class OfferRevokeProvider extends IQProvider<IQ> {
037
038    @Override
039    public OfferRevokePacket parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
040        // The parser will be positioned on the opening IQ tag, so get the JID attribute.
041        Jid userJID = ParserUtils.getJidAttribute(parser);
042        // Default the userID to the JID.
043        Jid userID = userJID;
044        String reason = null;
045        String sessionID = null;
046        boolean done = false;
047
048        while (!done) {
049            XmlPullParser.Event eventType = parser.next();
050
051            if ((eventType == XmlPullParser.Event.START_ELEMENT) && parser.getName().equals("reason")) {
052                reason = parser.nextText();
053            }
054            else if ((eventType == XmlPullParser.Event.START_ELEMENT)
055                         && parser.getName().equals(SessionID.ELEMENT_NAME)) {
056                sessionID = parser.getAttributeValue("", "id");
057            }
058            else if ((eventType == XmlPullParser.Event.START_ELEMENT)
059                         && parser.getName().equals(UserID.ELEMENT_NAME)) {
060                userID = ParserUtils.getJidAttribute(parser, "id");
061            }
062            else if ((eventType == XmlPullParser.Event.END_ELEMENT) && parser.getName().equals(
063                    "offer-revoke")) {
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}