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