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