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