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