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
020import java.io.IOException;
021
022import org.jivesoftware.smack.provider.ExtensionElementProvider;
023import org.jivesoftware.smack.util.ParserUtils;
024
025import org.jivesoftware.smackx.muc.packet.MUCUser;
026
027import org.jxmpp.jid.EntityBareJid;
028import org.jxmpp.jid.EntityJid;
029import org.xmlpull.v1.XmlPullParser;
030import org.xmlpull.v1.XmlPullParserException;
031
032/**
033 * The MUCUserProvider parses packets with extended presence information about 
034 * roles and affiliations.
035 *
036 * @author Gaston Dombiak
037 */
038public class MUCUserProvider extends ExtensionElementProvider<MUCUser> {
039
040    /**
041     * Parses a MUCUser stanza(/packet) (extension sub-packet).
042     *
043     * @param parser the XML parser, positioned at the starting element of the extension.
044     * @return a PacketExtension.
045     * @throws IOException 
046     * @throws XmlPullParserException 
047     */
048    @Override
049    public MUCUser parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
050        MUCUser mucUser = new MUCUser();
051        outerloop: while (true) {
052            switch (parser.next()) {
053            case XmlPullParser.START_TAG:
054                switch (parser.getName()) {
055                case "invite":
056                    mucUser.setInvite(parseInvite(parser));
057                    break;
058                case "item":
059                    mucUser.setItem(MUCParserUtils.parseItem(parser));
060                    break;
061                case "password":
062                    mucUser.setPassword(parser.nextText());
063                    break;
064                case "status":
065                    String statusString = parser.getAttributeValue("", "code");
066                    mucUser.addStatusCode(MUCUser.Status.create(statusString));
067                    break;
068                case "decline":
069                    mucUser.setDecline(parseDecline(parser));
070                    break;
071                case "destroy":
072                    mucUser.setDestroy(MUCParserUtils.parseDestroy(parser));
073                    break;
074                }
075                break;
076            case XmlPullParser.END_TAG:
077                if (parser.getDepth() == initialDepth) {
078                    break outerloop;
079                }
080                break;
081            }
082        }
083
084        return mucUser;
085    }
086
087    private static MUCUser.Invite parseInvite(XmlPullParser parser) throws XmlPullParserException, IOException {
088        String reason = null;
089        EntityBareJid to = ParserUtils.getBareJidAttribute(parser, "to");
090        EntityJid from = ParserUtils.getEntityJidAttribute(parser, "from");
091
092        outerloop: while (true) {
093            int eventType = parser.next();
094            if (eventType == XmlPullParser.START_TAG) {
095                if (parser.getName().equals("reason")) {
096                    reason = parser.nextText();
097                }
098            }
099            else if (eventType == XmlPullParser.END_TAG) {
100                if (parser.getName().equals("invite")) {
101                    break outerloop;
102                }
103            }
104        }
105        return new MUCUser.Invite(reason, from, to);
106    }
107
108    private static MUCUser.Decline parseDecline(XmlPullParser parser) throws XmlPullParserException, IOException {
109        String reason = null;
110        EntityBareJid to = ParserUtils.getBareJidAttribute(parser, "to");
111        EntityBareJid from = ParserUtils.getBareJidAttribute(parser, "from");
112
113        outerloop: while (true) {
114            int eventType = parser.next();
115            if (eventType == XmlPullParser.START_TAG) {
116                if (parser.getName().equals("reason")) {
117                    reason = parser.nextText();
118                }
119            }
120            else if (eventType == XmlPullParser.END_TAG) {
121                if (parser.getName().equals("decline")) {
122                    break outerloop;
123                }
124            }
125        }
126        return new MUCUser.Decline(reason, from, to);
127    }
128}