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.IQ.Type;
023import org.jivesoftware.smack.provider.IQProvider;
024
025import org.jivesoftware.smackx.muclight.element.MUCLightBlockingIQ;
026
027import org.jxmpp.jid.Jid;
028import org.jxmpp.jid.impl.JidCreate;
029import org.jxmpp.stringprep.XmppStringprepException;
030import org.xmlpull.v1.XmlPullParser;
031import org.xmlpull.v1.XmlPullParserException;
032
033/**
034 * MUC Light blocking IQ provider class.
035 *
036 * @author Fernando Ramirez
037 *
038 */
039public class MUCLightBlockingIQProvider extends IQProvider<MUCLightBlockingIQ> {
040
041    @Override
042    public MUCLightBlockingIQ parse(XmlPullParser parser, int initialDepth) throws Exception {
043        HashMap<Jid, Boolean> rooms = null;
044        HashMap<Jid, Boolean> users = null;
045
046        outerloop: while (true) {
047            int eventType = parser.next();
048
049            if (eventType == XmlPullParser.START_TAG) {
050
051                if (parser.getName().equals("room")) {
052                    rooms = parseBlocking(parser, rooms);
053                }
054
055                if (parser.getName().equals("user")) {
056                    users = parseBlocking(parser, users);
057                }
058
059            } else if (eventType == XmlPullParser.END_TAG) {
060                if (parser.getDepth() == initialDepth) {
061                    break outerloop;
062                }
063            }
064        }
065
066        MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(rooms, users);
067        mucLightBlockingIQ.setType(Type.result);
068        return mucLightBlockingIQ;
069    }
070
071    private static HashMap<Jid, Boolean> parseBlocking(XmlPullParser parser, HashMap<Jid, Boolean> map)
072            throws XmppStringprepException, XmlPullParserException, IOException {
073        if (map == null) {
074            map = new HashMap<>();
075        }
076        String action = parser.getAttributeValue("", "action");
077
078        if (action.equals("deny")) {
079            map.put(JidCreate.from(parser.nextText()), false);
080        } else if (action.equals("allow")) {
081            map.put(JidCreate.from(parser.nextText()), true);
082        }
083        return map;
084    }
085
086}