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