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