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 */
017package org.jivesoftware.smackx.muc.provider;
018
019import java.io.IOException;
020
021import org.jivesoftware.smackx.muc.MUCAffiliation;
022import org.jivesoftware.smackx.muc.MUCRole;
023import org.jivesoftware.smackx.muc.packet.Destroy;
024import org.jivesoftware.smackx.muc.packet.MUCItem;
025import org.xmlpull.v1.XmlPullParser;
026import org.xmlpull.v1.XmlPullParserException;
027
028public class MUCParserUtils {
029    public static MUCItem parseItem(XmlPullParser parser) throws XmlPullParserException, IOException {
030        int initialDepth = parser.getDepth();
031        MUCAffiliation affiliation = MUCAffiliation.fromString(parser.getAttributeValue("", "affiliation"));
032        String nick = parser.getAttributeValue("", "nick");
033        MUCRole role = MUCRole.fromString(parser.getAttributeValue("", "role"));
034        String jid = parser.getAttributeValue("", "jid");
035        String actor = null;
036        String reason = null;
037        outerloop: while (true) {
038            int eventType = parser.next();
039            switch (eventType) {
040            case XmlPullParser.START_TAG:
041                String name = parser.getName();
042                switch (name) {
043                case "actor":
044                    actor = parser.getAttributeValue("", "jid");
045                    break;
046                case "reason":
047                    reason = parser.nextText();
048                    break;
049                }
050                break;
051            case XmlPullParser.END_TAG:
052                if (parser.getDepth() == initialDepth) {
053                    break outerloop;
054                }
055                break;
056            }
057        }
058        return new MUCItem(affiliation, role, actor, reason, jid, nick);
059    }
060
061    public static Destroy parseDestroy(XmlPullParser parser) throws XmlPullParserException, IOException {
062        boolean done = false;
063        Destroy destroy = new Destroy();
064        destroy.setJid(parser.getAttributeValue("", "jid"));
065        while (!done) {
066            int eventType = parser.next();
067            if (eventType == XmlPullParser.START_TAG) {
068                if (parser.getName().equals("reason")) {
069                    destroy.setReason(parser.nextText());
070                }
071            }
072            else if (eventType == XmlPullParser.END_TAG) {
073                if (parser.getName().equals("destroy")) {
074                    done = true;
075                }
076            }
077        }
078        return destroy;
079    }
080}