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