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