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