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.IQ;
021import org.jivesoftware.smack.provider.IQProvider;
022import org.jivesoftware.smackx.muc.packet.MUCAdmin;
023import org.xmlpull.v1.XmlPullParser;
024
025/**
026 * The MUCAdminProvider parses MUCAdmin packets. (@see MUCAdmin)
027 * 
028 * @author Gaston Dombiak
029 */
030public class MUCAdminProvider implements IQProvider {
031
032    public IQ parseIQ(XmlPullParser parser) throws Exception {
033        MUCAdmin mucAdmin = new MUCAdmin();
034        boolean done = false;
035        while (!done) {
036            int eventType = parser.next();
037            if (eventType == XmlPullParser.START_TAG) {
038                if (parser.getName().equals("item")) {
039                    mucAdmin.addItem(parseItem(parser));
040                }
041            }
042            else if (eventType == XmlPullParser.END_TAG) {
043                if (parser.getName().equals("query")) {
044                    done = true;
045                }
046            }
047        }
048
049        return mucAdmin;
050    }
051
052    private MUCAdmin.Item parseItem(XmlPullParser parser) throws Exception {
053        boolean done = false;
054        MUCAdmin.Item item =
055            new MUCAdmin.Item(
056                parser.getAttributeValue("", "affiliation"),
057                parser.getAttributeValue("", "role"));
058        item.setNick(parser.getAttributeValue("", "nick"));
059        item.setJid(parser.getAttributeValue("", "jid"));
060        while (!done) {
061            int eventType = parser.next();
062            if (eventType == XmlPullParser.START_TAG) {
063                if (parser.getName().equals("actor")) {
064                    item.setActor(parser.getAttributeValue("", "jid"));
065                }
066                if (parser.getName().equals("reason")) {
067                    item.setReason(parser.nextText());
068                }
069            }
070            else if (eventType == XmlPullParser.END_TAG) {
071                if (parser.getName().equals("item")) {
072                    done = true;
073                }
074            }
075        }
076        return item;
077    }
078}