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        private String userJID;
071        private String userID;
072        private String sessionID;
073        private String reason;
074
075        public OfferRevokePacket (String userJID, String userID, String cause, String sessionID) {
076            super("offer-revoke", "http://jabber.org/protocol/workgroup");
077            this.userJID = userJID;
078            this.userID = userID;
079            this.reason = cause;
080            this.sessionID = sessionID;
081        }
082
083        public String getUserJID() {
084            return userJID;
085        }
086
087        public String getUserID() {
088            return this.userID;
089        }
090
091        public String getReason() {
092            return this.reason;
093        }
094
095        public String getSessionID() {
096            return this.sessionID;
097        }
098
099        @Override
100        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
101            buf.append(" jid=\"").append(userID).append("\">");
102            if (reason != null) {
103                buf.append("<reason>").append(reason).append("</reason>");
104            }
105            if (sessionID != null) {
106                buf.append(new SessionID(sessionID).toXML());
107            }
108            if (userID != null) {
109                buf.append(new UserID(userID).toXML());
110            }
111            return buf;
112        }
113    }
114}