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.jid.EntityBareJid;
031import org.jxmpp.jid.Jid;
032import org.jxmpp.jid.parts.Resourcepart;
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            XmlPullParser.Event eventType = parser.next();
046            switch (eventType) {
047            case START_ELEMENT:
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 END_ELEMENT:
066                if (parser.getDepth() == initialDepth) {
067                    break outerloop;
068                }
069                break;
070            default:
071                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
072                break;
073            }
074        }
075        return new MUCItem(affiliation, role, actor, reason, jid, nick, actorNick);
076    }
077
078    public static Destroy parseDestroy(XmlPullParser parser) throws XmlPullParserException, IOException {
079        final int initialDepth = parser.getDepth();
080        final EntityBareJid jid = ParserUtils.getBareJidAttribute(parser);
081        String reason = null;
082        outerloop: while (true) {
083            XmlPullParser.Event eventType = parser.next();
084            switch (eventType) {
085            case START_ELEMENT:
086                final String name = parser.getName();
087                switch (name) {
088                case "reason":
089                    reason = parser.nextText();
090                    break;
091                }
092                break;
093            case END_ELEMENT:
094                if (initialDepth == parser.getDepth()) {
095                    break outerloop;
096                }
097                break;
098            default:
099                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
100                break;
101            }
102        }
103        return new Destroy(jid, reason);
104    }
105}