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.smack.util.ParserUtils;
022
023import org.jivesoftware.smackx.muc.MUCAffiliation;
024import org.jivesoftware.smackx.muc.MUCRole;
025import org.jivesoftware.smackx.muc.packet.Destroy;
026import org.jivesoftware.smackx.muc.packet.MUCItem;
027
028import org.jxmpp.jid.EntityBareJid;
029import org.jxmpp.jid.Jid;
030import org.jxmpp.jid.parts.Resourcepart;
031import org.xmlpull.v1.XmlPullParser;
032import org.xmlpull.v1.XmlPullParserException;
033
034public class MUCParserUtils {
035    public static MUCItem parseItem(XmlPullParser parser) throws XmlPullParserException, IOException {
036        int initialDepth = parser.getDepth();
037        MUCAffiliation affiliation = MUCAffiliation.fromString(parser.getAttributeValue("", "affiliation"));
038        Resourcepart nick = ParserUtils.getResourcepartAttribute(parser, "nick");
039        MUCRole role = MUCRole.fromString(parser.getAttributeValue("", "role"));
040        Jid jid = ParserUtils.getJidAttribute(parser);
041        Jid actor = null;
042        Resourcepart actorNick = null;
043        String reason = null;
044        outerloop: while (true) {
045            int eventType = parser.next();
046            switch (eventType) {
047            case XmlPullParser.START_TAG:
048                String name = parser.getName();
049                switch (name) {
050                case "actor":
051                    actor = ParserUtils.getJidAttribute(parser);
052                    // TODO change to
053                    // actorNick = Resourcepart.from(parser.getAttributeValue("", "nick"));
054                    // once a newer version of JXMPP is used that supports from(null).
055                    String actorNickString = parser.getAttributeValue("", "nick");
056                    if (actorNickString != null) {
057                        actorNick = Resourcepart.from(actorNickString);
058                    }
059                    break;
060                case "reason":
061                    reason = parser.nextText();
062                    break;
063                }
064                break;
065            case XmlPullParser.END_TAG:
066                if (parser.getDepth() == initialDepth) {
067                    break outerloop;
068                }
069                break;
070            }
071        }
072        return new MUCItem(affiliation, role, actor, reason, jid, nick, actorNick);
073    }
074
075    public static Destroy parseDestroy(XmlPullParser parser) throws XmlPullParserException, IOException {
076        final int initialDepth = parser.getDepth();
077        final EntityBareJid jid = ParserUtils.getBareJidAttribute(parser);
078        String reason = null;
079        outerloop: while (true) {
080            int eventType = parser.next();
081            switch (eventType) {
082            case XmlPullParser.START_TAG:
083                final String name = parser.getName();
084                switch (name) {
085                case "reason":
086                    reason = parser.nextText();
087                    break;
088                }
089                break;
090            case XmlPullParser.END_TAG:
091                if (initialDepth == parser.getDepth()) {
092                    break outerloop;
093                }
094                break;
095            }
096        }
097        return new Destroy(jid, reason);
098    }
099}