001/*
002 *
003 * Copyright 2016 Fernando Ramirez
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.muclight.provider;
018
019import java.io.IOException;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.jivesoftware.smack.packet.IqData;
024import org.jivesoftware.smack.packet.XmlEnvironment;
025import org.jivesoftware.smack.provider.IqProvider;
026import org.jivesoftware.smack.xml.XmlPullParser;
027import org.jivesoftware.smack.xml.XmlPullParserException;
028
029import org.jivesoftware.smackx.muclight.MUCLightAffiliation;
030import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration;
031import org.jivesoftware.smackx.muclight.element.MUCLightInfoIQ;
032
033import org.jxmpp.JxmppContext;
034import org.jxmpp.jid.Jid;
035import org.jxmpp.jid.impl.JidCreate;
036
037/**
038 * MUC Light info IQ provider class.
039 *
040 * @author Fernando Ramirez
041 *
042 */
043public class MUCLightInfoIQProvider extends IqProvider<MUCLightInfoIQ> {
044
045    @Override
046    public MUCLightInfoIQ parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
047        String version = null;
048        String roomName = null;
049        String subject = null;
050        Map<String, String> customConfigs = null;
051        Map<Jid, MUCLightAffiliation> occupants = new HashMap<>();
052
053        outerloop: while (true) {
054            XmlPullParser.Event eventType = parser.next();
055
056            if (eventType == XmlPullParser.Event.START_ELEMENT) {
057
058                if (parser.getName().equals("version")) {
059                    version = parser.nextText();
060                }
061                if (parser.getName().equals("configuration")) {
062
063                    int depth = parser.getDepth();
064
065                    outerloop2: while (true) {
066                        eventType = parser.next();
067
068                        if (eventType == XmlPullParser.Event.START_ELEMENT) {
069                            if (parser.getName().equals("roomname")) {
070                                roomName = parser.nextText();
071                            } else if (parser.getName().equals("subject")) {
072                                subject = parser.nextText();
073                            } else {
074                                if (customConfigs == null) {
075                                    customConfigs = new HashMap<>();
076                                }
077                                customConfigs.put(parser.getName(), parser.nextText());
078                            }
079
080                        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
081                            if (parser.getDepth() == depth) {
082                                break outerloop2;
083                            }
084                        }
085                    }
086                }
087
088                if (parser.getName().equals("occupants")) {
089                    occupants = iterateOccupants(parser);
090                }
091
092            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
093                if (parser.getDepth() == initialDepth) {
094                    break outerloop;
095                }
096            }
097        }
098
099        return new MUCLightInfoIQ(version, new MUCLightRoomConfiguration(roomName, subject, customConfigs), occupants);
100    }
101
102    private static Map<Jid, MUCLightAffiliation> iterateOccupants(XmlPullParser parser) throws XmlPullParserException, IOException {
103        Map<Jid, MUCLightAffiliation> occupants = new HashMap<>();
104        int depth = parser.getDepth();
105
106        outerloop: while (true) {
107            XmlPullParser.Event eventType = parser.next();
108            if (eventType == XmlPullParser.Event.START_ELEMENT) {
109                if (parser.getName().equals("user")) {
110                    MUCLightAffiliation affiliation = MUCLightAffiliation
111                            .fromString(parser.getAttributeValue("", "affiliation"));
112                    occupants.put(JidCreate.from(parser.nextText()), affiliation);
113                }
114            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
115                if (parser.getDepth() == depth) {
116                    break outerloop;
117                }
118            }
119        }
120
121        return occupants;
122    }
123
124}