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