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