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