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.ExtensionElementProvider;
024import org.jivesoftware.smack.xml.XmlPullParser;
025import org.jivesoftware.smack.xml.XmlPullParserException;
026
027import org.jivesoftware.smackx.muclight.MUCLightAffiliation;
028import org.jivesoftware.smackx.muclight.element.MUCLightElements.AffiliationsChangeExtension;
029
030import org.jxmpp.JxmppContext;
031import org.jxmpp.jid.Jid;
032import org.jxmpp.jid.impl.JidCreate;
033
034/**
035 * MUC Light Affiliations Change Provider class.
036 *
037 * @author Fernando Ramirez
038 *
039 */
040public class MUCLightAffiliationsChangeProvider extends ExtensionElementProvider<AffiliationsChangeExtension> {
041
042    @Override
043    public AffiliationsChangeExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
044        HashMap<Jid, MUCLightAffiliation> affiliations = new HashMap<>();
045        String prevVersion = null;
046        String version = null;
047
048        outerloop: while (true) {
049            XmlPullParser.Event eventType = parser.next();
050
051            if (eventType == XmlPullParser.Event.START_ELEMENT) {
052
053                if (parser.getName().equals("prev-version")) {
054                    prevVersion = parser.nextText();
055                }
056
057                if (parser.getName().equals("version")) {
058                    version = parser.nextText();
059                }
060
061                if (parser.getName().equals("user")) {
062                    MUCLightAffiliation mucLightAffiliation = MUCLightAffiliation
063                            .fromString(parser.getAttributeValue("", "affiliation"));
064                    Jid jid = JidCreate.from(parser.nextText());
065                    affiliations.put(jid, mucLightAffiliation);
066                }
067
068            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
069                if (parser.getDepth() == initialDepth) {
070                    break outerloop;
071                }
072            }
073        }
074
075        return new AffiliationsChangeExtension(affiliations, prevVersion, version);
076    }
077
078}