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.muc.provider;
019
020
021import org.jivesoftware.smack.packet.PacketExtension;
022import org.jivesoftware.smack.provider.PacketExtensionProvider;
023import org.jivesoftware.smackx.muc.packet.MUCUser;
024import org.xmlpull.v1.XmlPullParser;
025
026/**
027 * The MUCUserProvider parses packets with extended presence information about 
028 * roles and affiliations.
029 *
030 * @author Gaston Dombiak
031 */
032public class MUCUserProvider implements PacketExtensionProvider {
033
034    /**
035     * Creates a new MUCUserProvider.
036     * ProviderManager requires that every PacketExtensionProvider has a public, no-argument 
037     * constructor
038     */
039    public MUCUserProvider() {
040    }
041
042    /**
043     * Parses a MUCUser packet (extension sub-packet).
044     *
045     * @param parser the XML parser, positioned at the starting element of the extension.
046     * @return a PacketExtension.
047     * @throws Exception if a parsing error occurs.
048     */
049    public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
050        MUCUser mucUser = new MUCUser();
051        boolean done = false;
052        while (!done) {
053            int eventType = parser.next();
054            if (eventType == XmlPullParser.START_TAG) {
055                if (parser.getName().equals("invite")) {
056                    mucUser.setInvite(parseInvite(parser));
057                }
058                if (parser.getName().equals("item")) {
059                    mucUser.setItem(parseItem(parser));
060                }
061                if (parser.getName().equals("password")) {
062                    mucUser.setPassword(parser.nextText());
063                }
064                if (parser.getName().equals("status")) {
065                    mucUser.setStatus(new MUCUser.Status(parser.getAttributeValue("", "code")));
066                }
067                if (parser.getName().equals("decline")) {
068                    mucUser.setDecline(parseDecline(parser));
069                }
070                if (parser.getName().equals("destroy")) {
071                    mucUser.setDestroy(parseDestroy(parser));
072                }
073            }
074            else if (eventType == XmlPullParser.END_TAG) {
075                if (parser.getName().equals("x")) {
076                    done = true;
077                }
078            }
079        }
080
081        return mucUser;
082    }
083
084    private MUCUser.Item parseItem(XmlPullParser parser) throws Exception {
085        boolean done = false;
086        MUCUser.Item item =
087            new MUCUser.Item(
088                parser.getAttributeValue("", "affiliation"),
089                parser.getAttributeValue("", "role"));
090        item.setNick(parser.getAttributeValue("", "nick"));
091        item.setJid(parser.getAttributeValue("", "jid"));
092        while (!done) {
093            int eventType = parser.next();
094            if (eventType == XmlPullParser.START_TAG) {
095                if (parser.getName().equals("actor")) {
096                    item.setActor(parser.getAttributeValue("", "jid"));
097                }
098                if (parser.getName().equals("reason")) {
099                    item.setReason(parser.nextText());
100                }
101            }
102            else if (eventType == XmlPullParser.END_TAG) {
103                if (parser.getName().equals("item")) {
104                    done = true;
105                }
106            }
107        }
108        return item;
109    }
110
111    private MUCUser.Invite parseInvite(XmlPullParser parser) throws Exception {
112        boolean done = false;
113        MUCUser.Invite invite = new MUCUser.Invite();
114        invite.setFrom(parser.getAttributeValue("", "from"));
115        invite.setTo(parser.getAttributeValue("", "to"));
116        while (!done) {
117            int eventType = parser.next();
118            if (eventType == XmlPullParser.START_TAG) {
119                if (parser.getName().equals("reason")) {
120                    invite.setReason(parser.nextText());
121                }
122            }
123            else if (eventType == XmlPullParser.END_TAG) {
124                if (parser.getName().equals("invite")) {
125                    done = true;
126                }
127            }
128        }
129        return invite;
130    }
131
132    private MUCUser.Decline parseDecline(XmlPullParser parser) throws Exception {
133        boolean done = false;
134        MUCUser.Decline decline = new MUCUser.Decline();
135        decline.setFrom(parser.getAttributeValue("", "from"));
136        decline.setTo(parser.getAttributeValue("", "to"));
137        while (!done) {
138            int eventType = parser.next();
139            if (eventType == XmlPullParser.START_TAG) {
140                if (parser.getName().equals("reason")) {
141                    decline.setReason(parser.nextText());
142                }
143            }
144            else if (eventType == XmlPullParser.END_TAG) {
145                if (parser.getName().equals("decline")) {
146                    done = true;
147                }
148            }
149        }
150        return decline;
151    }
152
153    private MUCUser.Destroy parseDestroy(XmlPullParser parser) throws Exception {
154        boolean done = false;
155        MUCUser.Destroy destroy = new MUCUser.Destroy();
156        destroy.setJid(parser.getAttributeValue("", "jid"));
157        while (!done) {
158            int eventType = parser.next();
159            if (eventType == XmlPullParser.START_TAG) {
160                if (parser.getName().equals("reason")) {
161                    destroy.setReason(parser.nextText());
162                }
163            }
164            else if (eventType == XmlPullParser.END_TAG) {
165                if (parser.getName().equals("destroy")) {
166                    done = true;
167                }
168            }
169        }
170        return destroy;
171    }
172}