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 org.jivesoftware.smack.packet.*;
021import org.jivesoftware.smack.provider.*;
022import org.jivesoftware.smack.util.PacketParserUtils;
023import org.jivesoftware.smackx.muc.packet.MUCOwner;
024import org.xmlpull.v1.XmlPullParser;
025
026/**
027 * The MUCOwnerProvider parses MUCOwner packets. (@see MUCOwner)
028 * 
029 * @author Gaston Dombiak
030 */
031public class MUCOwnerProvider implements IQProvider {
032
033    public IQ parseIQ(XmlPullParser parser) throws Exception {
034        MUCOwner mucOwner = new MUCOwner();
035        boolean done = false;
036        while (!done) {
037            int eventType = parser.next();
038            if (eventType == XmlPullParser.START_TAG) {
039                if (parser.getName().equals("item")) {
040                    mucOwner.addItem(parseItem(parser));
041                }
042                else if (parser.getName().equals("destroy")) {
043                    mucOwner.setDestroy(parseDestroy(parser));
044                }
045                // Otherwise, it must be a packet extension.
046                else {
047                    mucOwner.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(),
048                            parser.getNamespace(), parser));
049                }
050            }
051            else if (eventType == XmlPullParser.END_TAG) {
052                if (parser.getName().equals("query")) {
053                    done = true;
054                }
055            }
056        }
057
058        return mucOwner;
059    }
060
061    private MUCOwner.Item parseItem(XmlPullParser parser) throws Exception {
062        boolean done = false;
063        MUCOwner.Item item = new MUCOwner.Item(parser.getAttributeValue("", "affiliation"));
064        item.setNick(parser.getAttributeValue("", "nick"));
065        item.setRole(parser.getAttributeValue("", "role"));
066        item.setJid(parser.getAttributeValue("", "jid"));
067        while (!done) {
068            int eventType = parser.next();
069            if (eventType == XmlPullParser.START_TAG) {
070                if (parser.getName().equals("actor")) {
071                    item.setActor(parser.getAttributeValue("", "jid"));
072                }
073                if (parser.getName().equals("reason")) {
074                    item.setReason(parser.nextText());
075                }
076            }
077            else if (eventType == XmlPullParser.END_TAG) {
078                if (parser.getName().equals("item")) {
079                    done = true;
080                }
081            }
082        }
083        return item;
084    }
085
086    private MUCOwner.Destroy parseDestroy(XmlPullParser parser) throws Exception {
087        boolean done = false;
088        MUCOwner.Destroy destroy = new MUCOwner.Destroy();
089        destroy.setJid(parser.getAttributeValue("", "jid"));
090        while (!done) {
091            int eventType = parser.next();
092            if (eventType == XmlPullParser.START_TAG) {
093                if (parser.getName().equals("reason")) {
094                    destroy.setReason(parser.nextText());
095                }
096            }
097            else if (eventType == XmlPullParser.END_TAG) {
098                if (parser.getName().equals("destroy")) {
099                    done = true;
100                }
101            }
102        }
103        return destroy;
104    }
105}